-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context methods to Clock interface
This allows for using timeout/deadline functionality built in to context.Context with a custom clock implementation. Module Go version bumped to 1.19 due to use of atomic.Bool
- Loading branch information
1 parent
e868797
commit a6b96d4
Showing
16 changed files
with
749 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build go1.21 | ||
|
||
package clocks | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func (c defaultClock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
return context.WithDeadlineCause(ctx, t, cause) | ||
} | ||
|
||
func (c defaultClock) ContextWithTimeoutCause(ctx context.Context, d time.Duration, cause error) (context.Context, context.CancelFunc) { | ||
return context.WithTimeoutCause(ctx, d, cause) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//go:build go1.21 | ||
|
||
package clocks | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDefaultClockContext121(t *testing.T) { | ||
c := DefaultClock() | ||
|
||
t.Run("ContextWithDeadlineCause", func(t *testing.T) { | ||
base := c.Now() | ||
|
||
ctx, cancel := c.ContextWithDeadlineCause(context.Background(), base.Add(time.Millisecond), errors.New("test")) | ||
t.Cleanup(cancel) | ||
|
||
if v := c.SleepUntil(ctx, base.Add(time.Second)); v { | ||
t.Errorf("unexpected return value: %t; expected false", v) | ||
} else { | ||
if ctx.Err() != context.DeadlineExceeded { | ||
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.DeadlineExceeded) | ||
} | ||
if context.Cause(ctx) == nil || context.Cause(ctx).Error() != "test" { | ||
t.Errorf("unexpected cause: %v; expected %v", context.Cause(ctx), "test") | ||
} | ||
} | ||
}) | ||
|
||
t.Run("ContextWithDeadlineCauseCanceled", func(t *testing.T) { | ||
base := c.Now() | ||
|
||
ctx, cancel := c.ContextWithDeadlineCause(context.Background(), base.Add(500*time.Millisecond), errors.New("test")) | ||
|
||
cancel() | ||
|
||
if v := c.SleepUntil(ctx, base.Add(time.Second)); v { | ||
t.Errorf("unexpected return value: %t; expected false", v) | ||
} else { | ||
if ctx.Err() != context.Canceled { | ||
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.Canceled) | ||
} | ||
if context.Cause(ctx) == nil || context.Cause(ctx) != context.Canceled { | ||
t.Errorf("unexpected cause: %v; expected %v", context.Cause(ctx), context.Canceled) | ||
} | ||
} | ||
}) | ||
|
||
t.Run("ContextWithTimeoutCause", func(t *testing.T) { | ||
ctx, cancel := c.ContextWithTimeoutCause(context.Background(), time.Millisecond, errors.New("test")) | ||
t.Cleanup(cancel) | ||
|
||
if v := c.SleepFor(ctx, time.Second); v { | ||
t.Errorf("unexpected return value: %t; expected false", v) | ||
} else { | ||
if ctx.Err() != context.DeadlineExceeded { | ||
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.DeadlineExceeded) | ||
} | ||
if context.Cause(ctx) == nil || context.Cause(ctx).Error() != "test" { | ||
t.Errorf("unexpected cause: %v; expected %v", context.Cause(ctx), "test") | ||
} | ||
} | ||
}) | ||
|
||
t.Run("ContextWithTimeoutCauseCanceled", func(t *testing.T) { | ||
ctx, cancel := c.ContextWithTimeoutCause(context.Background(), 500*time.Millisecond, errors.New("test")) | ||
|
||
cancel() | ||
|
||
if v := c.SleepFor(ctx, time.Second); v { | ||
t.Errorf("unexpected return value: %t; expected false", v) | ||
} else { | ||
if ctx.Err() != context.Canceled { | ||
t.Errorf("unexpected error: %v; expected %v", ctx.Err(), context.Canceled) | ||
} | ||
if context.Cause(ctx) == nil || context.Cause(ctx) != context.Canceled { | ||
t.Errorf("unexpected cause: %v; expected %v", context.Cause(ctx), context.Canceled) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
//go:build !go1.21 | ||
|
||
package clocks | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
func (c defaultClock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
return context.WithDeadline(ctx, t) | ||
} | ||
|
||
func (c defaultClock) ContextWithTimeoutCause(ctx context.Context, d time.Duration, cause error) (context.Context, context.CancelFunc) { | ||
return context.WithTimeout(ctx, d) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//go:build go1.21 | ||
|
||
package fake | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// ContextWithDeadlineCause behaves like context.WithDeadlineCause, but it | ||
// uses the clock to determine the when the deadline has expired. Cause is | ||
// ignored in Go 1.20 and earlier. | ||
func (f *Clock) ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc) { | ||
cctx, cancelCause := context.WithCancelCause(ctx) | ||
dctx := &deadlineContext{ | ||
Context: cctx, | ||
deadline: t, | ||
} | ||
dur := f.Until(t) | ||
if dur <= 0 { | ||
dctx.timedOut.Store(true) | ||
cancelCause(cause) | ||
return dctx, func() {} | ||
} | ||
stop := f.AfterFunc(dur, func() { | ||
if cctx.Err() == nil { | ||
dctx.timedOut.Store(true) | ||
} | ||
cancelCause(cause) | ||
}) | ||
cancel := func() { | ||
cancelCause(context.Canceled) | ||
stop.Stop() | ||
} | ||
return dctx, cancel | ||
} |
Oops, something went wrong.