freeclient_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2018 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 les
  17. import (
  18. "fmt"
  19. "math/rand"
  20. "strconv"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common/mclock"
  24. "github.com/ethereum/go-ethereum/core/rawdb"
  25. )
  26. func TestFreeClientPoolL10C100(t *testing.T) {
  27. testFreeClientPool(t, 10, 100)
  28. }
  29. func TestFreeClientPoolL40C200(t *testing.T) {
  30. testFreeClientPool(t, 40, 200)
  31. }
  32. func TestFreeClientPoolL100C300(t *testing.T) {
  33. testFreeClientPool(t, 100, 300)
  34. }
  35. const testFreeClientPoolTicks = 500000
  36. func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
  37. var (
  38. clock mclock.Simulated
  39. db = rawdb.NewMemoryDatabase()
  40. connected = make([]bool, clientCount)
  41. connTicks = make([]int, clientCount)
  42. disconnCh = make(chan int, clientCount)
  43. peerAddress = func(i int) string {
  44. return fmt.Sprintf("addr #%d", i)
  45. }
  46. peerId = func(i int) string {
  47. return fmt.Sprintf("id #%d", i)
  48. }
  49. disconnFn = func(id string) {
  50. i, err := strconv.Atoi(id[4:])
  51. if err != nil {
  52. panic(err)
  53. }
  54. disconnCh <- i
  55. }
  56. pool = newFreeClientPool(db, 1, 10000, &clock, disconnFn)
  57. )
  58. pool.setLimits(connLimit, uint64(connLimit))
  59. // pool should accept new peers up to its connected limit
  60. for i := 0; i < connLimit; i++ {
  61. if pool.connect(peerAddress(i), peerId(i)) {
  62. connected[i] = true
  63. } else {
  64. t.Fatalf("Test peer #%d rejected", i)
  65. }
  66. }
  67. // since all accepted peers are new and should not be kicked out, the next one should be rejected
  68. if pool.connect(peerAddress(connLimit), peerId(connLimit)) {
  69. connected[connLimit] = true
  70. t.Fatalf("Peer accepted over connected limit")
  71. }
  72. // randomly connect and disconnect peers, expect to have a similar total connection time at the end
  73. for tickCounter := 0; tickCounter < testFreeClientPoolTicks; tickCounter++ {
  74. clock.Run(1 * time.Second)
  75. i := rand.Intn(clientCount)
  76. if connected[i] {
  77. pool.disconnect(peerAddress(i))
  78. connected[i] = false
  79. connTicks[i] += tickCounter
  80. } else {
  81. if pool.connect(peerAddress(i), peerId(i)) {
  82. connected[i] = true
  83. connTicks[i] -= tickCounter
  84. }
  85. }
  86. pollDisconnects:
  87. for {
  88. select {
  89. case i := <-disconnCh:
  90. pool.disconnect(peerAddress(i))
  91. if connected[i] {
  92. connTicks[i] += tickCounter
  93. connected[i] = false
  94. }
  95. default:
  96. break pollDisconnects
  97. }
  98. }
  99. }
  100. expTicks := testFreeClientPoolTicks * connLimit / clientCount
  101. expMin := expTicks - expTicks/10
  102. expMax := expTicks + expTicks/10
  103. // check if the total connected time of peers are all in the expected range
  104. for i, c := range connected {
  105. if c {
  106. connTicks[i] += testFreeClientPoolTicks
  107. }
  108. if connTicks[i] < expMin || connTicks[i] > expMax {
  109. t.Errorf("Total connected time of test node #%d (%d) outside expected range (%d to %d)", i, connTicks[i], expMin, expMax)
  110. }
  111. }
  112. // a previously unknown peer should be accepted now
  113. if !pool.connect("newAddr", "newId") {
  114. t.Fatalf("Previously unknown peer rejected")
  115. }
  116. // close and restart pool
  117. pool.stop()
  118. pool = newFreeClientPool(db, 1, 10000, &clock, disconnFn)
  119. pool.setLimits(connLimit, uint64(connLimit))
  120. // try connecting all known peers (connLimit should be filled up)
  121. for i := 0; i < clientCount; i++ {
  122. pool.connect(peerAddress(i), peerId(i))
  123. }
  124. // expect pool to remember known nodes and kick out one of them to accept a new one
  125. if !pool.connect("newAddr2", "newId2") {
  126. t.Errorf("Previously unknown peer rejected after restarting pool")
  127. }
  128. pool.stop()
  129. }