jonhoo/go-events

Tests do not check that channels have no blocking senders

Opened this issue · 0 comments

The require.Empty test on a channel checks its length, which for a channel gives the number of buffered elements. If the channel is unbuffered, this always returns 0, as the below example shows

sending := make(chan bool)
ch := make(chan bool)
bufCh := make(chan bool, 1)
go func() {
    sending <- true
    bufCh <- true
    ch <- true
}()
<-sending
println("sending channel length:", len(sending))
println("unbuffered channel length:", len(ch))
println("buffered channel length:", len(bufCh))

// Output:
// sending channel length: 0
// unbuffered channel length: 0
// buffered channel length: 1

This means that the assertion that a channel does not have a blocking sender must be done a different way.