jonboulle / clockwork

a fake clock for golang
Apache License 2.0
631 stars 57 forks source link

Fake clock does not handle negative durations to After() correctly #21

Closed pcman312 closed 3 years ago

pcman312 commented 5 years ago

Description

fakeClock does not handle negative durations passed to After() correctly.

This looks very similar to https://github.com/jonboulle/clockwork/issues/19

Steps to reproduce

func TestNegativeTimer(t *testing.T) {
    fakeClock := clockwork.NewFakeClock()
    timer := time.NewTimer(2 * time.Second)
    defer timer.Stop()
    select {
    case <-fakeClock.After(-1 * time.Second):
    // Success
    case <-timer.C:
        t.Fatalf("Timed out!")
    }
}

Output:

$ go test -v -run TestNegativeTimer
=== RUN   TestNegativeTimer
--- FAIL: TestNegativeTimer (2.00s)
    negativetimer_test.go:18: Timed out!
FAIL
exit status 1
lyonssp commented 4 years ago

Just opened a PR for a fix https://github.com/jonboulle/clockwork/pull/24