1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
package missinggo
import (
"testing"
"time"
"github.com/bradfitz/iter"
"github.com/stretchr/testify/assert"
)
func TestTimerDrain(t *testing.T) {
tr := time.NewTimer(0)
<-tr.C
select {
case <-tr.C:
assert.Fail(t, "shouldn't have received again on the the expired timer")
default:
}
tr.Reset(1)
select {
case <-tr.C:
assert.Fail(t, "received too soon")
default:
}
time.Sleep(1)
<-tr.C
// Stop() should return false, as it just fired.
assert.False(t, tr.Stop())
tr.Reset(0)
// Check we receive again after a Reset().
<-tr.C
}
func TestTimerDoesNotFireAfterStop(t *testing.T) {
t.Skip("the standard library implementation is broken")
fail := make(chan struct{})
done := make(chan struct{})
defer close(done)
for range iter.N(1000) {
tr := time.NewTimer(0)
tr.Stop()
// There may or may not be a value in the channel now. But definitely
// one should not be added after we receive it.
select {
case <-tr.C:
default:
}
// Now set the timer to trigger in hour. It definitely shouldn't be
// receivable now for an hour.
tr.Reset(time.Hour)
go func() {
select {
case <-tr.C:
// As soon as the channel receives, notify failure.
fail <- struct{}{}
case <-done:
}
}()
}
select {
case <-fail:
t.FailNow()
case <-time.After(100 * time.Millisecond):
}
}
|