Skip to content

Commit

Permalink
fix: use select for timeout wait
Browse files Browse the repository at this point in the history
  • Loading branch information
romanzac committed Mar 23, 2024
1 parent ff01699 commit 95f3ff0
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions waku/v2/node/wakunode2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func TestDecoupledStoreFromRelay(t *testing.T) {
}

func TestStaticShardingMultipleTopics(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

// Node1 with Relay
Expand All @@ -343,10 +343,6 @@ func TestStaticShardingMultipleTopics(t *testing.T) {
pubSubTopic2Str := pubSubTopic2.String()
contentTopic2 := "/test/3/my-app/sharded"

log := utils.Logger()

log.Info("pubsub1", zap.String("pb1", pubSubTopic1Str))

r := wakuNode1.Relay()

subs1, err := r.Subscribe(ctx, protocol.NewContentFilter(pubSubTopic1Str, contentTopic1))
Expand Down Expand Up @@ -376,7 +372,7 @@ func TestStaticShardingMultipleTopics(t *testing.T) {
_, err = r.Publish(ctx, msg, relay.WithPubSubTopic(pubSubTopic1Str))
require.NoError(t, err)

time.Sleep(500 * time.Millisecond)
time.Sleep(100 * time.Millisecond)

var wg sync.WaitGroup
wg.Add(1)
Expand All @@ -396,14 +392,20 @@ func TestStaticShardingMultipleTopics(t *testing.T) {
_, err = r.Publish(ctx, msg2, relay.WithPubSubTopic("/waku/2/rs/0/321"))
require.NoError(t, err)

time.Sleep(500 * time.Millisecond)
time.Sleep(100 * time.Millisecond)

// No message could be retrieved
wg.Add(1)
go func() {
defer wg.Done()
_, ok := <-subs1[0].Ch
require.False(t, ok)
select {
case _, ok := <-subs1[0].Ch:
require.False(t, ok, "should not retrieve message")
case <-time.After(1 * time.Second):
// All good
case <-ctx.Done():
require.Fail(t, "test exceeded allocated time")
}
}()

wg.Wait()
Expand All @@ -414,14 +416,20 @@ func TestStaticShardingMultipleTopics(t *testing.T) {
_, err = r.Publish(ctx, msg3, relay.WithPubSubTopic(pubSubTopic1Str))
require.NoError(t, err)

time.Sleep(500 * time.Millisecond)
time.Sleep(100 * time.Millisecond)

// No message could be retrieved
wg.Add(1)
go func() {
defer wg.Done()
_, ok := <-subs1[0].Ch
require.False(t, ok)
select {
case _, ok := <-subs1[0].Ch:
require.False(t, ok, "should not retrieve message")
case <-time.After(1 * time.Second):
// All good
case <-ctx.Done():
require.Fail(t, "test exceeded allocated time")
}
}()

wg.Wait()
Expand Down

0 comments on commit 95f3ff0

Please sign in to comment.