testy: make safe for concurrent use.

Signed-off-by: David Anderson <dave@natulte.net>
This commit is contained in:
David Anderson 2020-03-27 03:05:25 -07:00
parent 1fdadf06f1
commit 80261b02ba

View File

@ -4,7 +4,10 @@
package testy
import "time"
import (
"sync"
"time"
)
// Clock is a testing clock that advances every time its Now method is
// called, beginning at Start.
@ -18,11 +21,15 @@ type Clock struct {
Step time.Duration
// Present is the time that the next Now call will receive.
Present time.Time
sync.Mutex
}
// Now returns the virtual clock's current time, and avances it
// according to its step configuration.
func (c *Clock) Now() time.Time {
c.Lock()
defer c.Unlock()
if c.Start.IsZero() {
c.Start = time.Now()
c.Present = c.Start
@ -35,5 +42,7 @@ func (c *Clock) Now() time.Time {
// Reset rewinds the virtual clock to its start time.
func (c *Clock) Reset() {
c.Lock()
defer c.Unlock()
c.Present = c.Start
}