serverpool_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright 2020 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 client
  17. import (
  18. "math/rand"
  19. "strconv"
  20. "sync/atomic"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common/mclock"
  24. "github.com/ethereum/go-ethereum/ethdb"
  25. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  26. "github.com/ethereum/go-ethereum/p2p/enode"
  27. "github.com/ethereum/go-ethereum/p2p/enr"
  28. )
  29. const (
  30. spTestNodes = 1000
  31. spTestTarget = 5
  32. spTestLength = 10000
  33. spMinTotal = 40000
  34. spMaxTotal = 50000
  35. )
  36. func testNodeID(i int) enode.ID {
  37. return enode.ID{42, byte(i % 256), byte(i / 256)}
  38. }
  39. func testNodeIndex(id enode.ID) int {
  40. if id[0] != 42 {
  41. return -1
  42. }
  43. return int(id[1]) + int(id[2])*256
  44. }
  45. type ServerPoolTest struct {
  46. db ethdb.KeyValueStore
  47. clock *mclock.Simulated
  48. quit chan struct{}
  49. preNeg, preNegFail bool
  50. vt *ValueTracker
  51. sp *ServerPool
  52. spi enode.Iterator
  53. input enode.Iterator
  54. testNodes []spTestNode
  55. trusted []string
  56. waitCount, waitEnded int32
  57. cycle, conn, servedConn int
  58. serviceCycles, dialCount int
  59. disconnect map[int][]int
  60. }
  61. type spTestNode struct {
  62. connectCycles, waitCycles int
  63. nextConnCycle, totalConn int
  64. connected, service bool
  65. node *enode.Node
  66. }
  67. func newServerPoolTest(preNeg, preNegFail bool) *ServerPoolTest {
  68. nodes := make([]*enode.Node, spTestNodes)
  69. for i := range nodes {
  70. nodes[i] = enode.SignNull(&enr.Record{}, testNodeID(i))
  71. }
  72. return &ServerPoolTest{
  73. clock: &mclock.Simulated{},
  74. db: memorydb.New(),
  75. input: enode.CycleNodes(nodes),
  76. testNodes: make([]spTestNode, spTestNodes),
  77. preNeg: preNeg,
  78. preNegFail: preNegFail,
  79. }
  80. }
  81. func (s *ServerPoolTest) beginWait() {
  82. // ensure that dialIterator and the maximal number of pre-neg queries are not all stuck in a waiting state
  83. for atomic.AddInt32(&s.waitCount, 1) > preNegLimit {
  84. atomic.AddInt32(&s.waitCount, -1)
  85. s.clock.Run(time.Second)
  86. }
  87. }
  88. func (s *ServerPoolTest) endWait() {
  89. atomic.AddInt32(&s.waitCount, -1)
  90. atomic.AddInt32(&s.waitEnded, 1)
  91. }
  92. func (s *ServerPoolTest) addTrusted(i int) {
  93. s.trusted = append(s.trusted, enode.SignNull(&enr.Record{}, testNodeID(i)).String())
  94. }
  95. func (s *ServerPoolTest) start() {
  96. var testQuery QueryFunc
  97. if s.preNeg {
  98. testQuery = func(node *enode.Node) int {
  99. idx := testNodeIndex(node.ID())
  100. n := &s.testNodes[idx]
  101. canConnect := !n.connected && n.connectCycles != 0 && s.cycle >= n.nextConnCycle
  102. if s.preNegFail {
  103. // simulate a scenario where UDP queries never work
  104. s.beginWait()
  105. s.clock.Sleep(time.Second * 5)
  106. s.endWait()
  107. return -1
  108. }
  109. switch idx % 3 {
  110. case 0:
  111. // pre-neg returns true only if connection is possible
  112. if canConnect {
  113. return 1
  114. }
  115. return 0
  116. case 1:
  117. // pre-neg returns true but connection might still fail
  118. return 1
  119. case 2:
  120. // pre-neg returns true if connection is possible, otherwise timeout (node unresponsive)
  121. if canConnect {
  122. return 1
  123. }
  124. s.beginWait()
  125. s.clock.Sleep(time.Second * 5)
  126. s.endWait()
  127. return -1
  128. }
  129. return -1
  130. }
  131. }
  132. requestList := make([]RequestInfo, testReqTypes)
  133. for i := range requestList {
  134. requestList[i] = RequestInfo{Name: "testreq" + strconv.Itoa(i), InitAmount: 1, InitValue: 1}
  135. }
  136. s.sp, s.spi = NewServerPool(s.db, []byte("sp:"), 0, testQuery, s.clock, s.trusted, requestList)
  137. s.sp.AddSource(s.input)
  138. s.sp.validSchemes = enode.ValidSchemesForTesting
  139. s.sp.unixTime = func() int64 { return int64(s.clock.Now()) / int64(time.Second) }
  140. s.disconnect = make(map[int][]int)
  141. s.sp.Start()
  142. s.quit = make(chan struct{})
  143. go func() {
  144. last := int32(-1)
  145. for {
  146. select {
  147. case <-time.After(time.Millisecond * 100):
  148. c := atomic.LoadInt32(&s.waitEnded)
  149. if c == last {
  150. // advance clock if test is stuck (might happen in rare cases)
  151. s.clock.Run(time.Second)
  152. }
  153. last = c
  154. case <-s.quit:
  155. return
  156. }
  157. }
  158. }()
  159. }
  160. func (s *ServerPoolTest) stop() {
  161. close(s.quit)
  162. s.sp.Stop()
  163. s.spi.Close()
  164. for i := range s.testNodes {
  165. n := &s.testNodes[i]
  166. if n.connected {
  167. n.totalConn += s.cycle
  168. }
  169. n.connected = false
  170. n.node = nil
  171. n.nextConnCycle = 0
  172. }
  173. s.conn, s.servedConn = 0, 0
  174. }
  175. func (s *ServerPoolTest) run() {
  176. for count := spTestLength; count > 0; count-- {
  177. if dcList := s.disconnect[s.cycle]; dcList != nil {
  178. for _, idx := range dcList {
  179. n := &s.testNodes[idx]
  180. s.sp.UnregisterNode(n.node)
  181. n.totalConn += s.cycle
  182. n.connected = false
  183. n.node = nil
  184. s.conn--
  185. if n.service {
  186. s.servedConn--
  187. }
  188. n.nextConnCycle = s.cycle + n.waitCycles
  189. }
  190. delete(s.disconnect, s.cycle)
  191. }
  192. if s.conn < spTestTarget {
  193. s.dialCount++
  194. s.beginWait()
  195. s.spi.Next()
  196. s.endWait()
  197. dial := s.spi.Node()
  198. id := dial.ID()
  199. idx := testNodeIndex(id)
  200. n := &s.testNodes[idx]
  201. if !n.connected && n.connectCycles != 0 && s.cycle >= n.nextConnCycle {
  202. s.conn++
  203. if n.service {
  204. s.servedConn++
  205. }
  206. n.totalConn -= s.cycle
  207. n.connected = true
  208. dc := s.cycle + n.connectCycles
  209. s.disconnect[dc] = append(s.disconnect[dc], idx)
  210. n.node = dial
  211. nv, _ := s.sp.RegisterNode(n.node)
  212. if n.service {
  213. nv.Served([]ServedRequest{{ReqType: 0, Amount: 100}}, 0)
  214. }
  215. }
  216. }
  217. s.serviceCycles += s.servedConn
  218. s.clock.Run(time.Second)
  219. s.cycle++
  220. }
  221. }
  222. func (s *ServerPoolTest) setNodes(count, conn, wait int, service, trusted bool) (res []int) {
  223. for ; count > 0; count-- {
  224. idx := rand.Intn(spTestNodes)
  225. for s.testNodes[idx].connectCycles != 0 || s.testNodes[idx].connected {
  226. idx = rand.Intn(spTestNodes)
  227. }
  228. res = append(res, idx)
  229. s.testNodes[idx] = spTestNode{
  230. connectCycles: conn,
  231. waitCycles: wait,
  232. service: service,
  233. }
  234. if trusted {
  235. s.addTrusted(idx)
  236. }
  237. }
  238. return
  239. }
  240. func (s *ServerPoolTest) resetNodes() {
  241. for i, n := range s.testNodes {
  242. if n.connected {
  243. n.totalConn += s.cycle
  244. s.sp.UnregisterNode(n.node)
  245. }
  246. s.testNodes[i] = spTestNode{totalConn: n.totalConn}
  247. }
  248. s.conn, s.servedConn = 0, 0
  249. s.disconnect = make(map[int][]int)
  250. s.trusted = nil
  251. }
  252. func (s *ServerPoolTest) checkNodes(t *testing.T, nodes []int) {
  253. var sum int
  254. for _, idx := range nodes {
  255. n := &s.testNodes[idx]
  256. if n.connected {
  257. n.totalConn += s.cycle
  258. }
  259. sum += n.totalConn
  260. n.totalConn = 0
  261. if n.connected {
  262. n.totalConn -= s.cycle
  263. }
  264. }
  265. if sum < spMinTotal || sum > spMaxTotal {
  266. t.Errorf("Total connection amount %d outside expected range %d to %d", sum, spMinTotal, spMaxTotal)
  267. }
  268. }
  269. func TestServerPool(t *testing.T) { testServerPool(t, false, false) }
  270. func TestServerPoolWithPreNeg(t *testing.T) { testServerPool(t, true, false) }
  271. func TestServerPoolWithPreNegFail(t *testing.T) { testServerPool(t, true, true) }
  272. func testServerPool(t *testing.T, preNeg, fail bool) {
  273. s := newServerPoolTest(preNeg, fail)
  274. nodes := s.setNodes(100, 200, 200, true, false)
  275. s.setNodes(100, 20, 20, false, false)
  276. s.start()
  277. s.run()
  278. s.stop()
  279. s.checkNodes(t, nodes)
  280. }
  281. func TestServerPoolChangedNodes(t *testing.T) { testServerPoolChangedNodes(t, false) }
  282. func TestServerPoolChangedNodesWithPreNeg(t *testing.T) { testServerPoolChangedNodes(t, true) }
  283. func testServerPoolChangedNodes(t *testing.T, preNeg bool) {
  284. s := newServerPoolTest(preNeg, false)
  285. nodes := s.setNodes(100, 200, 200, true, false)
  286. s.setNodes(100, 20, 20, false, false)
  287. s.start()
  288. s.run()
  289. s.checkNodes(t, nodes)
  290. for i := 0; i < 3; i++ {
  291. s.resetNodes()
  292. nodes := s.setNodes(100, 200, 200, true, false)
  293. s.setNodes(100, 20, 20, false, false)
  294. s.run()
  295. s.checkNodes(t, nodes)
  296. }
  297. s.stop()
  298. }
  299. func TestServerPoolRestartNoDiscovery(t *testing.T) { testServerPoolRestartNoDiscovery(t, false) }
  300. func TestServerPoolRestartNoDiscoveryWithPreNeg(t *testing.T) {
  301. testServerPoolRestartNoDiscovery(t, true)
  302. }
  303. func testServerPoolRestartNoDiscovery(t *testing.T, preNeg bool) {
  304. s := newServerPoolTest(preNeg, false)
  305. nodes := s.setNodes(100, 200, 200, true, false)
  306. s.setNodes(100, 20, 20, false, false)
  307. s.start()
  308. s.run()
  309. s.stop()
  310. s.checkNodes(t, nodes)
  311. s.input = nil
  312. s.start()
  313. s.run()
  314. s.stop()
  315. s.checkNodes(t, nodes)
  316. }
  317. func TestServerPoolTrustedNoDiscovery(t *testing.T) { testServerPoolTrustedNoDiscovery(t, false) }
  318. func TestServerPoolTrustedNoDiscoveryWithPreNeg(t *testing.T) {
  319. testServerPoolTrustedNoDiscovery(t, true)
  320. }
  321. func testServerPoolTrustedNoDiscovery(t *testing.T, preNeg bool) {
  322. s := newServerPoolTest(preNeg, false)
  323. trusted := s.setNodes(200, 200, 200, true, true)
  324. s.input = nil
  325. s.start()
  326. s.run()
  327. s.stop()
  328. s.checkNodes(t, trusted)
  329. }