-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add client side timestamp with microsecond monotonicity
- Loading branch information
Showing
6 changed files
with
193 additions
and
35 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
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,44 @@ | ||
package yarpc | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Clock is a thin wrapper around time.Now() to allow mocking | ||
type Clock interface { | ||
Now() time.Time | ||
} | ||
|
||
type mockClock struct { | ||
timesToRead []time.Time | ||
m sync.Mutex | ||
} | ||
|
||
type realClock struct{} | ||
|
||
func (r *realClock) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
||
func (m *mockClock) Now() (returnTime time.Time) { | ||
m.m.Lock() | ||
defer m.m.Unlock() | ||
|
||
if len(m.timesToRead) == 0 { | ||
panic("Too many calls to Now()") | ||
} | ||
returnTime = m.timesToRead[0] | ||
m.timesToRead = m.timesToRead[1:] | ||
return | ||
} | ||
|
||
// NewRealClock produces a new real clock | ||
func NewRealClock() Clock { | ||
return &realClock{} | ||
} | ||
|
||
// NewMockClock produces a mock clock with a series of time points to retrieve | ||
func NewMockClock(timesToRead []time.Time) Clock { | ||
return &mockClock{timesToRead: timesToRead} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.