Skip to content

Commit a87776a

Browse files
holisticodezelig
authored andcommitted
swarm/network/stream: Fix flaky tests in GetSubscriptionsRPC test (#19227)
* swarm/network/stream: fixed timing issues * swarm/network/stream: only count first iteration of subscriptions * swarm/network/stream/: fix linter errors
1 parent 72b21db commit a87776a

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

‎swarm/network/stream/streamer_test.go‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,12 @@ func TestGetSubscriptionsRPC(t *testing.T) {
12071207

12081208
// we use this subscriptionFunc for this test: just increases count and calls the actual subscription
12091209
subscriptionFunc = func(r *Registry, p *network.Peer, bin uint8, subs map[enode.ID]map[Stream]struct{}) bool {
1210-
expectedMsgCount.inc()
1210+
// syncing starts after syncUpdateDelay and loops after that Duration; we only want to count at the first iteration
1211+
// in the first iteration, subs will be empty (no existing subscriptions), thus we can use this check
1212+
// this avoids flakyness
1213+
if len(subs) == 0 {
1214+
expectedMsgCount.inc()
1215+
}
12111216
doRequestSubscription(r, p, bin, subs)
12121217
return true
12131218
}
@@ -1245,19 +1250,19 @@ func TestGetSubscriptionsRPC(t *testing.T) {
12451250
ctx, cancelSimRun := context.WithTimeout(context.Background(), 3*time.Minute)
12461251
defer cancelSimRun()
12471252

1248-
// upload a snapshot
1249-
err := sim.UploadSnapshot(fmt.Sprintf("testing/snapshot_%d.json", nodeCount))
1250-
if err != nil {
1251-
t.Fatal(err)
1252-
}
1253-
12541253
// setup the filter for SubscribeMsg
12551254
msgs := sim.PeerEvents(
12561255
context.Background(),
1257-
sim.NodeIDs(),
1256+
sim.UpNodeIDs(),
12581257
simulation.NewPeerEventsFilter().ReceivedMessages().Protocol("stream").MsgCode(subscribeMsgCode),
12591258
)
12601259

1260+
// upload a snapshot
1261+
err := sim.UploadSnapshot(fmt.Sprintf("testing/snapshot_%d.json", nodeCount))
1262+
if err != nil {
1263+
t.Fatal(err)
1264+
}
1265+
12611266
// strategy: listen to all SubscribeMsg events; after every event we wait
12621267
// if after `waitDuration` no more messages are being received, we assume the
12631268
// subscription phase has terminated!
@@ -1267,9 +1272,9 @@ func TestGetSubscriptionsRPC(t *testing.T) {
12671272
// any new subscriptions any more
12681273
go func() {
12691274
//for long running sims, waiting 1 sec will not be enough
1270-
waitDuration := time.Duration(nodeCount/16) * time.Second
1275+
waitDuration := 1 * time.Second
12711276
if *longrunning {
1272-
waitDuration = syncUpdateDelay
1277+
waitDuration = 3 * time.Second
12731278
}
12741279
for {
12751280
select {
@@ -1335,8 +1340,10 @@ func TestGetSubscriptionsRPC(t *testing.T) {
13351340
log.Debug("All node streams counted", "realCount", realCount)
13361341
}
13371342
emc := expectedMsgCount.count()
1338-
if realCount != emc {
1339-
return fmt.Errorf("Real subscriptions and expected amount don't match; real: %d, expected: %d", realCount, emc)
1343+
// after a subscription request, internally a live AND a history stream will be subscribed,
1344+
// thus the real count should be half of the actual request subscriptions sent
1345+
if realCount/2 != emc {
1346+
return fmt.Errorf("Real subscriptions and expected amount don't match; real: %d, expected: %d", realCount/2, emc)
13401347
}
13411348
return nil
13421349
})

0 commit comments

Comments
 (0)