queueiterator_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. "testing"
  19. "time"
  20. "github.com/ethereum/go-ethereum/common/mclock"
  21. "github.com/ethereum/go-ethereum/p2p/enode"
  22. "github.com/ethereum/go-ethereum/p2p/enr"
  23. "github.com/ethereum/go-ethereum/p2p/nodestate"
  24. )
  25. func testNodeID(i int) enode.ID {
  26. return enode.ID{42, byte(i % 256), byte(i / 256)}
  27. }
  28. func testNodeIndex(id enode.ID) int {
  29. if id[0] != 42 {
  30. return -1
  31. }
  32. return int(id[1]) + int(id[2])*256
  33. }
  34. func testNode(i int) *enode.Node {
  35. return enode.SignNull(new(enr.Record), testNodeID(i))
  36. }
  37. func TestQueueIteratorFIFO(t *testing.T) {
  38. testQueueIterator(t, true)
  39. }
  40. func TestQueueIteratorLIFO(t *testing.T) {
  41. testQueueIterator(t, false)
  42. }
  43. func testQueueIterator(t *testing.T, fifo bool) {
  44. ns := nodestate.NewNodeStateMachine(nil, nil, &mclock.Simulated{}, testSetup)
  45. qi := NewQueueIterator(ns, sfTest2, sfTest3.Or(sfTest4), fifo, nil)
  46. ns.Start()
  47. for i := 1; i <= iterTestNodeCount; i++ {
  48. ns.SetState(testNode(i), sfTest1, nodestate.Flags{}, 0)
  49. }
  50. next := func() int {
  51. ch := make(chan struct{})
  52. go func() {
  53. qi.Next()
  54. close(ch)
  55. }()
  56. select {
  57. case <-ch:
  58. case <-time.After(time.Second * 5):
  59. t.Fatalf("Iterator.Next() timeout")
  60. }
  61. node := qi.Node()
  62. ns.SetState(node, sfTest4, nodestate.Flags{}, 0)
  63. return testNodeIndex(node.ID())
  64. }
  65. exp := func(i int) {
  66. n := next()
  67. if n != i {
  68. t.Errorf("Wrong item returned by iterator (expected %d, got %d)", i, n)
  69. }
  70. }
  71. explist := func(list []int) {
  72. for i := range list {
  73. if fifo {
  74. exp(list[i])
  75. } else {
  76. exp(list[len(list)-1-i])
  77. }
  78. }
  79. }
  80. ns.SetState(testNode(1), sfTest2, nodestate.Flags{}, 0)
  81. ns.SetState(testNode(2), sfTest2, nodestate.Flags{}, 0)
  82. ns.SetState(testNode(3), sfTest2, nodestate.Flags{}, 0)
  83. explist([]int{1, 2, 3})
  84. ns.SetState(testNode(4), sfTest2, nodestate.Flags{}, 0)
  85. ns.SetState(testNode(5), sfTest2, nodestate.Flags{}, 0)
  86. ns.SetState(testNode(6), sfTest2, nodestate.Flags{}, 0)
  87. ns.SetState(testNode(5), sfTest3, nodestate.Flags{}, 0)
  88. explist([]int{4, 6})
  89. ns.SetState(testNode(1), nodestate.Flags{}, sfTest4, 0)
  90. ns.SetState(testNode(2), nodestate.Flags{}, sfTest4, 0)
  91. ns.SetState(testNode(3), nodestate.Flags{}, sfTest4, 0)
  92. ns.SetState(testNode(2), sfTest3, nodestate.Flags{}, 0)
  93. ns.SetState(testNode(2), nodestate.Flags{}, sfTest3, 0)
  94. explist([]int{1, 3, 2})
  95. ns.Stop()
  96. }