wrsiterator_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "reflect"
  19. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common/mclock"
  22. "github.com/ethereum/go-ethereum/p2p/nodestate"
  23. )
  24. var (
  25. testSetup = &nodestate.Setup{}
  26. sfTest1 = testSetup.NewFlag("test1")
  27. sfTest2 = testSetup.NewFlag("test2")
  28. sfTest3 = testSetup.NewFlag("test3")
  29. sfTest4 = testSetup.NewFlag("test4")
  30. sfiTestWeight = testSetup.NewField("nodeWeight", reflect.TypeOf(uint64(0)))
  31. )
  32. const iterTestNodeCount = 6
  33. func TestWrsIterator(t *testing.T) {
  34. ns := nodestate.NewNodeStateMachine(nil, nil, &mclock.Simulated{}, testSetup)
  35. w := NewWrsIterator(ns, sfTest2, sfTest3.Or(sfTest4), sfiTestWeight)
  36. ns.Start()
  37. for i := 1; i <= iterTestNodeCount; i++ {
  38. ns.SetState(testNode(i), sfTest1, nodestate.Flags{}, 0)
  39. ns.SetField(testNode(i), sfiTestWeight, uint64(1))
  40. }
  41. next := func() int {
  42. ch := make(chan struct{})
  43. go func() {
  44. w.Next()
  45. close(ch)
  46. }()
  47. select {
  48. case <-ch:
  49. case <-time.After(time.Second * 5):
  50. t.Fatalf("Iterator.Next() timeout")
  51. }
  52. node := w.Node()
  53. ns.SetState(node, sfTest4, nodestate.Flags{}, 0)
  54. return testNodeIndex(node.ID())
  55. }
  56. set := make(map[int]bool)
  57. expset := func() {
  58. for len(set) > 0 {
  59. n := next()
  60. if !set[n] {
  61. t.Errorf("Item returned by iterator not in the expected set (got %d)", n)
  62. }
  63. delete(set, n)
  64. }
  65. }
  66. ns.SetState(testNode(1), sfTest2, nodestate.Flags{}, 0)
  67. ns.SetState(testNode(2), sfTest2, nodestate.Flags{}, 0)
  68. ns.SetState(testNode(3), sfTest2, nodestate.Flags{}, 0)
  69. set[1] = true
  70. set[2] = true
  71. set[3] = true
  72. expset()
  73. ns.SetState(testNode(4), sfTest2, nodestate.Flags{}, 0)
  74. ns.SetState(testNode(5), sfTest2.Or(sfTest3), nodestate.Flags{}, 0)
  75. ns.SetState(testNode(6), sfTest2, nodestate.Flags{}, 0)
  76. set[4] = true
  77. set[6] = true
  78. expset()
  79. ns.SetField(testNode(2), sfiTestWeight, uint64(0))
  80. ns.SetState(testNode(1), nodestate.Flags{}, sfTest4, 0)
  81. ns.SetState(testNode(2), nodestate.Flags{}, sfTest4, 0)
  82. ns.SetState(testNode(3), nodestate.Flags{}, sfTest4, 0)
  83. set[1] = true
  84. set[3] = true
  85. expset()
  86. ns.SetField(testNode(2), sfiTestWeight, uint64(1))
  87. ns.SetState(testNode(2), nodestate.Flags{}, sfTest2, 0)
  88. ns.SetState(testNode(1), nodestate.Flags{}, sfTest4, 0)
  89. ns.SetState(testNode(2), sfTest2, sfTest4, 0)
  90. ns.SetState(testNode(3), nodestate.Flags{}, sfTest4, 0)
  91. set[1] = true
  92. set[2] = true
  93. set[3] = true
  94. expset()
  95. ns.Stop()
  96. }