-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a WithSynchronousAfterFunc option #68
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,9 @@ package clockwork | |
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
@@ -199,3 +201,33 @@ func TestFakeClockRace(t *testing.T) { | |
go func() { fc.NewTimer(d) }() | ||
go func() { fc.Sleep(d) }() | ||
} | ||
|
||
func TestFakeClockSynchronousAfterFunc(t *testing.T) { | ||
t.Parallel() | ||
fc := NewFakeClock(WithSynchronousAfterFunc(true)) | ||
var called atomic.Bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's avoid atomic unless as a last resort. Here you can pretty readily use a channel:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually realized that since this is called synchronously, there's no need for further synchronization, so I just used a bool. I ran the same test with the |
||
fc.AfterFunc(time.Second, func() { | ||
called.Store(true) | ||
}) | ||
if called.Load() { | ||
t.Fatal("AfterFunc called before Advance") | ||
} | ||
fc.Advance(time.Second) | ||
if !called.Load() { | ||
t.Fatal("AfterFunc not called after Advance") | ||
} | ||
} | ||
|
||
func ExampleWithSynchronousAfterFunc() { | ||
fc := NewFakeClock(WithSynchronousAfterFunc(true)) | ||
fc.AfterFunc(time.Second, func() { | ||
fmt.Println("AfterFunc called") | ||
}) | ||
fmt.Println("Calling Advance") | ||
fc.Advance(time.Second) | ||
fmt.Println("Advance returned") | ||
// Output: | ||
// Calling Advance | ||
// AfterFunc called | ||
// Advance returned | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On a first-pass, I don't think the argument is needed. The name of the option communicates the intent: if present, the AfterFunc is sync, if absent, AfterFunc is async (the default).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed, thanks