Skip to content

Commit

Permalink
parallelize the fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
yhassanzadeh13 committed Jul 12, 2023
1 parent 7c21e79 commit 93e31f8
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions network/p2p/test/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/rand"
crand "math/rand"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -684,21 +685,31 @@ func EnsureNoPubsubMessageExchange(t *testing.T, ctx context.Context, from []p2p
// let subscriptions propagate
time.Sleep(1 * time.Second)

wg := &sync.WaitGroup{}
for _, node := range from {
node := node // capture range variable
for i := 0; i < count; i++ {
// creates a unique message to be published by the node.
msg := messageFactory()
channel, ok := channels.ChannelFromTopic(topic)
require.True(t, ok)
data := p2pfixtures.MustEncodeEvent(t, msg, channel)

// ensure the message is NOT received by any of the nodes.
require.NoError(t, node.Publish(ctx, topic, data))
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
p2pfixtures.SubsMustNeverReceiveAnyMessage(t, ctx, subs)
cancel()
wg.Add(1)
go func() {
// creates a unique message to be published by the node.
msg := messageFactory()
channel, ok := channels.ChannelFromTopic(topic)
require.True(t, ok)
data := p2pfixtures.MustEncodeEvent(t, msg, channel)

// ensure the message is NOT received by any of the nodes.
require.NoError(t, node.Publish(ctx, topic, data))
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
p2pfixtures.SubsMustNeverReceiveAnyMessage(t, ctx, subs)
cancel()
wg.Done()
}()
}
}

// we wait for 5 seconds at most for the messages to be exchanged, hence we wait for a total of 6 seconds here to ensure
// that the goroutines are done in a timely manner.
unittest.RequireReturnsBefore(t, wg.Wait, 6*time.Second, "timed out waiting for messages to be exchanged")
}

// EnsureNoPubsubExchangeBetweenGroups ensures that no pubsub message is exchanged between the given groups of nodes.
Expand Down

0 comments on commit 93e31f8

Please sign in to comment.