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