Skip to content

Commit a6f8167

Browse files
committed
feat(tester): add WithInitialTime option
This option sets the initial mocked time for a workflow tester to enable testing date- and/or time-of-day-dependent behavior.
1 parent 1d17cf8 commit a6f8167

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

tester/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type options struct {
1313
Logger *slog.Logger
1414
Converter converter.Converter
1515
Propagators []workflow.ContextPropagator
16+
InitialTime time.Time
1617
}
1718

1819
type WorkflowTesterOption func(*options)
@@ -40,3 +41,9 @@ func WithTestTimeout(timeout time.Duration) WorkflowTesterOption {
4041
o.TestTimeout = timeout
4142
}
4243
}
44+
45+
func WithInitialTime(t time.Time) WorkflowTesterOption {
46+
return func(o *options) {
47+
o.InitialTime = t
48+
}
49+
}

tester/tester.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,6 @@ func NewWorkflowTester[TResult any](workflow workflow.Workflow, opts ...Workflow
187187
panic(fmt.Sprintf("workflow return type does not match: %s", err))
188188
}
189189

190-
// Start with the current wall-c time
191-
c := clock.NewMock()
192-
c.Set(time.Now())
193-
194-
wfi := core.NewWorkflowInstance(uuid.NewString(), uuid.NewString())
195-
registry := registry.New()
196-
197190
options := &options{
198191
TestTimeout: time.Second * 10,
199192
Logger: slog.Default(),
@@ -204,6 +197,17 @@ func NewWorkflowTester[TResult any](workflow workflow.Workflow, opts ...Workflow
204197
o(options)
205198
}
206199

200+
// Start with the current wall-c time unless InitialTime is set
201+
c := clock.NewMock()
202+
if options.InitialTime.IsZero() {
203+
c.Set(time.Now())
204+
} else {
205+
c.Set(options.InitialTime)
206+
}
207+
208+
wfi := core.NewWorkflowInstance(uuid.NewString(), uuid.NewString())
209+
registry := registry.New()
210+
207211
tracer := noop.NewTracerProvider().Tracer("workflow-tester")
208212

209213
wt := &workflowTester[TResult]{

tester/tester_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,18 @@ func waitForSignal(ctx workflow.Context) (int, error) {
269269

270270
return 42, nil
271271
}
272+
273+
func Test_WithInitialTime(t *testing.T) {
274+
getTimeWorkflow := func(ctx workflow.Context) (time.Time, error) {
275+
return workflow.Now(ctx), nil
276+
}
277+
278+
initialTime := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
279+
tester := NewWorkflowTester[time.Time](getTimeWorkflow, WithInitialTime(initialTime))
280+
281+
tester.Execute(context.Background())
282+
283+
require.True(t, tester.WorkflowFinished())
284+
r, _ := tester.WorkflowResult()
285+
require.Equal(t, initialTime, r)
286+
}

0 commit comments

Comments
 (0)