Created
January 2, 2025 12:42
-
-
Save pboothe/8aede4f00ac342c68adbac5164de80bc to your computer and use it in GitHub Desktop.
A clock for injecting deterministic times during unit tests and real times in production.
This file contains hidden or 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
package timex | |
import "time" | |
type Clock interface { | |
Now() time.Time | |
Sleep(d time.Duration) | |
} | |
type SystemClock struct{} | |
func (s *SystemClock) Now() time.Time { return time.Now() } | |
func (s *SystemClock) Sleep(d time.Duration) { time.Sleep(d) } | |
type StaticClock struct { | |
Time time.Time | |
} | |
func (s *StaticClock) Now() time.Time { return s.Time } | |
func (s *StaticClock) Sleep(d time.Duration) { s.Time = s.Time.Add(d) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I wrote this code and I assign it to the public domain. Use it in good health!