Skip to content

Commit 4973be8

Browse files
authored
Merge pull request #371 from decibelcooper/feat/tester-with-initial-time
feat(tester): add WithInitialTime option
2 parents cd41c30 + 1a3fd84 commit 4973be8

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
@@ -193,13 +193,6 @@ func NewWorkflowTester[TResult any](workflow workflow.Workflow, opts ...Workflow
193193
panic(fmt.Sprintf("workflow return type does not match: %s", err))
194194
}
195195

196-
// Start with the current wall-c time
197-
c := clock.NewMock()
198-
c.Set(time.Now())
199-
200-
wfi := core.NewWorkflowInstance(uuid.NewString(), uuid.NewString())
201-
registry := registry.New()
202-
203196
options := &options{
204197
TestTimeout: time.Second * 10,
205198
Logger: slog.Default(),
@@ -210,6 +203,17 @@ func NewWorkflowTester[TResult any](workflow workflow.Workflow, opts ...Workflow
210203
o(options)
211204
}
212205

206+
// Start with the current wall-c time unless InitialTime is set
207+
c := clock.NewMock()
208+
if options.InitialTime.IsZero() {
209+
c.Set(time.Now())
210+
} else {
211+
c.Set(options.InitialTime)
212+
}
213+
214+
wfi := core.NewWorkflowInstance(uuid.NewString(), uuid.NewString())
215+
registry := registry.New()
216+
213217
tracer := noop.NewTracerProvider().Tracer("workflow-tester")
214218

215219
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)