-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify mocking of now and make sequential now func thread safe
- Loading branch information
1 parent
890f2d4
commit 08a0433
Showing
6 changed files
with
37 additions
and
52 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,51 @@ | ||
package dates | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Now returns the time now.. according to the current source of now | ||
// Now returns the time now.. according to the current now function which can be switched out for testing. | ||
func Now() time.Time { | ||
return currentNowSource.Now() | ||
return currentNow() | ||
} | ||
|
||
// Since returns the time elapsed since t | ||
func Since(t time.Time) time.Duration { | ||
return Now().Sub(t) | ||
} | ||
|
||
// NowSource is something that can provide a now result | ||
type NowSource interface { | ||
Now() time.Time | ||
} | ||
|
||
// defaultNowSource returns now as the current system time | ||
type defaultNowSource struct{} | ||
|
||
func (s defaultNowSource) Now() time.Time { | ||
return time.Now() | ||
} | ||
// NowFunc is a function that can provide a now time | ||
type NowFunc func() time.Time | ||
|
||
// DefaultNowSource is the default time source | ||
var DefaultNowSource NowSource = defaultNowSource{} | ||
var currentNowSource = DefaultNowSource | ||
var currentNow = time.Now | ||
|
||
// SetNowSource sets the time source used by Now() | ||
func SetNowSource(source NowSource) { | ||
currentNowSource = source | ||
// SetNowFunc sets the current now function | ||
func SetNowFunc(source NowFunc) { | ||
currentNow = source | ||
} | ||
|
||
// a source which returns a fixed time | ||
type fixedNowSource struct { | ||
now time.Time | ||
// NewFixedNow creates a new fixed now func | ||
func NewFixedNow(now time.Time) NowFunc { | ||
return func() time.Time { return now } | ||
} | ||
|
||
func (s *fixedNowSource) Now() time.Time { | ||
return s.now | ||
type sequentialNow struct { | ||
start time.Time | ||
step time.Duration | ||
mutex sync.Mutex | ||
} | ||
|
||
// NewFixedNowSource creates a new fixed time now source | ||
func NewFixedNowSource(now time.Time) NowSource { | ||
return &fixedNowSource{now: now} | ||
} | ||
|
||
// a now source which returns a sequence of times 1 second after each other | ||
type sequentialNowSource struct { | ||
current time.Time | ||
} | ||
func (s *sequentialNow) now() time.Time { | ||
s.mutex.Lock() | ||
defer s.mutex.Unlock() | ||
|
||
func (s *sequentialNowSource) Now() time.Time { | ||
now := s.current | ||
s.current = s.current.Add(time.Second * 1) | ||
now := s.start | ||
s.start = s.start.Add(s.step) | ||
return now | ||
} | ||
|
||
// NewSequentialNowSource creates a new sequential time source | ||
func NewSequentialNowSource(start time.Time) NowSource { | ||
return &sequentialNowSource{current: start} | ||
// NewSequentialNow creates a new sequential time func | ||
func NewSequentialNow(start time.Time, step time.Duration) NowFunc { | ||
return (&sequentialNow{start: start, step: step}).now | ||
} |
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
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