Merge pull request #36 from moreati/utils-clock_test

utils: Add unit tests for clocks
This commit is contained in:
Stephen Buttolph 2020-03-29 15:57:26 -04:00 committed by GitHub
commit 6c549d1504
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 0 deletions

37
utils/timer/clock_test.go Normal file
View File

@ -0,0 +1,37 @@
package timer
import (
"testing"
"time"
)
func TestClockSet(t *testing.T) {
clock := Clock{}
clock.Set(time.Unix(1000000, 0))
if clock.faked == false {
t.Error("Fake time was set, but .faked flag was not set")
}
if !clock.Time().Equal(time.Unix(1000000, 0)) {
t.Error("Fake time was set, but not returned")
}
}
func TestClockSync(t *testing.T) {
clock := Clock{true, time.Unix(0, 0)}
clock.Sync()
if clock.faked == true {
t.Error("Clock was synced, but .faked flag was set")
}
if clock.Time().Equal(time.Unix(0, 0)) {
t.Error("Clock was synced, but returned a fake time")
}
}
func TestClockUnix(t *testing.T) {
clock := Clock{true, time.Unix(-14159040, 0)}
actual := clock.Unix()
if actual != 0 {
// We are Unix of 1970s, Moon landings are irrelevant
t.Errorf("Expected time prior to Unix epoch to be clamped to 0, got %d", actual)
}
}