Skip to content

Commit

Permalink
fix: make broadcast batching test deterministic (#2130)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgorenflo authored Apr 3, 2024
1 parent a6c9ca1 commit af6c153
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions sdk-utils/broadcast/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,15 @@ func TestInBatches(t *testing.T) {
assert.Equal(t, msgs[0].String(), response.Events[0].Type)
}()
<-broadcastCalled
ensureGoRoutineIsCalled := &sync.WaitGroup{}
ensureGoRoutineIsCalled.Add(20)
// accumulate msgs in the backlog
for i := 0; i < 9; i++ {
for i := 0; i < 20; i++ {
wg.Add(1)
go func() {
defer wg.Done()
msgs := randomMsgs(1)
ensureGoRoutineIsCalled.Done()
response, err := batched.Broadcast(context.Background(), msgs...)
assert.NoError(t, err)
// make sure the expected msg is part of the response
Expand All @@ -322,9 +325,13 @@ func TestInBatches(t *testing.T) {
func(event abci.Event) bool { return msgs[0].String() == event.Type }))
}()
}
// with this waitgroup we ensure that all goroutines that broadcast a message are at least started.
// Without it, the unblockBroadcast channel might be closed before the main thread relinquishes control and
// the messages would trickle in one after the other without batching
ensureGoRoutineIsCalled.Wait()
close(unblockBroadcast)
wg.Wait()
assert.Less(t, len(broadcaster.BroadcastCalls()), 10)
assert.Less(t, len(broadcaster.BroadcastCalls()), 20)
}),
).Run(t)
}
Expand Down

0 comments on commit af6c153

Please sign in to comment.