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