-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a Clock interface that is injected everywhere time.Now an…
…d time.After are used.
- Loading branch information
1 parent
f4d0f2e
commit 1e421cb
Showing
13 changed files
with
1,177 additions
and
1,069 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,41 @@ | ||
package clock | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type Clock interface { | ||
Now() time.Time | ||
After(d time.Duration) <-chan time.Time | ||
} | ||
|
||
func NewClock() Clock { | ||
// If we're in a test environment, return the mock clock. | ||
if strings.Contains(os.Args[0], ".test") { | ||
return MockClock{} | ||
} | ||
return RealClock{} | ||
} | ||
|
||
type RealClock struct{} | ||
|
||
func (RealClock) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
||
func (RealClock) After(d time.Duration) <-chan time.Time { | ||
return time.After(d) | ||
} | ||
|
||
type MockClock struct{} | ||
|
||
func (MockClock) Now() time.Time { | ||
t, _ := time.Parse(time.RFC3339, "2036-01-02T15:04:05+07:00") | ||
return t | ||
} | ||
|
||
func (MockClock) After(d time.Duration) <-chan time.Time { | ||
return time.After(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.