-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.advancetime_test.go
More file actions
193 lines (163 loc) · 4.75 KB
/
action.advancetime_test.go
File metadata and controls
193 lines (163 loc) · 4.75 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package testkit_test
import (
"fmt"
"strings"
"testing"
"time"
"github.com/dogmatiq/dogma"
"github.com/dogmatiq/enginekit/config"
. "github.com/dogmatiq/enginekit/enginetest/stubs"
. "github.com/dogmatiq/testkit"
"github.com/dogmatiq/testkit/engine"
"github.com/dogmatiq/testkit/fact"
"github.com/dogmatiq/testkit/internal/testingmock"
"github.com/dogmatiq/testkit/internal/x/xtesting"
)
func TestAdvanceTime(t *testing.T) {
newFixture := func() (*testingmock.T, time.Time, *fact.Buffer, *Test) {
app := &ApplicationStub{
ConfigureFunc: func(c dogma.ApplicationConfigurer) {
c.Identity("<app>", "140ca29b-7a05-4f26-968b-6285255e6d8a")
},
}
tm := &testingmock.T{}
startTime := time.Now()
buf := &fact.Buffer{}
tc := Begin(
tm,
app,
StartTimeAt(startTime),
WithUnsafeOperationOptions(
engine.WithObserver(buf),
),
)
return tm, startTime, buf, tc
}
t.Run("it retains the virtual time between calls", func(t *testing.T) {
_, startTime, buf, tc := newFixture()
tc.Prepare(
AdvanceTime(ByDuration(1*time.Second)),
AdvanceTime(ByDuration(1*time.Second)),
)
xtesting.ExpectContains[fact.Fact](
t,
"expected tick fact",
buf.Facts(),
fact.TickCycleBegun{
EngineTime: startTime.Add(2 * time.Second),
EnabledHandlerTypes: map[config.HandlerType]bool{
config.AggregateHandlerType: true,
config.IntegrationHandlerType: false,
config.ProcessHandlerType: true,
config.ProjectionHandlerType: false,
},
EnabledHandlers: map[string]bool{},
},
)
})
t.Run("it fails the test if time is reversed", func(t *testing.T) {
tm, startTime, _, tc := newFixture()
tm.FailSilently = true
target := startTime.Add(-1 * time.Second)
tc.Prepare(AdvanceTime(ToTime(target)))
if !tm.Failed() {
t.Fatal("expected test to fail")
}
xtesting.ExpectContains(
t,
"expected failure log",
tm.Logs,
fmt.Sprintf("adjusting the clock to %s would reverse time", target.Format(time.RFC3339)),
)
})
t.Run("it panics if the adjustment is nil", func(t *testing.T) {
xtesting.ExpectPanic(
t,
"AdvanceTime(<nil>): adjustment must not be nil",
func() {
AdvanceTime(nil)
},
)
})
t.Run("it captures the location that the action was created", func(t *testing.T) {
act := advanceTime(ByDuration(10 * time.Second))
loc := act.Location()
xtesting.Expect(t, "unexpected function name", loc.Func, "github.com/dogmatiq/testkit_test.advanceTime")
if !strings.HasSuffix(loc.File, "/action.linenumber_test.go") {
t.Fatalf("unexpected file: %s", loc.File)
}
xtesting.Expect(t, "unexpected line", loc.Line, 50)
})
t.Run("passed a ToTime() adjustment", func(t *testing.T) {
targetTime := time.Date(2100, 1, 2, 3, 4, 5, 6, time.UTC)
t.Run("it advances the clock to the provided time", func(t *testing.T) {
_, _, buf, tc := newFixture()
tc.Prepare(AdvanceTime(ToTime(targetTime)))
xtesting.ExpectContains[fact.Fact](
t,
"expected tick fact",
buf.Facts(),
fact.TickCycleBegun{
EngineTime: targetTime,
EnabledHandlerTypes: map[config.HandlerType]bool{
config.AggregateHandlerType: true,
config.IntegrationHandlerType: false,
config.ProcessHandlerType: true,
config.ProjectionHandlerType: false,
},
EnabledHandlers: map[string]bool{},
},
)
})
t.Run("it produces the expected caption", func(t *testing.T) {
tm, _, _, tc := newFixture()
tc.Prepare(AdvanceTime(ToTime(targetTime)))
xtesting.ExpectContains(
t,
"expected caption",
tm.Logs,
"--- advancing time to 2100-01-02T03:04:05Z ---",
)
})
})
t.Run("passed a ByDuration() adjustment", func(t *testing.T) {
t.Run("it advances the clock then performs a tick", func(t *testing.T) {
_, startTime, buf, tc := newFixture()
tc.Prepare(AdvanceTime(ByDuration(3 * time.Second)))
xtesting.ExpectContains[fact.Fact](
t,
"expected tick fact",
buf.Facts(),
fact.TickCycleBegun{
EngineTime: startTime.Add(3 * time.Second),
EnabledHandlerTypes: map[config.HandlerType]bool{
config.AggregateHandlerType: true,
config.IntegrationHandlerType: false,
config.ProcessHandlerType: true,
config.ProjectionHandlerType: false,
},
EnabledHandlers: map[string]bool{},
},
)
})
t.Run("it produces the expected caption", func(t *testing.T) {
tm, _, _, tc := newFixture()
tc.Prepare(AdvanceTime(ByDuration(3 * time.Second)))
xtesting.ExpectContains(
t,
"expected caption",
tm.Logs,
"--- advancing time by 3s ---",
)
})
t.Run("it panics if the duration is negative", func(t *testing.T) {
xtesting.ExpectPanic(
t,
"ByDuration(-1s): duration must not be negative",
func() {
ByDuration(-1 * time.Second)
},
)
})
})
}