This repository has been archived by the owner on Sep 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,013 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package clockdog | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
"time" | ||
|
||
clock "github.com/nhatthm/go-clock" | ||
) | ||
|
||
// ErrClockIsNotSet indicates that the clock must be set by either Clock.Set() or Clock.Freeze() before adding some | ||
// time.Duration into it. | ||
var ErrClockIsNotSet = errors.New("clock is not set") | ||
|
||
var _ clock.Clock = (*Clock)(nil) | ||
|
||
// Clock is a clock.Clock. | ||
type Clock struct { | ||
timestamp *time.Time | ||
mu sync.Mutex | ||
} | ||
|
||
// Now returns a fixed timestamp or time.Now(). | ||
func (c *Clock) Now() time.Time { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
if c.timestamp == nil { | ||
return time.Now() | ||
} | ||
|
||
return *c.timestamp | ||
} | ||
|
||
// Set fixes the clock at a time. | ||
func (c *Clock) Set(t time.Time) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.timestamp = timestamp(t) | ||
} | ||
|
||
// Add adds time to the clock. | ||
func (c *Clock) Add(d time.Duration) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
if c.timestamp == nil { | ||
return ErrClockIsNotSet | ||
} | ||
|
||
c.timestamp = timestamp(c.timestamp.Add(d)) | ||
|
||
return nil | ||
} | ||
|
||
// AddDate adds date to the clock. | ||
func (c *Clock) AddDate(years, months, days int) error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
if c.timestamp == nil { | ||
return ErrClockIsNotSet | ||
} | ||
|
||
c.timestamp = timestamp(c.timestamp.AddDate(years, months, days)) | ||
|
||
return nil | ||
} | ||
|
||
// Freeze freezes the clock. | ||
func (c *Clock) Freeze() { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.timestamp = timestamp(time.Now()) | ||
} | ||
|
||
// Unfreeze unfreezes the clock. | ||
func (c *Clock) Unfreeze() { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
|
||
c.timestamp = nil | ||
} | ||
|
||
// Clock provides clock.Clock. | ||
func (c *Clock) Clock() clock.Clock { | ||
return c | ||
} | ||
|
||
// New initiates a new Clock. | ||
func New() *Clock { | ||
return &Clock{} | ||
} | ||
|
||
func timestamp(t time.Time) *time.Time { | ||
return &t | ||
} |
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,74 @@ | ||
package clockdog_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/nhatthm/clockdog" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestClock(t *testing.T) { | ||
t.Parallel() | ||
|
||
c := clockdog.New() | ||
|
||
now := time.Now() | ||
|
||
assert.True(t, now.Before(c.Now())) | ||
|
||
// Errors while adding time to a live clock. | ||
assert.Equal(t, clockdog.ErrClockIsNotSet, c.Add(time.Hour)) | ||
assert.Equal(t, clockdog.ErrClockIsNotSet, c.AddDate(0, 0, 1)) | ||
|
||
// Freeze the clock. | ||
c.Freeze() | ||
|
||
ts := c.Now() | ||
|
||
<-time.After(50 * time.Millisecond) | ||
|
||
assert.Equal(t, ts, c.Now()) | ||
|
||
// Set to another time. | ||
ts = time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC) | ||
|
||
c.Set(ts) | ||
|
||
<-time.After(50 * time.Millisecond) | ||
|
||
assert.Equal(t, ts, c.Now()) | ||
|
||
// Change the time. | ||
ts = ts.Add(2 * time.Hour) | ||
err := c.Add(2 * time.Hour) | ||
assert.NoError(t, err) | ||
|
||
<-time.After(50 * time.Millisecond) | ||
|
||
assert.Equal(t, ts, c.Now()) | ||
|
||
// Change the date. | ||
ts = ts.AddDate(2, 1, 3) | ||
err = c.AddDate(2, 1, 3) | ||
assert.NoError(t, err) | ||
|
||
<-time.After(50 * time.Millisecond) | ||
|
||
assert.Equal(t, ts, c.Now()) | ||
|
||
// Unfreeze the clock. | ||
c.Unfreeze() | ||
|
||
now = time.Now() | ||
|
||
assert.True(t, now.Before(c.Now())) | ||
} | ||
|
||
func TestClock_Clock(t *testing.T) { | ||
t.Parallel() | ||
|
||
c := clockdog.New() | ||
|
||
assert.Equal(t, c, c.Clock()) | ||
} |
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,2 +1,2 @@ | ||
// Package main | ||
package main | ||
// Package clockdog provides a clock for tests with cucumber/godog. | ||
package clockdog |
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,14 @@ | ||
Feature: Test with background | ||
|
||
Background: | ||
Given now is "2020-04-05T06:07:08Z" | ||
|
||
Scenario: Add days | ||
Given someone adds 2 days to the clock | ||
|
||
Then the time is "2020-04-07T06:07:08Z" | ||
|
||
Scenario: Add months | ||
Given someone adds 1 month to the clock | ||
|
||
Then the time is "2020-05-05T06:07:08Z" |
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,2 @@ | ||
// Package bootstrap provides integration tests. | ||
package bootstrap |
Oops, something went wrong.