freeclient_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package light implements on-demand retrieval capable state and chain objects
  17. // for the Ethereum Light Client.
  18. package les
  19. import (
  20. "fmt"
  21. "math/rand"
  22. "testing"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common/mclock"
  25. "github.com/ethereum/go-ethereum/ethdb"
  26. )
  27. func TestFreeClientPoolL10C100(t *testing.T) {
  28. testFreeClientPool(t, 10, 100)
  29. }
  30. func TestFreeClientPoolL40C200(t *testing.T) {
  31. testFreeClientPool(t, 40, 200)
  32. }
  33. func TestFreeClientPoolL100C300(t *testing.T) {
  34. testFreeClientPool(t, 100, 300)
  35. }
  36. const testFreeClientPoolTicks = 500000
  37. func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
  38. var (
  39. clock mclock.Simulated
  40. db = ethdb.NewMemDatabase()
  41. pool = newFreeClientPool(db, connLimit, 10000, &clock)
  42. connected = make([]bool, clientCount)
  43. connTicks = make([]int, clientCount)
  44. disconnCh = make(chan int, clientCount)
  45. )
  46. peerId := func(i int) string {
  47. return fmt.Sprintf("test peer #%d", i)
  48. }
  49. disconnFn := func(i int) func() {
  50. return func() {
  51. disconnCh <- i
  52. }
  53. }
  54. // pool should accept new peers up to its connected limit
  55. for i := 0; i < connLimit; i++ {
  56. if pool.connect(peerId(i), disconnFn(i)) {
  57. connected[i] = true
  58. } else {
  59. t.Fatalf("Test peer #%d rejected", i)
  60. }
  61. }
  62. // since all accepted peers are new and should not be kicked out, the next one should be rejected
  63. if pool.connect(peerId(connLimit), disconnFn(connLimit)) {
  64. connected[connLimit] = true
  65. t.Fatalf("Peer accepted over connected limit")
  66. }
  67. // randomly connect and disconnect peers, expect to have a similar total connection time at the end
  68. for tickCounter := 0; tickCounter < testFreeClientPoolTicks; tickCounter++ {
  69. clock.Run(1 * time.Second)
  70. i := rand.Intn(clientCount)
  71. if connected[i] {
  72. pool.disconnect(peerId(i))
  73. connected[i] = false
  74. connTicks[i] += tickCounter
  75. } else {
  76. if pool.connect(peerId(i), disconnFn(i)) {
  77. connected[i] = true
  78. connTicks[i] -= tickCounter
  79. }
  80. }
  81. pollDisconnects:
  82. for {
  83. select {
  84. case i := <-disconnCh:
  85. pool.disconnect(peerId(i))
  86. if connected[i] {
  87. connTicks[i] += tickCounter
  88. connected[i] = false
  89. }
  90. default:
  91. break pollDisconnects
  92. }
  93. }
  94. }
  95. expTicks := testFreeClientPoolTicks * connLimit / clientCount
  96. expMin := expTicks - expTicks/10
  97. expMax := expTicks + expTicks/10
  98. // check if the total connected time of peers are all in the expected range
  99. for i, c := range connected {
  100. if c {
  101. connTicks[i] += testFreeClientPoolTicks
  102. }
  103. if connTicks[i] < expMin || connTicks[i] > expMax {
  104. t.Errorf("Total connected time of test node #%d (%d) outside expected range (%d to %d)", i, connTicks[i], expMin, expMax)
  105. }
  106. }
  107. // a previously unknown peer should be accepted now
  108. if !pool.connect("newPeer", func() {}) {
  109. t.Fatalf("Previously unknown peer rejected")
  110. }
  111. // close and restart pool
  112. pool.stop()
  113. pool = newFreeClientPool(db, connLimit, 10000, &clock)
  114. // try connecting all known peers (connLimit should be filled up)
  115. for i := 0; i < clientCount; i++ {
  116. pool.connect(peerId(i), func() {})
  117. }
  118. // expect pool to remember known nodes and kick out one of them to accept a new one
  119. if !pool.connect("newPeer2", func() {}) {
  120. t.Errorf("Previously unknown peer rejected after restarting pool")
  121. }
  122. pool.stop()
  123. }