@@ -75,40 +75,6 @@ type testWorkflow struct {
75
75
pendingEvents []* history.Event
76
76
}
77
77
78
- type WorkflowTester [TResult any ] interface {
79
- // Now returns the current time of the simulated clock in the tester
80
- Now () time.Time
81
-
82
- Execute (ctx context.Context , args ... interface {})
83
-
84
- Registry () * registry.Registry
85
-
86
- OnActivity (activity workflow.Activity , args ... interface {}) * mock.Call
87
-
88
- OnActivityByName (name string , activity workflow.Activity , args ... interface {}) * mock.Call
89
-
90
- OnSubWorkflow (workflow workflow.Workflow , args ... interface {}) * mock.Call
91
-
92
- OnSubWorkflowByName (name string , workflow workflow.Workflow , args ... interface {}) * mock.Call
93
-
94
- SignalWorkflow (signalName string , value interface {})
95
-
96
- SignalWorkflowInstance (wfi * core.WorkflowInstance , signalName string , value interface {}) error
97
-
98
- WorkflowFinished () bool
99
-
100
- WorkflowResult () (TResult , error )
101
-
102
- // AssertExpectations asserts any assertions set up for mock activities and sub-workflow
103
- AssertExpectations (t * testing.T )
104
-
105
- // ScheduleCallback schedules the given callback after the given delay in workflow time (not wall clock).
106
- ScheduleCallback (delay time.Duration , callback func ())
107
-
108
- // ListenSubWorkflow registers a handler to be called when a sub-workflow is started.
109
- ListenSubWorkflow (listener func (instance * core.WorkflowInstance , name string ))
110
- }
111
-
112
78
type workflowTester [TResult any ] struct {
113
79
options * options
114
80
@@ -162,8 +128,6 @@ type workflowTester[TResult any] struct {
162
128
propagators []workflow.ContextPropagator
163
129
}
164
130
165
- var _ WorkflowTester [any ] = (* workflowTester [any ])(nil )
166
-
167
131
func NewWorkflowTester [TResult any ](workflow workflow.Workflow , opts ... WorkflowTesterOption ) * workflowTester [TResult ] {
168
132
if err := args.ReturnTypeMatch [TResult ](workflow ); err != nil {
169
133
panic (fmt .Sprintf ("workflow return type does not match: %s" , err ))
@@ -231,14 +195,17 @@ func NewWorkflowTester[TResult any](workflow workflow.Workflow, opts ...Workflow
231
195
return wt
232
196
}
233
197
198
+ // Now returns the current time in the workflow tester's clock
234
199
func (wt * workflowTester [TResult ]) Now () time.Time {
235
200
return wt .clock .Now ()
236
201
}
237
202
203
+ // Registry returns the registry used by the workflow tester.
238
204
func (wt * workflowTester [TResult ]) Registry () * registry.Registry {
239
205
return wt .registry
240
206
}
241
207
208
+ // ScheduleCallback schedules a callback to be called after the given delay.
242
209
func (wt * workflowTester [TResult ]) ScheduleCallback (delay time.Duration , callback func ()) {
243
210
wt .timers = append (wt .timers , & testTimer {
244
211
At : wt .clock .Now ().Add (delay ),
@@ -247,10 +214,13 @@ func (wt *workflowTester[TResult]) ScheduleCallback(delay time.Duration, callbac
247
214
})
248
215
}
249
216
217
+ // ListenSubWorkflow registers a listener that is called whenever a sub-workflow is started. It receives
218
+ // the workflow instance and the name of the sub-workflow.
250
219
func (wt * workflowTester [TResult ]) ListenSubWorkflow (listener func (* core.WorkflowInstance , string )) {
251
220
wt .subWorkflowListener = listener
252
221
}
253
222
223
+ // OnActivityByName registers a mock activity with the given name.
254
224
func (wt * workflowTester [TResult ]) OnActivityByName (name string , activity workflow.Activity , args ... any ) * mock.Call {
255
225
// Register activity so that we can correctly identify its arguments later
256
226
wt .registry .RegisterActivity (activity , registry .WithName (name ))
@@ -259,6 +229,7 @@ func (wt *workflowTester[TResult]) OnActivityByName(name string, activity workfl
259
229
return wt .ma .On (name , args ... )
260
230
}
261
231
232
+ // OnActivity registers a mock activity.
262
233
func (wt * workflowTester [TResult ]) OnActivity (activity workflow.Activity , args ... any ) * mock.Call {
263
234
// Register activity so that we can correctly identify its arguments later
264
235
wt .registry .RegisterActivity (activity )
@@ -268,6 +239,7 @@ func (wt *workflowTester[TResult]) OnActivity(activity workflow.Activity, args .
268
239
return wt .ma .On (name , args ... )
269
240
}
270
241
242
+ // OnSubWorkflowByName registers a mock sub-workflow with the given name.
271
243
func (wt * workflowTester [TResult ]) OnSubWorkflowByName (name string , workflow workflow.Workflow , args ... any ) * mock.Call {
272
244
// Register workflow so that we can correctly identify its arguments later
273
245
wt .registry .RegisterWorkflow (workflow , registry .WithName (name ))
@@ -276,6 +248,7 @@ func (wt *workflowTester[TResult]) OnSubWorkflowByName(name string, workflow wor
276
248
return wt .mw .On (name , args ... )
277
249
}
278
250
251
+ // OnSubWorkflow registers a mock sub-workflow.
279
252
func (wt * workflowTester [TResult ]) OnSubWorkflow (workflow workflow.Workflow , args ... any ) * mock.Call {
280
253
// Register workflow so that we can correctly identify its arguments later
281
254
wt .registry .RegisterWorkflow (workflow )
@@ -285,6 +258,7 @@ func (wt *workflowTester[TResult]) OnSubWorkflow(workflow workflow.Workflow, arg
285
258
return wt .mw .On (name , args ... )
286
259
}
287
260
261
+ // Execute executes the workflow under test with the given arguments.
288
262
func (wt * workflowTester [TResult ]) Execute (ctx context.Context , args ... any ) {
289
263
for _ , propagator := range wt .propagators {
290
264
if err := propagator .Inject (ctx , wt .wfm ); err != nil {
@@ -508,11 +482,13 @@ func (wt *workflowTester[TResult]) sendEvent(wfi *core.WorkflowInstance, event *
508
482
w .pendingEvents = append (w .pendingEvents , event )
509
483
}
510
484
511
- func (wt * workflowTester [TResult ]) SignalWorkflow (name string , value interface {}) {
485
+ // SignalWorkflow sends a signal to the workflow under test.
486
+ func (wt * workflowTester [TResult ]) SignalWorkflow (name string , value any ) {
512
487
wt .SignalWorkflowInstance (wt .wfi , name , value )
513
488
}
514
489
515
- func (wt * workflowTester [TResult ]) SignalWorkflowInstance (wfi * core.WorkflowInstance , name string , value interface {}) error {
490
+ // SignalWorkflowInstance sends a signal to the given workflow instance.
491
+ func (wt * workflowTester [TResult ]) SignalWorkflowInstance (wfi * core.WorkflowInstance , name string , value any ) error {
516
492
if wt .getWorkflow (wfi ) == nil {
517
493
return backend .ErrInstanceNotFound
518
494
}
@@ -541,10 +517,12 @@ func (wt *workflowTester[TResult]) SignalWorkflowInstance(wfi *core.WorkflowInst
541
517
return nil
542
518
}
543
519
520
+ // WorkflowFinished returns true if the workflow under test has finished.
544
521
func (wt * workflowTester [TResult ]) WorkflowFinished () bool {
545
522
return wt .workflowFinished
546
523
}
547
524
525
+ // WorkflowResult returns the result of the workflow under test.
548
526
func (wt * workflowTester [TResult ]) WorkflowResult () (TResult , error ) {
549
527
var r TResult
550
528
if wt .workflowResult != nil {
@@ -557,6 +535,7 @@ func (wt *workflowTester[TResult]) WorkflowResult() (TResult, error) {
557
535
return r , err
558
536
}
559
537
538
+ // AssertExpectations asserts that all expected activities were executed.
560
539
func (wt * workflowTester [TResult ]) AssertExpectations (t * testing.T ) {
561
540
wt .ma .AssertExpectations (t )
562
541
}
@@ -829,13 +808,3 @@ func getNextWorkflowTask(wfi *core.WorkflowInstance, history []*history.Event, n
829
808
NewEvents : newEvents ,
830
809
}
831
810
}
832
-
833
- type signaler [T any ] struct {
834
- wt * workflowTester [T ]
835
- }
836
-
837
- func (s * signaler [T ]) SignalWorkflow (ctx context.Context , instanceID string , name string , arg interface {}) error {
838
- return s .wt .SignalWorkflowInstance (core .NewWorkflowInstance (instanceID , "" ), name , arg )
839
- }
840
-
841
- var _ signals.Signaler = (* signaler [any ])(nil )
0 commit comments