Skip to content

Commit 7187dfe

Browse files
committed
fix some network flakiness
1 parent 7c8cc68 commit 7187dfe

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

network/hybridNetwork_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestHybridNetwork_DuplicateConn(t *testing.T) {
3939

4040
cfg := config.GetDefaultLocal()
4141
cfg.EnableP2PHybridMode = true
42+
cfg.DNSBootstrapID = ""
4243
log := logging.TestingLog(t)
4344
const p2pKeyDir = ""
4445

@@ -208,6 +209,7 @@ func TestHybridNetwork_HybridRelayStrategy(t *testing.T) {
208209

209210
cfg := config.GetDefaultLocal()
210211
cfg.EnableP2PHybridMode = true
212+
cfg.DNSBootstrapID = ""
211213
log := logging.TestingLog(t)
212214

213215
genesisInfo := GenesisInfo{genesisID, "net"}

network/mesh_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package network
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"sync/atomic"
7+
"testing"
8+
"time"
9+
10+
"github.com/algorand/go-algorand/config"
11+
"github.com/algorand/go-algorand/logging"
12+
"github.com/algorand/go-algorand/network/limitcaller"
13+
p2piface "github.com/algorand/go-algorand/network/p2p"
14+
"github.com/algorand/go-algorand/protocol"
15+
"github.com/algorand/go-algorand/test/partitiontest"
16+
pubsub "github.com/libp2p/go-libp2p-pubsub"
17+
"github.com/libp2p/go-libp2p/core/network"
18+
"github.com/libp2p/go-libp2p/core/peer"
19+
"github.com/stretchr/testify/require"
20+
)
21+
22+
// mockP2PService implements p2p.Service and counts DialPeersUntilTargetCount invocations.
23+
// It relies on p2p's meshThreadInner's defer of DialPeersUntilTargetCount to detect invocation.
24+
type mockP2PService struct{ dialCount atomic.Int32 }
25+
26+
func (m *mockP2PService) Start() error { return nil }
27+
func (m *mockP2PService) Close() error { return nil }
28+
func (m *mockP2PService) ID() peer.ID { return "" }
29+
func (m *mockP2PService) IDSigner() *p2piface.PeerIDChallengeSigner { return nil }
30+
func (m *mockP2PService) AddrInfo() peer.AddrInfo { return peer.AddrInfo{} }
31+
func (m *mockP2PService) NetworkNotify(network.Notifiee) {}
32+
func (m *mockP2PService) NetworkStopNotify(network.Notifiee) {}
33+
func (m *mockP2PService) DialPeersUntilTargetCount(int) { m.dialCount.Add(1) }
34+
func (m *mockP2PService) ClosePeer(peer.ID) error { return nil }
35+
func (m *mockP2PService) Conns() []network.Conn { return nil }
36+
func (m *mockP2PService) ListPeersForTopic(string) []peer.ID { return nil }
37+
func (m *mockP2PService) Subscribe(string, pubsub.ValidatorEx) (p2piface.SubNextCancellable, error) {
38+
return nil, nil
39+
}
40+
func (m *mockP2PService) Publish(context.Context, string, []byte) error { return nil }
41+
func (m *mockP2PService) GetHTTPClient(*peer.AddrInfo, limitcaller.ConnectionTimeStore, time.Duration) (*http.Client, error) {
42+
return &http.Client{}, nil
43+
}
44+
45+
// TestMesh_HybridRelayP2PInnerCall ensures the wsConnections <= targetConnCount condition
46+
// in the hybridRelayMeshCreator mesh function in order to make sure P2PNetwork.meshThreadInner is invoked
47+
func TestMesh_HybridRelayP2PInnerCall(t *testing.T) {
48+
partitiontest.PartitionTest(t)
49+
t.Parallel()
50+
51+
cfg := config.GetDefaultLocal()
52+
cfg.GossipFanout = 0
53+
cfg.DNSBootstrapID = ""
54+
cfg.EnableP2PHybridMode = true
55+
cfg.PublicAddress = "public-address"
56+
cfg.NetAddress = "127.0.0.1:0"
57+
cfg.P2PHybridNetAddress = "127.0.0.1:0"
58+
59+
log := logging.TestingLog(t)
60+
genesisInfo := GenesisInfo{GenesisID: "test-genesis", NetworkID: protocol.NetworkID("test-network")}
61+
net, err := NewHybridP2PNetwork(log, cfg, "", nil, genesisInfo, &nopeNodeInfo{}, nil)
62+
require.NoError(t, err)
63+
64+
mockSvc := &mockP2PService{}
65+
net.p2pNetwork.service = mockSvc
66+
net.p2pNetwork.relayMessages = false // prevent pubsub startup
67+
68+
err = net.Start()
69+
require.NoError(t, err)
70+
defer net.Stop()
71+
72+
net.RequestConnectOutgoing(false, nil)
73+
require.Eventually(t, func() bool {
74+
// RequestConnectOutgoing queues mesh update request so we have to wait a bit
75+
return mockSvc.dialCount.Load() > 0
76+
}, 3*time.Second, 50*time.Millisecond, "expected DialPeersUntilTargetCount to be called")
77+
}

network/p2pNetwork_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func TestP2PSubmitTX(t *testing.T) {
7272
cfg := config.GetDefaultLocal()
7373
cfg.ForceFetchTransactions = true
7474
cfg.NetAddress = "127.0.0.1:0"
75+
cfg.DNSBootstrapID = ""
7576
log := logging.TestingLog(t)
7677
genesisInfo := GenesisInfo{genesisID, config.Devtestnet}
7778
netA, err := NewP2PNetwork(log, cfg, "", nil, genesisInfo, &nopeNodeInfo{}, nil, nil)
@@ -162,6 +163,7 @@ func TestP2PSubmitTXNoGossip(t *testing.T) {
162163
cfg := config.GetDefaultLocal()
163164
cfg.ForceFetchTransactions = true
164165
cfg.NetAddress = "127.0.0.1:0"
166+
cfg.DNSBootstrapID = ""
165167
log := logging.TestingLog(t)
166168
genesisInfo := GenesisInfo{genesisID, config.Devtestnet}
167169
netA, err := NewP2PNetwork(log, cfg, "", nil, genesisInfo, &nopeNodeInfo{}, nil, nil)
@@ -256,6 +258,7 @@ func TestP2PSubmitWS(t *testing.T) {
256258

257259
cfg := config.GetDefaultLocal()
258260
cfg.NetAddress = "127.0.0.1:0"
261+
cfg.DNSBootstrapID = ""
259262
log := logging.TestingLog(t)
260263
genesisInfo := GenesisInfo{genesisID, config.Devtestnet}
261264
netA, err := NewP2PNetwork(log, cfg, "", nil, genesisInfo, &nopeNodeInfo{}, nil, nil)
@@ -595,6 +598,7 @@ func TestP2PNetworkDHTCapabilities(t *testing.T) {
595598
cfg := config.GetDefaultLocal()
596599
cfg.NetAddress = "127.0.0.1:0"
597600
cfg.EnableDHTProviders = true
601+
cfg.DNSBootstrapID = ""
598602
log := logging.TestingLog(t)
599603
genesisInfo := GenesisInfo{genesisID, config.Devtestnet}
600604

0 commit comments

Comments
 (0)