-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock_test.go
More file actions
152 lines (130 loc) · 3.72 KB
/
clock_test.go
File metadata and controls
152 lines (130 loc) · 3.72 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package statekit
import (
"sync/atomic"
"testing"
"time"
)
func TestSystemClock_Fires(t *testing.T) {
t.Parallel()
var fired atomic.Bool
done := make(chan struct{})
timer := SystemClock().AfterFunc(5*time.Millisecond, func() {
fired.Store(true)
close(done)
})
t.Cleanup(func() { timer.Stop() })
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("timer did not fire within 1s")
}
if !fired.Load() {
t.Error("expected timer to fire")
}
}
func TestSystemClock_StopBeforeFire(t *testing.T) {
t.Parallel()
var fired atomic.Bool
timer := SystemClock().AfterFunc(time.Hour, func() { fired.Store(true) })
if !timer.Stop() {
t.Error("Stop should report true for not-yet-fired timer")
}
if fired.Load() {
t.Error("did not expect callback to fire")
}
}
func TestFakeClock_AdvanceFires(t *testing.T) {
t.Parallel()
clk := NewFakeClock(time.Unix(1_000_000, 0))
var fired atomic.Int32
clk.AfterFunc(100*time.Millisecond, func() { fired.Add(1) })
if fired.Load() != 0 {
t.Error("timer fired before Advance")
}
if got := clk.PendingTimers(); got != 1 {
t.Errorf("PendingTimers = %d, want 1", got)
}
clk.Advance(99 * time.Millisecond)
if fired.Load() != 0 {
t.Error("timer fired before deadline")
}
clk.Advance(2 * time.Millisecond)
if fired.Load() != 1 {
t.Errorf("expected 1 fire after Advance, got %d", fired.Load())
}
if got := clk.PendingTimers(); got != 0 {
t.Errorf("PendingTimers after fire = %d, want 0", got)
}
}
func TestFakeClock_OrderOfFires(t *testing.T) {
t.Parallel()
clk := NewFakeClock(time.Unix(0, 0))
var order []int
clk.AfterFunc(50*time.Millisecond, func() { order = append(order, 50) })
clk.AfterFunc(10*time.Millisecond, func() { order = append(order, 10) })
clk.AfterFunc(30*time.Millisecond, func() { order = append(order, 30) })
clk.Advance(100 * time.Millisecond)
want := []int{10, 30, 50}
if len(order) != len(want) {
t.Fatalf("order = %v, want %v", order, want)
}
for i := range want {
if order[i] != want[i] {
t.Errorf("order[%d] = %d, want %d", i, order[i], want[i])
}
}
}
func TestFakeClock_Stop(t *testing.T) {
t.Parallel()
clk := NewFakeClock(time.Unix(0, 0))
var fired atomic.Bool
timer := clk.AfterFunc(10*time.Millisecond, func() { fired.Store(true) })
if !timer.Stop() {
t.Error("first Stop should report true")
}
if timer.Stop() {
t.Error("second Stop should report false (already stopped)")
}
clk.Advance(time.Hour)
if fired.Load() {
t.Error("stopped timer should not fire")
}
if got := clk.PendingTimers(); got != 0 {
t.Errorf("PendingTimers after stop = %d, want 0", got)
}
}
// TestInterpreter_WithFakeClock_DelayedTransition verifies that an
// interpreter using FakeClock fires its delayed transition only when
// the clock is advanced past the deadline.
func TestInterpreter_WithFakeClock_DelayedTransition(t *testing.T) {
t.Parallel()
machine, err := NewMachine[struct{}]("delay-test").
WithInitial("loading").
State("loading").
After(50 * time.Millisecond).Target("ready").
Done().
State("ready").Final().
Done().
Build()
if err != nil {
t.Fatalf("build: %v", err)
}
clk := NewFakeClock(time.Unix(0, 0))
interp := NewInterpreter(machine, WithClock[struct{}](clk))
defer func() { _ = interp.Close() }()
interp.Start()
if got := string(interp.State().Value); got != "loading" {
t.Fatalf("after Start: state = %q, want loading", got)
}
clk.Advance(49 * time.Millisecond)
if got := string(interp.State().Value); got != "loading" {
t.Errorf("at 49ms: state = %q, want still loading", got)
}
clk.Advance(2 * time.Millisecond)
if got := string(interp.State().Value); got != "ready" {
t.Errorf("at 51ms: state = %q, want ready", got)
}
if !interp.Done() {
t.Error("expected Done after timer fired")
}
}