node: common: Fix racy test

Use a channel instead of concurrently accessing the same variable from 2
different goroutines.
This commit is contained in:
Chirantan Ekbote 2022-09-20 19:27:04 +09:00 committed by Chirantan Ekbote
parent 7eddddc17c
commit 9989730d8f
1 changed files with 8 additions and 6 deletions

View File

@ -14,7 +14,7 @@ func TestObsvReqSendLimitEnforced(t *testing.T) {
obsvReqSendC := make(chan *gossipv1.ObservationRequest, ObsvReqChannelSize)
// If the channel overflows, the write hangs, so use a go routine with a timeout.
done := false
done := make(chan struct{})
go func() {
// Filling the queue up should work.
for count := 1; count <= ObsvReqChannelSize; count++ {
@ -32,11 +32,13 @@ func TestObsvReqSendLimitEnforced(t *testing.T) {
err := PostObservationRequest(obsvReqSendC, req)
assert.ErrorIs(t, err, ErrChanFull)
done = true
done <- struct{}{}
}()
time.Sleep(time.Second)
// Make sure we didn't hang.
assert.Equal(t, true, done)
timeout := time.NewTimer(time.Second)
select {
case <-timeout.C:
assert.Fail(t, "timed out")
case <-done:
}
}