@@ -121,6 +121,7 @@ type workflowTester[TResult any] struct {
121
121
122
122
workflowHistory []history.Event
123
123
clock * clock.Mock
124
+ startTime time.Time
124
125
125
126
timers []* testTimer
126
127
callbacks chan func () * history.WorkflowEvent
@@ -149,7 +150,7 @@ func WithTestTimeout(timeout time.Duration) WorkflowTesterOption {
149
150
}
150
151
151
152
func NewWorkflowTester [TResult any ](wf interface {}, opts ... WorkflowTesterOption ) WorkflowTester [TResult ] {
152
- // Start with the current wall-clock tiem
153
+ // Start with the current wall-clock time
153
154
clock := clock .NewMock ()
154
155
clock .Set (time .Now ())
155
156
@@ -246,6 +247,9 @@ func (wt *workflowTester[TResult]) OnSubWorkflow(workflow interface{}, args ...i
246
247
}
247
248
248
249
func (wt * workflowTester [TResult ]) Execute (args ... interface {}) {
250
+ // Record start time of test run
251
+ wt .startTime = wt .clock .Now ()
252
+
249
253
// Start workflow under test
250
254
initialEvent := wt .getInitialEvent (wt .wf , args )
251
255
wt .addWorkflow (wt .wfi , initialEvent )
@@ -295,6 +299,16 @@ func (wt *workflowTester[TResult]) Execute(args ...interface{}) {
295
299
}
296
300
}
297
301
302
+ // Schedule activities
303
+ for _ , event := range result .ActivityEvents {
304
+ gotNewEvents = true
305
+
306
+ a := event .Attributes .(* history.ActivityScheduledAttributes )
307
+ wt .logger .Debug ("Activity event" , "activity" , a .Name )
308
+
309
+ wt .scheduleActivity (tw .instance , event )
310
+ }
311
+
298
312
for _ , workflowEvent := range result .WorkflowEvents {
299
313
gotNewEvents = true
300
314
wt .logger .Debug ("Workflow event" , "event_type" , workflowEvent .HistoryEvent .Type )
@@ -314,11 +328,6 @@ func (wt *workflowTester[TResult]) Execute(args ...interface{}) {
314
328
315
329
wt .scheduleTimer (tw .instance , timerEvent )
316
330
}
317
-
318
- // Schedule activities
319
- for _ , event := range result .ActivityEvents {
320
- wt .scheduleActivity (tw .instance , event )
321
- }
322
331
}
323
332
324
333
for ! wt .workflowFinished && ! gotNewEvents {
@@ -334,12 +343,10 @@ func (wt *workflowTester[TResult]) Execute(args ...interface{}) {
334
343
default :
335
344
}
336
345
337
- if len (wt .timers ) > 0 {
338
- // Take first timer and execute it
339
- sort .SliceStable (wt .timers , func (i , j int ) bool {
340
- return wt .timers [i ].At .Before (wt .timers [j ].At )
341
- })
346
+ // If there are no running activities and timers, skip time and jump to the next scheduled timer
342
347
348
+ if atomic .LoadInt32 (& wt .runningActivities ) == 0 && len (wt .timers ) > 0 {
349
+ // Take first timer and execute it
343
350
t := wt .timers [0 ]
344
351
wt .timers = wt .timers [1 :]
345
352
@@ -431,8 +438,9 @@ func (wt *workflowTester[TResult]) AssertExpectations(t *testing.T) {
431
438
func (wt * workflowTester [TResult ]) scheduleActivity (wfi * core.WorkflowInstance , event history.Event ) {
432
439
e := event .Attributes .(* history.ActivityScheduledAttributes )
433
440
441
+ atomic .AddInt32 (& wt .runningActivities , 1 )
442
+
434
443
go func () {
435
- atomic .AddInt32 (& wt .runningActivities , 1 )
436
444
defer atomic .AddInt32 (& wt .runningActivities , - 1 )
437
445
438
446
var activityErr error
@@ -542,6 +550,10 @@ func (wt *workflowTester[TResult]) scheduleTimer(instance *core.WorkflowInstance
542
550
}
543
551
},
544
552
})
553
+
554
+ sort .SliceStable (wt .timers , func (i , j int ) bool {
555
+ return wt .timers [i ].At .Before (wt .timers [j ].At )
556
+ })
545
557
}
546
558
547
559
func (wt * workflowTester [TResult ]) cancelTimer (instance * core.WorkflowInstance , event history.Event ) {
0 commit comments