iter_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // Copyright 2019 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 enode
  17. import (
  18. "encoding/binary"
  19. "runtime"
  20. "sync/atomic"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/p2p/enr"
  24. )
  25. func TestReadNodes(t *testing.T) {
  26. nodes := ReadNodes(new(genIter), 10)
  27. checkNodes(t, nodes, 10)
  28. }
  29. // This test checks that ReadNodes terminates when reading N nodes from an iterator
  30. // which returns less than N nodes in an endless cycle.
  31. func TestReadNodesCycle(t *testing.T) {
  32. iter := &callCountIter{
  33. Iterator: CycleNodes([]*Node{
  34. testNode(0, 0),
  35. testNode(1, 0),
  36. testNode(2, 0),
  37. }),
  38. }
  39. nodes := ReadNodes(iter, 10)
  40. checkNodes(t, nodes, 3)
  41. if iter.count != 10 {
  42. t.Fatalf("%d calls to Next, want %d", iter.count, 100)
  43. }
  44. }
  45. func TestFilterNodes(t *testing.T) {
  46. nodes := make([]*Node, 100)
  47. for i := range nodes {
  48. nodes[i] = testNode(uint64(i), uint64(i))
  49. }
  50. it := Filter(IterNodes(nodes), func(n *Node) bool {
  51. return n.Seq() >= 50
  52. })
  53. for i := 50; i < len(nodes); i++ {
  54. if !it.Next() {
  55. t.Fatal("Next returned false")
  56. }
  57. if it.Node() != nodes[i] {
  58. t.Fatalf("iterator returned wrong node %v\nwant %v", it.Node(), nodes[i])
  59. }
  60. }
  61. if it.Next() {
  62. t.Fatal("Next returned true after underlying iterator has ended")
  63. }
  64. }
  65. func checkNodes(t *testing.T, nodes []*Node, wantLen int) {
  66. if len(nodes) != wantLen {
  67. t.Errorf("slice has %d nodes, want %d", len(nodes), wantLen)
  68. return
  69. }
  70. seen := make(map[ID]bool)
  71. for i, e := range nodes {
  72. if e == nil {
  73. t.Errorf("nil node at index %d", i)
  74. return
  75. }
  76. if seen[e.ID()] {
  77. t.Errorf("slice has duplicate node %v", e.ID())
  78. return
  79. }
  80. seen[e.ID()] = true
  81. }
  82. }
  83. // This test checks fairness of FairMix in the happy case where all sources return nodes
  84. // within the context's deadline.
  85. func TestFairMix(t *testing.T) {
  86. for i := 0; i < 500; i++ {
  87. testMixerFairness(t)
  88. }
  89. }
  90. func testMixerFairness(t *testing.T) {
  91. mix := NewFairMix(1 * time.Second)
  92. mix.AddSource(&genIter{index: 1})
  93. mix.AddSource(&genIter{index: 2})
  94. mix.AddSource(&genIter{index: 3})
  95. defer mix.Close()
  96. nodes := ReadNodes(mix, 500)
  97. checkNodes(t, nodes, 500)
  98. // Verify that the nodes slice contains an approximately equal number of nodes
  99. // from each source.
  100. d := idPrefixDistribution(nodes)
  101. for _, count := range d {
  102. if approxEqual(count, len(nodes)/3, 30) {
  103. t.Fatalf("ID distribution is unfair: %v", d)
  104. }
  105. }
  106. }
  107. // This test checks that FairMix falls back to an alternative source when
  108. // the 'fair' choice doesn't return a node within the timeout.
  109. func TestFairMixNextFromAll(t *testing.T) {
  110. mix := NewFairMix(1 * time.Millisecond)
  111. mix.AddSource(&genIter{index: 1})
  112. mix.AddSource(CycleNodes(nil))
  113. defer mix.Close()
  114. nodes := ReadNodes(mix, 500)
  115. checkNodes(t, nodes, 500)
  116. d := idPrefixDistribution(nodes)
  117. if len(d) > 1 || d[1] != len(nodes) {
  118. t.Fatalf("wrong ID distribution: %v", d)
  119. }
  120. }
  121. // This test ensures FairMix works for Next with no sources.
  122. func TestFairMixEmpty(t *testing.T) {
  123. var (
  124. mix = NewFairMix(1 * time.Second)
  125. testN = testNode(1, 1)
  126. ch = make(chan *Node)
  127. )
  128. defer mix.Close()
  129. go func() {
  130. mix.Next()
  131. ch <- mix.Node()
  132. }()
  133. mix.AddSource(CycleNodes([]*Node{testN}))
  134. if n := <-ch; n != testN {
  135. t.Errorf("got wrong node: %v", n)
  136. }
  137. }
  138. // This test checks closing a source while Next runs.
  139. func TestFairMixRemoveSource(t *testing.T) {
  140. mix := NewFairMix(1 * time.Second)
  141. source := make(blockingIter)
  142. mix.AddSource(source)
  143. sig := make(chan *Node)
  144. go func() {
  145. <-sig
  146. mix.Next()
  147. sig <- mix.Node()
  148. }()
  149. sig <- nil
  150. runtime.Gosched()
  151. source.Close()
  152. wantNode := testNode(0, 0)
  153. mix.AddSource(CycleNodes([]*Node{wantNode}))
  154. n := <-sig
  155. if len(mix.sources) != 1 {
  156. t.Fatalf("have %d sources, want one", len(mix.sources))
  157. }
  158. if n != wantNode {
  159. t.Fatalf("mixer returned wrong node")
  160. }
  161. }
  162. type blockingIter chan struct{}
  163. func (it blockingIter) Next() bool {
  164. <-it
  165. return false
  166. }
  167. func (it blockingIter) Node() *Node {
  168. return nil
  169. }
  170. func (it blockingIter) Close() {
  171. close(it)
  172. }
  173. func TestFairMixClose(t *testing.T) {
  174. for i := 0; i < 20 && !t.Failed(); i++ {
  175. testMixerClose(t)
  176. }
  177. }
  178. func testMixerClose(t *testing.T) {
  179. mix := NewFairMix(-1)
  180. mix.AddSource(CycleNodes(nil))
  181. mix.AddSource(CycleNodes(nil))
  182. done := make(chan struct{})
  183. go func() {
  184. defer close(done)
  185. if mix.Next() {
  186. t.Error("Next returned true")
  187. }
  188. }()
  189. // This call is supposed to make it more likely that NextNode is
  190. // actually executing by the time we call Close.
  191. runtime.Gosched()
  192. mix.Close()
  193. select {
  194. case <-done:
  195. case <-time.After(3 * time.Second):
  196. t.Fatal("Next didn't unblock on Close")
  197. }
  198. mix.Close() // shouldn't crash
  199. }
  200. func idPrefixDistribution(nodes []*Node) map[uint32]int {
  201. d := make(map[uint32]int)
  202. for _, node := range nodes {
  203. id := node.ID()
  204. d[binary.BigEndian.Uint32(id[:4])]++
  205. }
  206. return d
  207. }
  208. func approxEqual(x, y, ε int) bool {
  209. if y > x {
  210. x, y = y, x
  211. }
  212. return x-y > ε
  213. }
  214. // genIter creates fake nodes with numbered IDs based on 'index' and 'gen'
  215. type genIter struct {
  216. node *Node
  217. index, gen uint32
  218. }
  219. func (s *genIter) Next() bool {
  220. index := atomic.LoadUint32(&s.index)
  221. if index == ^uint32(0) {
  222. s.node = nil
  223. return false
  224. }
  225. s.node = testNode(uint64(index)<<32|uint64(s.gen), 0)
  226. s.gen++
  227. return true
  228. }
  229. func (s *genIter) Node() *Node {
  230. return s.node
  231. }
  232. func (s *genIter) Close() {
  233. s.index = ^uint32(0)
  234. }
  235. func testNode(id, seq uint64) *Node {
  236. var nodeID ID
  237. binary.BigEndian.PutUint64(nodeID[:], id)
  238. r := new(enr.Record)
  239. r.SetSeq(seq)
  240. return SignNull(r, nodeID)
  241. }
  242. // callCountIter counts calls to NextNode.
  243. type callCountIter struct {
  244. Iterator
  245. count int
  246. }
  247. func (it *callCountIter) Next() bool {
  248. it.count++
  249. return it.Iterator.Next()
  250. }