From 52e3d427a1e567f14eb9cfad80f5869bd29e6586 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Sun, 29 Mar 2020 16:53:55 +0100 Subject: [PATCH 1/2] utils: Add unit tests for clocks --- utils/timer/clock_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 utils/timer/clock_test.go diff --git a/utils/timer/clock_test.go b/utils/timer/clock_test.go new file mode 100644 index 0000000..05b18a7 --- /dev/null +++ b/utils/timer/clock_test.go @@ -0,0 +1,37 @@ +package timer + +import ( + "testing" + "time" +) + +func TestClockSet(t *testing.T) { + clock := Clock{false, time.Unix(1000000, 0)} + clock.Set(time.Unix(0, 0)) + if clock.faked == false { + t.Error("Fake time was set, but .faked flag was not set") + } + if !clock.Time().Equal(time.Unix(0, 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) + } +} From 8f54a7c1d86481908a4d83b9c93fe562b363b603 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Sun, 29 Mar 2020 19:20:28 +0100 Subject: [PATCH 2/2] utils: Initialise Clock with go defaults in TestClockSet Changed to be more idiomatic, addresses PR feedback. --- utils/timer/clock_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/utils/timer/clock_test.go b/utils/timer/clock_test.go index 05b18a7..ef8eb67 100644 --- a/utils/timer/clock_test.go +++ b/utils/timer/clock_test.go @@ -6,12 +6,12 @@ import ( ) func TestClockSet(t *testing.T) { - clock := Clock{false, time.Unix(1000000, 0)} - clock.Set(time.Unix(0, 0)) + 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(0, 0)) { + if !clock.Time().Equal(time.Unix(1000000, 0)) { t.Error("Fake time was set, but not returned") } }