fillset_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "testing"
  20. "time"
  21. "github.com/ethereum/go-ethereum/common/mclock"
  22. "github.com/ethereum/go-ethereum/p2p/enode"
  23. "github.com/ethereum/go-ethereum/p2p/enr"
  24. "github.com/ethereum/go-ethereum/p2p/nodestate"
  25. )
  26. type testIter struct {
  27. waitCh chan struct{}
  28. nodeCh chan *enode.Node
  29. node *enode.Node
  30. }
  31. func (i *testIter) Next() bool {
  32. i.waitCh <- struct{}{}
  33. i.node = <-i.nodeCh
  34. return i.node != nil
  35. }
  36. func (i *testIter) Node() *enode.Node {
  37. return i.node
  38. }
  39. func (i *testIter) Close() {}
  40. func (i *testIter) push() {
  41. var id enode.ID
  42. rand.Read(id[:])
  43. i.nodeCh <- enode.SignNull(new(enr.Record), id)
  44. }
  45. func (i *testIter) waiting(timeout time.Duration) bool {
  46. select {
  47. case <-i.waitCh:
  48. return true
  49. case <-time.After(timeout):
  50. return false
  51. }
  52. }
  53. func TestFillSet(t *testing.T) {
  54. ns := nodestate.NewNodeStateMachine(nil, nil, &mclock.Simulated{}, testSetup)
  55. iter := &testIter{
  56. waitCh: make(chan struct{}),
  57. nodeCh: make(chan *enode.Node),
  58. }
  59. fs := NewFillSet(ns, iter, sfTest1)
  60. ns.Start()
  61. expWaiting := func(i int, push bool) {
  62. for ; i > 0; i-- {
  63. if !iter.waiting(time.Second * 10) {
  64. t.Fatalf("FillSet not waiting for new nodes")
  65. }
  66. if push {
  67. iter.push()
  68. }
  69. }
  70. }
  71. expNotWaiting := func() {
  72. if iter.waiting(time.Millisecond * 100) {
  73. t.Fatalf("FillSet unexpectedly waiting for new nodes")
  74. }
  75. }
  76. expNotWaiting()
  77. fs.SetTarget(3)
  78. expWaiting(3, true)
  79. expNotWaiting()
  80. fs.SetTarget(100)
  81. expWaiting(2, true)
  82. expWaiting(1, false)
  83. // lower the target before the previous one has been filled up
  84. fs.SetTarget(0)
  85. iter.push()
  86. expNotWaiting()
  87. fs.SetTarget(10)
  88. expWaiting(4, true)
  89. expNotWaiting()
  90. // remove all previosly set flags
  91. ns.ForEach(sfTest1, nodestate.Flags{}, func(node *enode.Node, state nodestate.Flags) {
  92. ns.SetState(node, nodestate.Flags{}, sfTest1, 0)
  93. })
  94. // now expect FillSet to fill the set up again with 10 new nodes
  95. expWaiting(10, true)
  96. expNotWaiting()
  97. fs.Close()
  98. ns.Stop()
  99. }