Skip to content

Commit

Permalink
clock pkg optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Mar 5, 2024
1 parent db0b18a commit 3b49124
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clock/Clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ func Sleep(d time.Duration) {
}

func After(d time.Duration) <-chan time.Time {
startedAt := internal.GetTime()
ch := make(chan time.Time)
if d == 0 {
close(ch)
return ch
}
startedAt := internal.GetTime()
go func() {
wait:
for {
Expand Down
8 changes: 8 additions & 0 deletions clock/Clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ func TestAfter(t *testing.T) {
t.Fatal("clock.After should have finished already its work after travel that went more forward as the duration")
}
}) //Ω, testcase.Flaky(5*time.Second))

s.Test("no matter what, when the wait time is zero, clock.After returns instantly", func(t *testcase.T) {
timecop.SetSpeed(t, 0.001)
timecop.Travel(t, time.Second, timecop.Freeze())
assert.Within(t, time.Millisecond, func(ctx context.Context) {
<-clock.After(0)
}, "expected to finish instantly")
})
}

func Test_testTimeWithMinusDuration(t *testing.T) {
Expand Down

0 comments on commit 3b49124

Please sign in to comment.