-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_test.go
More file actions
71 lines (59 loc) · 1.59 KB
/
time_test.go
File metadata and controls
71 lines (59 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package deterministic_test
import (
"testing"
"time"
"github.com/selesy/deterministic"
)
func TestNowFunc_ReturnsFunction(t *testing.T) {
f := deterministic.NowFunc()
if f == nil {
t.Fatal("NowFunc returned nil")
}
}
func TestNowFunc_InitialTime(t *testing.T) {
f := deterministic.NowFunc()
result := f()
expected, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
if !result.Equal(expected) {
t.Errorf("got %v, want %v", result, expected)
}
}
func TestNowFunc_IncrementsOneSecond(t *testing.T) {
f := deterministic.NowFunc()
first := f()
second := f()
expected := first.Add(time.Second)
if !second.Equal(expected) {
t.Errorf("got %v, want %v", second, expected)
}
}
func TestNowFunc_ConsecutiveCalls(t *testing.T) {
f := deterministic.NowFunc()
baseTime, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
for i := 0; i < 10; i++ {
result := f()
expected := baseTime.Add(time.Duration(i) * time.Second)
if !result.Equal(expected) {
t.Errorf("call %d: got %v, want %v", i, result, expected)
}
}
}
func TestNowFunc_MultipleInstances(t *testing.T) {
f1 := deterministic.NowFunc()
f2 := deterministic.NowFunc()
t1 := f1()
t2 := f2()
if !t1.Equal(t2) {
t.Errorf("different instances should start at same time: got %v, want %v", t1, t2)
}
// Each instance maintains its own state independently
t1_second := f1()
t2_second := f2()
expected := t1.Add(time.Second)
if !t1_second.Equal(expected) {
t.Errorf("f1 second: got %v, want %v", t1_second, expected)
}
if !t2_second.Equal(expected) {
t.Errorf("f2 second: got %v, want %v", t2_second, expected)
}
}