Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

Commit

Permalink
Add context WithDeadline/WithTimeout methods to Clock interface
Browse files Browse the repository at this point in the history
  • Loading branch information
aviddiviner authored and morigs committed Nov 12, 2021
1 parent 5178ebe commit 83715db
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion clock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package clock

import (
"context"
"sort"
"sync"
"time"
Expand All @@ -24,6 +25,8 @@ type Clock interface {
Tick(d time.Duration) <-chan time.Time
Ticker(d time.Duration) *Ticker
Timer(d time.Duration) *Timer
WithDeadline(parent context.Context, d time.Time) (context.Context, context.CancelFunc)
WithTimeout(parent context.Context, t time.Duration) (context.Context, context.CancelFunc)
}

// New returns an instance of a real-time clock.
Expand Down Expand Up @@ -60,7 +63,15 @@ func (c *clock) Timer(d time.Duration) *Timer {
return &Timer{C: t.C, timer: t}
}

// Mock represents a mock clock that only moves forward programmatically.
func (c *clock) WithDeadline(parent context.Context, d time.Time) (context.Context, context.CancelFunc) {
return context.WithDeadline(parent, d)
}

func (c *clock) WithTimeout(parent context.Context, t time.Duration) (context.Context, context.CancelFunc) {
return context.WithTimeout(parent, t)
}

// Mock represents a mock clock that only moves forward programmically.
// It can be preferable to a real-time clock when testing time-based functionality.
type Mock struct {
mu sync.Mutex
Expand Down

0 comments on commit 83715db

Please sign in to comment.