-
Notifications
You must be signed in to change notification settings - Fork 71
Add AfterTimeout to Deadline #396
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ package deadline | |
|
|
||
| import ( | ||
| "context" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
|
|
@@ -93,6 +94,88 @@ func TestDeadline(t *testing.T) { | |
| expectedCalls := []byte{0} | ||
| assert.Equal(t, expectedCalls, calls, "Wrong order of deadline signal") | ||
| }) | ||
|
|
||
| t.Run("DeadlineAfterTimeoutExceedFuture", func(t *testing.T) { | ||
| now := time.Now() | ||
|
|
||
| d := New() | ||
| var trigger atomic.Int32 | ||
| d.AfterTimeout(func() { | ||
| trigger.Add(1) | ||
| }) | ||
| d.Set(now.Add(10 * time.Millisecond)) | ||
| <-time.After(20 * time.Millisecond) | ||
| d.Set(now.Add(10 * time.Millisecond)) | ||
| <-time.After(20 * time.Millisecond) | ||
|
Comment on lines
+106
to
+109
Contributor
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. 10/20ms margins will probably flake in CI. also double check that this is taking the expected path...
Author
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. Ideally we should be able to control a fake clock but I have simply copied from above tests.
Contributor
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. usually 100-200ms is enough slack for oversubscribed gh test runners. if the numbers you chose are found elsewhere in the package i retract my suggestion though
Member
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. We should start using synctest for time tests like this, it should be beind a 1.25 go build flag. |
||
|
|
||
| assert.Equal(t, int32(1), trigger.Load(), "Function triggered wrong number of time") | ||
| }) | ||
|
|
||
| t.Run("DeadlineAfterTimeoutExceedPast", func(t *testing.T) { | ||
| now := time.Now() | ||
|
|
||
| d := New() | ||
| var trigger atomic.Int32 | ||
| d.AfterTimeout(func() { | ||
| trigger.Add(1) | ||
| }) | ||
| d.Set(now.Add(10 * time.Millisecond)) | ||
| d.Set(now.Add(-10 * time.Millisecond)) | ||
| <-time.After(20 * time.Millisecond) | ||
|
|
||
| assert.Equal(t, int32(1), trigger.Load(), "Function triggered wrong number of time") | ||
| }) | ||
|
|
||
| t.Run("DeadlineAfterTimeoutAlreadyExceeded", func(t *testing.T) { | ||
| now := time.Now() | ||
|
|
||
| d := New() | ||
| var trigger atomic.Int32 | ||
| d.Set(now.Add(10 * time.Millisecond)) | ||
| d.Set(now.Add(-10 * time.Millisecond)) | ||
| <-time.After(20 * time.Millisecond) | ||
| d.AfterTimeout(func() { | ||
| trigger.Add(1) | ||
| }) | ||
| <-time.After(20 * time.Millisecond) | ||
|
|
||
| assert.Equal(t, int32(1), trigger.Load(), "Function triggered wrong number of time") | ||
| }) | ||
|
|
||
| t.Run("DeadlineAfterTimeoutDetach", func(t *testing.T) { | ||
| now := time.Now() | ||
|
|
||
| d := New() | ||
| var trigger atomic.Int32 | ||
| detach := d.AfterTimeout(func() { | ||
| trigger.Add(1) | ||
| }) | ||
| ret := detach() | ||
| d.Set(now.Add(10 * time.Millisecond)) | ||
| <-time.After(20 * time.Millisecond) | ||
|
|
||
| assert.Equal(t, int32(0), trigger.Load(), "Function triggered wrong number of time") | ||
| assert.Equal(t, true, ret, "The detach function returned wrong value") | ||
| }) | ||
|
|
||
| t.Run("DeadlineAfterTimeoutMultiCallbacks", func(t *testing.T) { | ||
| now := time.Now() | ||
|
|
||
| d := New() | ||
| var trigger atomic.Int32 | ||
| d.AfterTimeout(func() { | ||
| trigger.Add(1) | ||
| }) | ||
| d.AfterTimeout(func() { | ||
| trigger.Add(1) | ||
| }) | ||
| assert.Equal(t, int32(0), trigger.Load(), "Function triggered wrong number of time") | ||
|
|
||
| d.Set(now.Add(10 * time.Millisecond)) | ||
| <-time.After(20 * time.Millisecond) | ||
|
|
||
| assert.Equal(t, int32(2), trigger.Load(), "Function triggered wrong number of time") | ||
| }) | ||
| } | ||
|
|
||
| func sendOnDone(ctx context.Context, done <-chan struct{}, dest chan byte, val byte) { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.