|
| 1 | +// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package p2p |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "go.uber.org/mock/gomock" |
| 13 | + |
| 14 | + "github.com/ava-labs/avalanchego/ids" |
| 15 | + "github.com/ava-labs/avalanchego/snow/engine/common" |
| 16 | + "github.com/ava-labs/avalanchego/utils/logging" |
| 17 | + "github.com/ava-labs/avalanchego/utils/math" |
| 18 | + "github.com/ava-labs/avalanchego/utils/set" |
| 19 | +) |
| 20 | + |
| 21 | +// Sample should always return up to [limit] peers, and less if fewer than |
| 22 | +// [limit] peers are available. |
| 23 | +func TestPeersSample(t *testing.T) { |
| 24 | + nodeID1 := ids.GenerateTestNodeID() |
| 25 | + nodeID2 := ids.GenerateTestNodeID() |
| 26 | + nodeID3 := ids.GenerateTestNodeID() |
| 27 | + |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + connected set.Set[ids.NodeID] |
| 31 | + disconnected set.Set[ids.NodeID] |
| 32 | + limit int |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "no peers", |
| 36 | + limit: 1, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "one peer connected", |
| 40 | + connected: set.Of(nodeID1), |
| 41 | + limit: 1, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "multiple peers connected", |
| 45 | + connected: set.Of(nodeID1, nodeID2, nodeID3), |
| 46 | + limit: 1, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "peer connects and disconnects - 1", |
| 50 | + connected: set.Of(nodeID1), |
| 51 | + disconnected: set.Of(nodeID1), |
| 52 | + limit: 1, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "peer connects and disconnects - 2", |
| 56 | + connected: set.Of(nodeID1, nodeID2), |
| 57 | + disconnected: set.Of(nodeID2), |
| 58 | + limit: 1, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "peer connects and disconnects - 2", |
| 62 | + connected: set.Of(nodeID1, nodeID2, nodeID3), |
| 63 | + disconnected: set.Of(nodeID1, nodeID2), |
| 64 | + limit: 1, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "less than limit peers", |
| 68 | + connected: set.Of(nodeID1, nodeID2, nodeID3), |
| 69 | + limit: 4, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "limit peers", |
| 73 | + connected: set.Of(nodeID1, nodeID2, nodeID3), |
| 74 | + limit: 3, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "more than limit peers", |
| 78 | + connected: set.Of(nodeID1, nodeID2, nodeID3), |
| 79 | + limit: 2, |
| 80 | + }, |
| 81 | + } |
| 82 | + |
| 83 | + for _, tt := range tests { |
| 84 | + t.Run(tt.name, func(t *testing.T) { |
| 85 | + require := require.New(t) |
| 86 | + peers := &Peers{} |
| 87 | + |
| 88 | + for connected := range tt.connected { |
| 89 | + require.NoError(peers.Connected(context.Background(), connected, nil)) |
| 90 | + } |
| 91 | + |
| 92 | + for disconnected := range tt.disconnected { |
| 93 | + require.NoError(peers.Disconnected(context.Background(), disconnected)) |
| 94 | + } |
| 95 | + |
| 96 | + sampleable := set.Set[ids.NodeID]{} |
| 97 | + sampleable.Union(tt.connected) |
| 98 | + sampleable.Difference(tt.disconnected) |
| 99 | + |
| 100 | + sampled := peers.Sample(context.Background(), tt.limit) |
| 101 | + require.Len(sampled, math.Min(tt.limit, len(sampleable))) |
| 102 | + require.Subset(sampleable, sampled) |
| 103 | + }) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func TestAppRequestAnyNodeSelection(t *testing.T) { |
| 108 | + tests := []struct { |
| 109 | + name string |
| 110 | + peers []ids.NodeID |
| 111 | + expected error |
| 112 | + }{ |
| 113 | + { |
| 114 | + name: "no peers", |
| 115 | + expected: ErrNoPeers, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "has peers", |
| 119 | + peers: []ids.NodeID{ids.GenerateTestNodeID()}, |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tt := range tests { |
| 124 | + t.Run(tt.name, func(t *testing.T) { |
| 125 | + require := require.New(t) |
| 126 | + ctrl := gomock.NewController(t) |
| 127 | + mockAppSender := common.NewMockSender(ctrl) |
| 128 | + |
| 129 | + expectedCalls := 0 |
| 130 | + if tt.expected == nil { |
| 131 | + expectedCalls = 1 |
| 132 | + } |
| 133 | + mockAppSender.EXPECT().SendAppRequest(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(expectedCalls) |
| 134 | + |
| 135 | + r := NewRouter(logging.NoLog{}, mockAppSender) |
| 136 | + peers := &Peers{} |
| 137 | + for _, peer := range tt.peers { |
| 138 | + require.NoError(peers.Connected(context.Background(), peer, nil)) |
| 139 | + } |
| 140 | + |
| 141 | + client, err := r.RegisterAppProtocol(1, nil, peers) |
| 142 | + require.NoError(err) |
| 143 | + |
| 144 | + err = client.AppRequestAny(context.Background(), []byte("foobar"), nil) |
| 145 | + require.ErrorIs(err, tt.expected) |
| 146 | + }) |
| 147 | + } |
| 148 | +} |
0 commit comments