queueiterator_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 testNode(i int) *enode.Node {
  26. return enode.SignNull(new(enr.Record), testNodeID(i))
  27. }
  28. func TestQueueIteratorFIFO(t *testing.T) {
  29. testQueueIterator(t, true)
  30. }
  31. func TestQueueIteratorLIFO(t *testing.T) {
  32. testQueueIterator(t, false)
  33. }
  34. func testQueueIterator(t *testing.T, fifo bool) {
  35. ns := nodestate.NewNodeStateMachine(nil, nil, &mclock.Simulated{}, testSetup)
  36. qi := NewQueueIterator(ns, sfTest2, sfTest3.Or(sfTest4), fifo, nil)
  37. ns.Start()
  38. for i := 1; i <= iterTestNodeCount; i++ {
  39. ns.SetState(testNode(i), sfTest1, nodestate.Flags{}, 0)
  40. }
  41. next := func() int {
  42. ch := make(chan struct{})
  43. go func() {
  44. qi.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 := qi.Node()
  53. ns.SetState(node, sfTest4, nodestate.Flags{}, 0)
  54. return testNodeIndex(node.ID())
  55. }
  56. exp := func(i int) {
  57. n := next()
  58. if n != i {
  59. t.Errorf("Wrong item returned by iterator (expected %d, got %d)", i, n)
  60. }
  61. }
  62. explist := func(list []int) {
  63. for i := range list {
  64. if fifo {
  65. exp(list[i])
  66. } else {
  67. exp(list[len(list)-1-i])
  68. }
  69. }
  70. }
  71. ns.SetState(testNode(1), sfTest2, nodestate.Flags{}, 0)
  72. ns.SetState(testNode(2), sfTest2, nodestate.Flags{}, 0)
  73. ns.SetState(testNode(3), sfTest2, nodestate.Flags{}, 0)
  74. explist([]int{1, 2, 3})
  75. ns.SetState(testNode(4), sfTest2, nodestate.Flags{}, 0)
  76. ns.SetState(testNode(5), sfTest2, nodestate.Flags{}, 0)
  77. ns.SetState(testNode(6), sfTest2, nodestate.Flags{}, 0)
  78. ns.SetState(testNode(5), sfTest3, nodestate.Flags{}, 0)
  79. explist([]int{4, 6})
  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. ns.SetState(testNode(2), sfTest3, nodestate.Flags{}, 0)
  84. ns.SetState(testNode(2), nodestate.Flags{}, sfTest3, 0)
  85. explist([]int{1, 3, 2})
  86. ns.Stop()
  87. }