github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/les/vflux/client/queueiterator_test.go (about)

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