Skip to content
This repository was archived by the owner on Feb 1, 2023. It is now read-only.

Commit 462c94a

Browse files
committed
fix(network): make test more reliable
Account for times that routing table is not setup when provide is called
1 parent 2f865f7 commit 462c94a

File tree

2 files changed

+54
-38
lines changed

2 files changed

+54
-38
lines changed

benchmarks_test.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,27 @@ func BenchmarkDupsManyNodesRealWorldNetwork(b *testing.B) {
157157

158158
func BenchmarkDupsManyNodesRealWorldNetworkWithRealDHT(b *testing.B) {
159159
benchmarkLog = nil
160+
benchmarkSeed, err := strconv.ParseInt(os.Getenv("BENCHMARK_SEED"), 10, 64)
161+
var randomGen *rand.Rand = nil
162+
if err == nil {
163+
randomGen = rand.New(rand.NewSource(benchmarkSeed))
164+
}
165+
160166
fastNetworkDelayGenerator := tn.InternetLatencyDelayGenerator(
161167
mediumSpeed-fastSpeed, slowSpeed-fastSpeed,
162-
0.0, 0.0, distribution, nil)
168+
0.0, 0.0, distribution, randomGen)
163169
fastNetworkDelay := delay.Delay(fastSpeed, fastNetworkDelayGenerator)
164-
fastBandwidthGenerator := tn.VariableRateLimitGenerator(fastBandwidth, fastBandwidthDeviation, nil)
170+
fastBandwidthGenerator := tn.VariableRateLimitGenerator(fastBandwidth, fastBandwidthDeviation, randomGen)
165171
averageNetworkDelayGenerator := tn.InternetLatencyDelayGenerator(
166172
mediumSpeed-fastSpeed, slowSpeed-fastSpeed,
167-
0.3, 0.3, distribution, nil)
173+
0.3, 0.3, distribution, randomGen)
168174
averageNetworkDelay := delay.Delay(fastSpeed, averageNetworkDelayGenerator)
169-
averageBandwidthGenerator := tn.VariableRateLimitGenerator(mediumBandwidth, mediumBandwidthDeviation, nil)
175+
averageBandwidthGenerator := tn.VariableRateLimitGenerator(mediumBandwidth, mediumBandwidthDeviation, randomGen)
170176
slowNetworkDelayGenerator := tn.InternetLatencyDelayGenerator(
171177
mediumSpeed-fastSpeed, superSlowSpeed-fastSpeed,
172-
0.3, 0.3, distribution, nil)
178+
0.3, 0.3, distribution, randomGen)
173179
slowNetworkDelay := delay.Delay(fastSpeed, slowNetworkDelayGenerator)
174-
slowBandwidthGenerator := tn.VariableRateLimitGenerator(slowBandwidth, slowBandwidthDeviation, nil)
180+
slowBandwidthGenerator := tn.VariableRateLimitGenerator(slowBandwidth, slowBandwidthDeviation, randomGen)
175181

176182
b.Run("10Nodes-RandomRandom-BigBatch-FastNetwork", func(b *testing.B) {
177183
subtestDistributeAndFetchWithRealDHT(b, 20, 10, fastNetworkDelay, fastBandwidthGenerator, stdBlockSize, starPattern, randomRandom, batchFetchAll)
@@ -219,40 +225,42 @@ func subtestDistributeAndFetchRateLimited(b *testing.B, numnodes, numblks int, d
219225
}
220226

221227
func subtestDistributeAndFetchWithRealDHT(b *testing.B, numnodes, numblks int, d delay.D, rateLimitGenerator tn.RateLimitGenerator, blockSize int64, tf topologyFunc, df distFunc, ff fetchFunc) {
222-
start := time.Now()
223-
ctx := context.Background()
224-
mn := mocknet.New(ctx)
225-
routing := tn.NewDHTServer()
226-
net, err := tn.StreamNet(ctx, mn, routing)
227-
if err != nil {
228-
b.Fatal("Unable to initialize mocknet")
229-
}
228+
for i := 0; i < b.N; i++ {
229+
start := time.Now()
230+
ctx := context.Background()
231+
mn := mocknet.New(ctx)
232+
routing := tn.NewDHTServer()
233+
net, err := tn.StreamNet(ctx, mn, routing)
234+
if err != nil {
235+
b.Fatal("Unable to initialize mocknet")
236+
}
230237

231-
ig := testinstance.NewTestInstanceGenerator(net)
232-
defer ig.Close()
238+
ig := testinstance.NewTestInstanceGenerator(net)
239+
defer ig.Close()
233240

234-
var instances []testinstance.Instance
235-
for j := 0; j < numnodes; j++ {
236-
inst := ig.Next()
237-
instances = append(instances, inst)
238-
}
239-
mn.LinkAll()
240-
for i, inst := range instances {
241-
// rate limit and delate connections
242-
for j := i + 1; j < len(instances); j++ {
243-
oinst := instances[j]
244-
links := mn.LinksBetweenPeers(inst.Peer, oinst.Peer)
245-
for _, link := range links {
246-
link.SetOptions(mocknet.LinkOptions{Latency: d.NextWaitTime(), Bandwidth: rateLimitGenerator.NextRateLimit()})
241+
var instances []testinstance.Instance
242+
for j := 0; j < numnodes; j++ {
243+
inst := ig.Next()
244+
instances = append(instances, inst)
245+
}
246+
mn.LinkAll()
247+
for i, inst := range instances {
248+
// rate limit and delate connections
249+
for j := i + 1; j < len(instances); j++ {
250+
oinst := instances[j]
251+
links := mn.LinksBetweenPeers(inst.Peer, oinst.Peer)
252+
for _, link := range links {
253+
link.SetOptions(mocknet.LinkOptions{Latency: d.NextWaitTime(), Bandwidth: rateLimitGenerator.NextRateLimit()})
254+
}
247255
}
248256
}
249-
}
250257

251-
tf(b, instances)
258+
tf(b, instances)
252259

253-
blocks := testutil.GenerateBlocksOfSize(numblks, blockSize)
260+
blocks := testutil.GenerateBlocksOfSize(numblks, blockSize)
254261

255-
runDistribution(b, instances, blocks, df, ff, start)
262+
runDistribution(b, instances, blocks, df, ff, start)
263+
}
256264
}
257265

258266
func runDistribution(b *testing.B, instances []testinstance.Instance, blocks []blocks.Block, df distFunc, ff fetchFunc, start time.Time) {

network/ipfs_impl_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
blocksutil "github.com/ipfs/go-ipfs-blocksutil"
1111

1212
"github.com/libp2p/go-libp2p-core/peer"
13-
"github.com/libp2p/go-libp2p-testing/net"
13+
tnet "github.com/libp2p/go-libp2p-testing/net"
1414
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
1515
)
1616

@@ -196,7 +196,6 @@ func TestRealProviding(t *testing.T) {
196196
t.Fatal("did not connect peer")
197197
case <-r1.connectionEvent:
198198
}
199-
bsnet2.ConnectTo(ctx, p1.ID())
200199
select {
201200
case <-ctx.Done():
202201
t.Fatal("did not connect peer")
@@ -208,7 +207,6 @@ func TestRealProviding(t *testing.T) {
208207
t.Fatal("did not connect peer")
209208
case <-r2.connectionEvent:
210209
}
211-
bsnet3.ConnectTo(ctx, p2.ID())
212210
select {
213211
case <-ctx.Done():
214212
t.Fatal("did not connect peer")
@@ -229,9 +227,19 @@ func TestRealProviding(t *testing.T) {
229227
blockGenerator := blocksutil.NewBlockGenerator()
230228
block1 := blockGenerator.Next()
231229
// provide on node 3
232-
err = bsnet3.Provide(ctx, block1.Cid())
230+
// DHT can setup routing table in seperate thread,
231+
// meaning it's not finished even bitswap peers are connected
232+
// so attempt a few times with delay
233+
provideLoop:
234+
for i := 0; i < 5; i++ {
235+
err = bsnet3.Provide(ctx, block1.Cid())
236+
if err == nil {
237+
break provideLoop
238+
}
239+
time.Sleep(10 * time.Millisecond)
240+
}
233241
if err != nil {
234-
t.Fatal("Unable to provide node")
242+
t.Fatalf("Unable to provide node: %s", err)
235243
}
236244
incomingProvders := bsnet1.FindProvidersAsync(ctx, block1.Cid(), 1)
237245
select {

0 commit comments

Comments
 (0)