node: common: Fix racy test
Use a channel instead of concurrently accessing the same variable from 2 different goroutines.
This commit is contained in:
parent
7eddddc17c
commit
9989730d8f
|
@ -14,7 +14,7 @@ func TestObsvReqSendLimitEnforced(t *testing.T) {
|
||||||
obsvReqSendC := make(chan *gossipv1.ObservationRequest, ObsvReqChannelSize)
|
obsvReqSendC := make(chan *gossipv1.ObservationRequest, ObsvReqChannelSize)
|
||||||
|
|
||||||
// If the channel overflows, the write hangs, so use a go routine with a timeout.
|
// If the channel overflows, the write hangs, so use a go routine with a timeout.
|
||||||
done := false
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
// Filling the queue up should work.
|
// Filling the queue up should work.
|
||||||
for count := 1; count <= ObsvReqChannelSize; count++ {
|
for count := 1; count <= ObsvReqChannelSize; count++ {
|
||||||
|
@ -32,11 +32,13 @@ func TestObsvReqSendLimitEnforced(t *testing.T) {
|
||||||
err := PostObservationRequest(obsvReqSendC, req)
|
err := PostObservationRequest(obsvReqSendC, req)
|
||||||
assert.ErrorIs(t, err, ErrChanFull)
|
assert.ErrorIs(t, err, ErrChanFull)
|
||||||
|
|
||||||
done = true
|
done <- struct{}{}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
time.Sleep(time.Second)
|
timeout := time.NewTimer(time.Second)
|
||||||
|
select {
|
||||||
// Make sure we didn't hang.
|
case <-timeout.C:
|
||||||
assert.Equal(t, true, done)
|
assert.Fail(t, "timed out")
|
||||||
|
case <-done:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue