-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clock interface, real and fake clocks (#33)
- Loading branch information
Showing
12 changed files
with
157 additions
and
100 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
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,53 @@ | ||
package gcache | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type Clock interface { | ||
Now() time.Time | ||
} | ||
|
||
type RealClock struct{} | ||
|
||
func NewRealClock() Clock { | ||
return RealClock{} | ||
} | ||
|
||
func (rc RealClock) Now() time.Time { | ||
t := time.Now() | ||
return t | ||
} | ||
|
||
type FakeClock interface { | ||
Clock | ||
|
||
Advance(d time.Duration) | ||
} | ||
|
||
func NewFakeClock() FakeClock { | ||
return &fakeclock{ | ||
// Taken from github.com/jonboulle/clockwork: use a fixture that does not fulfill Time.IsZero() | ||
now: time.Date(1984, time.April, 4, 0, 0, 0, 0, time.UTC), | ||
} | ||
} | ||
|
||
type fakeclock struct { | ||
now time.Time | ||
|
||
mutex sync.RWMutex | ||
} | ||
|
||
func (fc *fakeclock) Now() time.Time { | ||
fc.mutex.RLock() | ||
defer fc.mutex.RUnlock() | ||
t := fc.now | ||
return t | ||
} | ||
|
||
func (fc *fakeclock) Advance(d time.Duration) { | ||
fc.mutex.Lock() | ||
defer fc.mutex.Unlock() | ||
fc.now = fc.now.Add(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
Oops, something went wrong.