jonboulle / clockwork

a fake clock for golang
Apache License 2.0
656 stars 58 forks source link

BlockUntil blocks forever if there are too many blockers #35

Closed nicks closed 2 years ago

nicks commented 2 years ago

Consider the following code:

func TestFakeClock(t *testing.T) {
    c := clockwork.NewFakeClock()

    go func(){
        c.Sleep(time.Second)
    }()
    go func(){
        c.Sleep(time.Second)
    }()

    c.BlockUntil(1)
}

Most of the time this code will pass! But non-deterministically (if the goroutines are scheduled at just the right time), the BlockUntil will block forever.

The problem is that numExpected != numActual (2 != 1), so the BlockUntil call will block forever. I think it should be changed so that it blocks until numExpected <= numActual. Would you accept a PR to change this?

I could also imagine other fixes, like BlockUntil panic-ing if there are more sleepers than numExpected, rather than blocking forever.

sagikazarmark commented 2 years ago

Hey @nicks !

Thanks for reporting this issue.

If you could provide a patch with appropriate test coverage, that would be awesome!

nicks commented 2 years ago

thanks @sagikazarmark ! PR opened - https://github.com/jonboulle/clockwork/pull/36