forked from uber-go/cadence-client
/
workflow_testsuite.go
632 lines (551 loc) · 29.1 KB
/
workflow_testsuite.go
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package internal
import (
"context"
"fmt"
"reflect"
"time"
"github.com/stretchr/testify/mock"
"github.com/uber-go/tally"
"go.uber.org/cadence/.gen/go/shared"
"go.uber.org/zap"
)
type (
// EncodedValues is a type alias used to encapsulate/extract encoded arguments from workflow/activity.
EncodedValues struct {
values []byte
dataConverter DataConverter
}
// ErrorDetailsValues is a type alias used hold error details objects.
ErrorDetailsValues []interface{}
// WorkflowTestSuite is the test suite to run unit tests for workflow/activity.
WorkflowTestSuite struct {
logger *zap.Logger
scope tally.Scope
ctxProps []ContextPropagator
header *shared.Header
}
// TestWorkflowEnvironment is the environment that you use to test workflow
TestWorkflowEnvironment struct {
mock.Mock
impl *testWorkflowEnvironmentImpl
}
// TestActivityEnvironment is the environment that you use to test activity
TestActivityEnvironment struct {
impl *testWorkflowEnvironmentImpl
}
// MockCallWrapper is a wrapper to mock.Call. It offers the ability to wait on workflow's clock instead of wall clock.
MockCallWrapper struct {
call *mock.Call
env *TestWorkflowEnvironment
runFn func(args mock.Arguments)
waitDuration func() time.Duration
}
)
func newEncodedValues(values []byte, dc DataConverter) Values {
if dc == nil {
dc = getDefaultDataConverter()
}
return &EncodedValues{values, dc}
}
// Get extract data from encoded data to desired value type. valuePtr is pointer to the actual value type.
func (b EncodedValues) Get(valuePtr ...interface{}) error {
if !b.HasValues() {
return ErrNoData
}
return b.dataConverter.FromData(b.values, valuePtr...)
}
// HasValues return whether there are values
func (b EncodedValues) HasValues() bool {
return b.values != nil
}
// Get extract data from encoded data to desired value type. valuePtr is pointer to the actual value type.
func (b ErrorDetailsValues) Get(valuePtr ...interface{}) error {
if !b.HasValues() {
return ErrNoData
}
if len(valuePtr) > len(b) {
return ErrTooManyArg
}
for i, item := range valuePtr {
reflect.ValueOf(item).Elem().Set(reflect.ValueOf(b[i]))
}
return nil
}
// HasValues return whether there are values.
func (b ErrorDetailsValues) HasValues() bool {
return b != nil && len(b) != 0
}
// NewTestWorkflowEnvironment creates a new instance of TestWorkflowEnvironment. Use the returned TestWorkflowEnvironment
// to run your workflow in the test environment.
func (s *WorkflowTestSuite) NewTestWorkflowEnvironment() *TestWorkflowEnvironment {
return &TestWorkflowEnvironment{impl: newTestWorkflowEnvironmentImpl(s)}
}
// NewTestActivityEnvironment creates a new instance of TestActivityEnvironment. Use the returned TestActivityEnvironment
// to run your activity in the test environment.
func (s *WorkflowTestSuite) NewTestActivityEnvironment() *TestActivityEnvironment {
return &TestActivityEnvironment{impl: newTestWorkflowEnvironmentImpl(s)}
}
// SetLogger sets the logger for this WorkflowTestSuite. If you don't set logger, test suite will create a default logger
// with Debug level logging enabled.
func (s *WorkflowTestSuite) SetLogger(logger *zap.Logger) {
s.logger = logger
}
// GetLogger gets the logger for this WorkflowTestSuite.
func (s *WorkflowTestSuite) GetLogger() *zap.Logger {
return s.logger
}
// SetMetricsScope sets the metrics scope for this WorkflowTestSuite. If you don't set scope, test suite will use
// tally.NoopScope
func (s *WorkflowTestSuite) SetMetricsScope(scope tally.Scope) {
s.scope = scope
}
// SetContextPropagators sets the context propagators for this WorkflowTestSuite. If you don't set context propagators,
// test suite will not use context propagators
func (s *WorkflowTestSuite) SetContextPropagators(ctxProps []ContextPropagator) {
s.ctxProps = ctxProps
}
// SetHeader sets the headers for this WorkflowTestSuite. If you don't set header, test suite will not pass headers to
// the workflow
func (s *WorkflowTestSuite) SetHeader(header *shared.Header) {
s.header = header
}
// ExecuteActivity executes an activity. The tested activity will be executed synchronously in the calling goroutinue.
// Caller should use Value.Get() to extract strong typed result value.
func (t *TestActivityEnvironment) ExecuteActivity(activityFn interface{}, args ...interface{}) (Value, error) {
return t.impl.executeActivity(activityFn, args...)
}
// ExecuteLocalActivity executes a local activity. The tested activity will be executed synchronously in the calling goroutinue.
// Caller should use Value.Get() to extract strong typed result value.
func (t *TestActivityEnvironment) ExecuteLocalActivity(activityFn interface{}, args ...interface{}) (Value, error) {
return t.impl.executeLocalActivity(activityFn, args...)
}
// SetWorkerOptions sets the WorkerOptions that will be use by TestActivityEnvironment. TestActivityEnvironment will
// use options of Identity, MetricsScope and BackgroundActivityContext on the WorkerOptions. Other options are ignored.
// Note: WorkerOptions is defined in internal package, use public type worker.Options instead.
func (t *TestActivityEnvironment) SetWorkerOptions(options WorkerOptions) *TestActivityEnvironment {
t.impl.setWorkerOptions(options)
return t
}
// SetTestTimeout sets the wall clock timeout for this activity test run. When test timeout happen, it means activity is
// taking too long.
func (t *TestActivityEnvironment) SetTestTimeout(idleTimeout time.Duration) *TestActivityEnvironment {
t.impl.testTimeout = idleTimeout
return t
}
// SetHeartbeatDetails sets the heartbeat details to be returned from activity.GetHeartbeatDetails()
func (t *TestActivityEnvironment) SetHeartbeatDetails(details interface{}) {
t.impl.setHeartbeatDetails(details)
}
// SetWorkerStopChannel sets the worker stop channel to be returned from activity.GetWorkerStopChannel(context)
// To test your activity on worker stop, you can provide a go channel with this function and call ExecuteActivity().
// Then call close(channel) to test the activity worker stop logic.
func (t *TestActivityEnvironment) SetWorkerStopChannel(c chan struct{}) {
t.impl.setWorkerStopChannel(c)
}
// SetStartTime sets the start time of the workflow. This is optional, default start time will be the wall clock time when
// workflow starts. Start time is the workflow.Now(ctx) time at the beginning of the workflow.
func (t *TestWorkflowEnvironment) SetStartTime(startTime time.Time) {
t.impl.setStartTime(startTime)
}
// OnActivity setup a mock call for activity. Parameter activity must be activity function (func) or activity name (string).
// You must call Return() with appropriate parameters on the returned *MockCallWrapper instance. The supplied parameters to
// the Return() call should either be a function that has exact same signature as the mocked activity, or it should be
// mock values with the same types as the mocked activity function returns.
// Example: assume the activity you want to mock has function signature as:
// func MyActivity(ctx context.Context, msg string) (string, error)
// You can mock it by return a function with exact same signature:
// t.OnActivity(MyActivity, mock.Anything, mock.Anything).Return(func(ctx context.Context, msg string) (string, error) {
// // your mock function implementation
// return "", nil
// })
// OR return mock values with same types as activity function's return types:
// t.OnActivity(MyActivity, mock.Anything, mock.Anything).Return("mock_result", nil)
func (t *TestWorkflowEnvironment) OnActivity(activity interface{}, args ...interface{}) *MockCallWrapper {
fType := reflect.TypeOf(activity)
var call *mock.Call
switch fType.Kind() {
case reflect.Func:
fnType := reflect.TypeOf(activity)
if err := validateFnFormat(fnType, false); err != nil {
panic(err)
}
fnName := getFunctionName(activity)
if alias, ok := getHostEnvironment().getActivityAlias(fnName); ok {
fnName = alias
}
call = t.Mock.On(fnName, args...)
case reflect.String:
call = t.Mock.On(activity.(string), args...)
default:
panic("activity must be function or string")
}
return t.wrapCall(call)
}
// ErrMockStartChildWorkflowFailed is special error used to indicate the mocked child workflow should fail to start.
// This error is also exposed as public as testsuite.ErrMockStartChildWorkflowFailed
var ErrMockStartChildWorkflowFailed = fmt.Errorf("start child workflow failed: %v", shared.ChildWorkflowExecutionFailedCauseWorkflowAlreadyRunning)
// OnWorkflow setup a mock call for workflow. Parameter workflow must be workflow function (func) or workflow name (string).
// You must call Return() with appropriate parameters on the returned *MockCallWrapper instance. The supplied parameters to
// the Return() call should either be a function that has exact same signature as the mocked workflow, or it should be
// mock values with the same types as the mocked workflow function returns.
// Example: assume the workflow you want to mock has function signature as:
// func MyChildWorkflow(ctx workflow.Context, msg string) (string, error)
// You can mock it by return a function with exact same signature:
// t.OnWorkflow(MyChildWorkflow, mock.Anything, mock.Anything).Return(func(ctx workflow.Context, msg string) (string, error) {
// // your mock function implementation
// return "", nil
// })
// OR return mock values with same types as workflow function's return types:
// t.OnWorkflow(MyChildWorkflow, mock.Anything, mock.Anything).Return("mock_result", nil)
// You could also setup mock to simulate start child workflow failure case by returning ErrMockStartChildWorkflowFailed
// as error.
func (t *TestWorkflowEnvironment) OnWorkflow(workflow interface{}, args ...interface{}) *MockCallWrapper {
fType := reflect.TypeOf(workflow)
var call *mock.Call
switch fType.Kind() {
case reflect.Func:
fnType := reflect.TypeOf(workflow)
if err := validateFnFormat(fnType, true); err != nil {
panic(err)
}
fnName := getFunctionName(workflow)
if alias, ok := getHostEnvironment().getWorkflowAlias(fnName); ok {
fnName = alias
}
call = t.Mock.On(fnName, args...)
case reflect.String:
call = t.Mock.On(workflow.(string), args...)
default:
panic("activity must be function or string")
}
return t.wrapCall(call)
}
const mockMethodForSignalExternalWorkflow = "workflow.SignalExternalWorkflow"
const mockMethodForRequestCancelExternalWorkflow = "workflow.RequestCancelExternalWorkflow"
const mockMethodForGetVersion = "workflow.GetVersion"
const mockMethodForUpsertSearchAttributes = "workflow.UpsertSearchAttributes"
// OnSignalExternalWorkflow setup a mock for sending signal to external workflow.
// This TestWorkflowEnvironment handles sending signals between the workflows that are started from the root workflow.
// For example, sending signals between parent and child workflows. Or sending signals between 2 child workflows.
// However, it does not know what to do if your tested workflow code is sending signal to external unknown workflows.
// In that case, you will need to setup mock for those signal calls.
// Some examples of how to setup mock:
//
// * mock for specific target workflow that matches specific signal name and signal data
// env.OnSignalExternalWorkflow("test-domain", "test-workflow-id1", "test-runid1", "test-signal", "test-data").Return(nil).Once()
// * mock for anything and succeed the send
// env.OnSignalExternalWorkflow(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
// * mock for anything and fail the send
// env.OnSignalExternalWorkflow(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.New("unknown external workflow")).Once()
// * mock function for SignalExternalWorkflow
// env.OnSignalExternalWorkflow(mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(
// func(domainName, workflowID, runID, signalName string, arg interface{}) error {
// // you can do differently based on the parameters
// return nil
// })
func (t *TestWorkflowEnvironment) OnSignalExternalWorkflow(domainName, workflowID, runID, signalName, arg interface{}) *MockCallWrapper {
call := t.Mock.On(mockMethodForSignalExternalWorkflow, domainName, workflowID, runID, signalName, arg)
return t.wrapCall(call)
}
// OnRequestCancelExternalWorkflow setup a mock for cancellation of external workflow.
// This TestWorkflowEnvironment handles cancellation of workflows that are started from the root workflow.
// For example, cancellation sent from parent to child workflows. Or cancellation between 2 child workflows.
// However, it does not know what to do if your tested workflow code is sending cancellation to external unknown workflows.
// In that case, you will need to setup mock for those cancel calls.
// Some examples of how to setup mock:
//
// * mock for specific target workflow that matches specific workflow ID and run ID
// env.OnRequestCancelExternalWorkflow("test-domain", "test-workflow-id1", "test-runid1").Return(nil).Once()
// * mock for anything and succeed the cancellation
// env.OnRequestCancelExternalWorkflow(mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
// * mock for anything and fail the cancellation
// env.OnRequestCancelExternalWorkflow(mock.Anything, mock.Anything, mock.Anything).Return(errors.New("unknown external workflow")).Once()
// * mock function for RequestCancelExternalWorkflow
// env.OnRequestCancelExternalWorkflow(mock.Anything, mock.Anything, mock.Anything).Return(
// func(domainName, workflowID, runID) error {
// // you can do differently based on the parameters
// return nil
// })
func (t *TestWorkflowEnvironment) OnRequestCancelExternalWorkflow(domainName, workflowID, runID string) *MockCallWrapper {
call := t.Mock.On(mockMethodForRequestCancelExternalWorkflow, domainName, workflowID, runID)
return t.wrapCall(call)
}
// OnGetVersion setup a mock for workflow.GetVersion() call. By default, if mock is not setup, the GetVersion call from
// workflow code will always return the maxSupported version. Make it not possible to test old version branch. With this
// mock support, it is possible to test code branch for different versions.
//
// Note: mock can be setup for a specific changeID. Or if mock.Anything is used as changeID then all calls to GetVersion
// will be mocked. Mock for a specific changeID has higher priority over mock.Anything.
func (t *TestWorkflowEnvironment) OnGetVersion(changeID string, minSupported, maxSupported Version) *MockCallWrapper {
call := t.Mock.On(getMockMethodForGetVersion(changeID), changeID, minSupported, maxSupported)
return t.wrapCall(call)
}
// OnUpsertSearchAttributes setup a mock for workflow.UpsertSearchAttributes call.
// If mock is not setup, the UpsertSearchAttributes call will only validate input attributes.
// If mock is setup, all UpsertSearchAttributes calls in workflow have to be mocked.
func (t *TestWorkflowEnvironment) OnUpsertSearchAttributes(attributes map[string]interface{}) *MockCallWrapper {
call := t.Mock.On(mockMethodForUpsertSearchAttributes, attributes)
return t.wrapCall(call)
}
func (t *TestWorkflowEnvironment) wrapCall(call *mock.Call) *MockCallWrapper {
callWrapper := &MockCallWrapper{call: call, env: t}
call.Run(t.impl.getMockRunFn(callWrapper))
return callWrapper
}
// Once indicates that the mock should only return the value once.
func (c *MockCallWrapper) Once() *MockCallWrapper {
return c.Times(1)
}
// Twice indicates that the mock should only return the value twice.
func (c *MockCallWrapper) Twice() *MockCallWrapper {
return c.Times(2)
}
// Times indicates that the mock should only return the indicated number of times.
func (c *MockCallWrapper) Times(i int) *MockCallWrapper {
c.call.Times(i)
return c
}
// Run sets a handler to be called before returning. It can be used when mocking a method such as unmarshalers that
// takes a pointer to a struct and sets properties in such struct.
func (c *MockCallWrapper) Run(fn func(args mock.Arguments)) *MockCallWrapper {
c.runFn = fn
return c
}
// After sets how long to wait on workflow's clock before the mock call returns.
func (c *MockCallWrapper) After(d time.Duration) *MockCallWrapper {
c.waitDuration = func() time.Duration { return d }
return c
}
// AfterFn sets a function which will tell how long to wait on workflow's clock before the mock call returns.
func (c *MockCallWrapper) AfterFn(fn func() time.Duration) *MockCallWrapper {
c.waitDuration = fn
return c
}
// Return specifies the return arguments for the expectation.
func (c *MockCallWrapper) Return(returnArguments ...interface{}) *MockCallWrapper {
c.call.Return(returnArguments...)
return c
}
// ExecuteWorkflow executes a workflow, wait until workflow complete. It will fail the test if workflow is blocked and
// cannot complete within TestTimeout (set by SetTestTimeout()).
func (t *TestWorkflowEnvironment) ExecuteWorkflow(workflowFn interface{}, args ...interface{}) {
t.impl.mock = &t.Mock
t.impl.executeWorkflow(workflowFn, args...)
}
// Now returns the current workflow time (a.k.a workflow.Now() time) of this TestWorkflowEnvironment.
func (t *TestWorkflowEnvironment) Now() time.Time {
return t.impl.Now()
}
// SetWorkerOptions sets the WorkerOptions for TestWorkflowEnvironment. TestWorkflowEnvironment will use options set by
// use options of Identity, MetricsScope and BackgroundActivityContext on the WorkerOptions. Other options are ignored.
// Note: WorkerOptions is defined in internal package, use public type worker.Options instead.
func (t *TestWorkflowEnvironment) SetWorkerOptions(options WorkerOptions) *TestWorkflowEnvironment {
t.impl.setWorkerOptions(options)
return t
}
// SetWorkerStopChannel sets the activity worker stop channel to be returned from activity.GetWorkerStopChannel(context)
// You can use this function to set the activity worker stop channel and use close(channel) to test your activity execution
// from workflow execution.
func (t *TestWorkflowEnvironment) SetWorkerStopChannel(c chan struct{}) {
t.impl.setWorkerStopChannel(c)
}
// SetTestTimeout sets the idle timeout based on wall clock for this tested workflow. Idle is when workflow is blocked
// waiting on events (including timer, activity, child workflow, signal etc). If there is no event happening longer than
// this idle timeout, the test framework would stop the workflow and return timeout error.
// This is based on real wall clock time, not the workflow time (a.k.a workflow.Now() time).
func (t *TestWorkflowEnvironment) SetTestTimeout(idleTimeout time.Duration) *TestWorkflowEnvironment {
t.impl.testTimeout = idleTimeout
return t
}
// SetWorkflowTimeout sets the execution timeout for this tested workflow. This test framework uses mock clock internally
// and when workflow is blocked on timer, it will auto forward the mock clock. Use SetWorkflowTimeout() to enforce a
// workflow execution timeout to return timeout error when the workflow mock clock is moved head of the timeout.
// This is based on the workflow time (a.k.a workflow.Now() time).
func (t *TestWorkflowEnvironment) SetWorkflowTimeout(executionTimeout time.Duration) *TestWorkflowEnvironment {
t.impl.executionTimeout = executionTimeout
return t
}
// SetOnActivityStartedListener sets a listener that will be called before activity starts execution.
// Note: ActivityInfo is defined in internal package, use public type activity.Info instead.
func (t *TestWorkflowEnvironment) SetOnActivityStartedListener(
listener func(activityInfo *ActivityInfo, ctx context.Context, args Values)) *TestWorkflowEnvironment {
t.impl.onActivityStartedListener = listener
return t
}
// SetOnActivityCompletedListener sets a listener that will be called after an activity is completed.
// Note: ActivityInfo is defined in internal package, use public type activity.Info instead.
func (t *TestWorkflowEnvironment) SetOnActivityCompletedListener(
listener func(activityInfo *ActivityInfo, result Value, err error)) *TestWorkflowEnvironment {
t.impl.onActivityCompletedListener = listener
return t
}
// SetOnActivityCanceledListener sets a listener that will be called after an activity is canceled.
// Note: ActivityInfo is defined in internal package, use public type activity.Info instead.
func (t *TestWorkflowEnvironment) SetOnActivityCanceledListener(
listener func(activityInfo *ActivityInfo)) *TestWorkflowEnvironment {
t.impl.onActivityCanceledListener = listener
return t
}
// SetOnActivityHeartbeatListener sets a listener that will be called when activity heartbeat.
// Note: ActivityInfo is defined in internal package, use public type activity.Info instead.
func (t *TestWorkflowEnvironment) SetOnActivityHeartbeatListener(
listener func(activityInfo *ActivityInfo, details Values)) *TestWorkflowEnvironment {
t.impl.onActivityHeartbeatListener = listener
return t
}
// SetOnChildWorkflowStartedListener sets a listener that will be called before a child workflow starts execution.
// Note: WorkflowInfo is defined in internal package, use public type workflow.Info instead.
func (t *TestWorkflowEnvironment) SetOnChildWorkflowStartedListener(
listener func(workflowInfo *WorkflowInfo, ctx Context, args Values)) *TestWorkflowEnvironment {
t.impl.onChildWorkflowStartedListener = listener
return t
}
// SetOnChildWorkflowCompletedListener sets a listener that will be called after a child workflow is completed.
// Note: WorkflowInfo is defined in internal package, use public type workflow.Info instead.
func (t *TestWorkflowEnvironment) SetOnChildWorkflowCompletedListener(
listener func(workflowInfo *WorkflowInfo, result Value, err error)) *TestWorkflowEnvironment {
t.impl.onChildWorkflowCompletedListener = listener
return t
}
// SetOnChildWorkflowCanceledListener sets a listener that will be called when a child workflow is canceled.
// Note: WorkflowInfo is defined in internal package, use public type workflow.Info instead.
func (t *TestWorkflowEnvironment) SetOnChildWorkflowCanceledListener(
listener func(workflowInfo *WorkflowInfo)) *TestWorkflowEnvironment {
t.impl.onChildWorkflowCanceledListener = listener
return t
}
// SetOnTimerScheduledListener sets a listener that will be called before a timer is scheduled.
func (t *TestWorkflowEnvironment) SetOnTimerScheduledListener(
listener func(timerID string, duration time.Duration)) *TestWorkflowEnvironment {
t.impl.onTimerScheduledListener = listener
return t
}
// SetOnTimerFiredListener sets a listener that will be called after a timer is fired.
func (t *TestWorkflowEnvironment) SetOnTimerFiredListener(listener func(timerID string)) *TestWorkflowEnvironment {
t.impl.onTimerFiredListener = listener
return t
}
// SetOnTimerCancelledListener sets a listener that will be called after a timer is cancelled
func (t *TestWorkflowEnvironment) SetOnTimerCancelledListener(listener func(timerID string)) *TestWorkflowEnvironment {
t.impl.onTimerCancelledListener = listener
return t
}
// SetOnLocalActivityStartedListener sets a listener that will be called before local activity starts execution.
// Note: ActivityInfo is defined in internal package, use public type activity.Info instead.
func (t *TestWorkflowEnvironment) SetOnLocalActivityStartedListener(
listener func(activityInfo *ActivityInfo, ctx context.Context, args []interface{})) *TestWorkflowEnvironment {
t.impl.onLocalActivityStartedListener = listener
return t
}
// SetOnLocalActivityCompletedListener sets a listener that will be called after local activity is completed.
// Note: ActivityInfo is defined in internal package, use public type activity.Info instead.
func (t *TestWorkflowEnvironment) SetOnLocalActivityCompletedListener(
listener func(activityInfo *ActivityInfo, result Value, err error)) *TestWorkflowEnvironment {
t.impl.onLocalActivityCompletedListener = listener
return t
}
// SetOnLocalActivityCanceledListener sets a listener that will be called after local activity is canceled.
// Note: ActivityInfo is defined in internal package, use public type activity.Info instead.
func (t *TestWorkflowEnvironment) SetOnLocalActivityCanceledListener(
listener func(activityInfo *ActivityInfo)) *TestWorkflowEnvironment {
t.impl.onLocalActivityCanceledListener = listener
return t
}
// IsWorkflowCompleted check if test is completed or not
func (t *TestWorkflowEnvironment) IsWorkflowCompleted() bool {
return t.impl.isTestCompleted
}
// GetWorkflowResult extracts the encoded result from test workflow, it returns error if the extraction failed.
func (t *TestWorkflowEnvironment) GetWorkflowResult(valuePtr interface{}) error {
if !t.impl.isTestCompleted {
panic("workflow is not completed")
}
if t.impl.testError != nil || t.impl.testResult == nil || valuePtr == nil {
return t.impl.testError
}
return t.impl.testResult.Get(valuePtr)
}
// GetWorkflowError return the error from test workflow
func (t *TestWorkflowEnvironment) GetWorkflowError() error {
return t.impl.testError
}
// CompleteActivity complete an activity that had returned activity.ErrResultPending error
func (t *TestWorkflowEnvironment) CompleteActivity(taskToken []byte, result interface{}, err error) error {
return t.impl.CompleteActivity(taskToken, result, err)
}
// CancelWorkflow requests cancellation (through workflow Context) to the currently running test workflow.
func (t *TestWorkflowEnvironment) CancelWorkflow() {
t.impl.cancelWorkflow(func(result []byte, err error) {})
}
// SignalWorkflow sends signal to the currently running test workflow.
func (t *TestWorkflowEnvironment) SignalWorkflow(name string, input interface{}) {
t.impl.signalWorkflow(name, input, true)
}
// SignalWorkflowSkippingDecision sends signal to the currently running test workflow without invoking workflow code.
// Used to test processing of multiple buffered signals before completing workflow.
// It must be followed by SignalWorkflow, CancelWorkflow or CompleteActivity to force a decision.
func (t *TestWorkflowEnvironment) SignalWorkflowSkippingDecision(name string, input interface{}) {
t.impl.signalWorkflow(name, input, false)
}
// SignalWorkflowByID sends signal to the currently running test workflow.
func (t *TestWorkflowEnvironment) SignalWorkflowByID(workflowID, signalName string, input interface{}) error {
return t.impl.signalWorkflowByID(workflowID, signalName, input)
}
// QueryWorkflow queries to the currently running test workflow and returns result synchronously.
func (t *TestWorkflowEnvironment) QueryWorkflow(queryType string, args ...interface{}) (Value, error) {
return t.impl.queryWorkflow(queryType, args...)
}
// RegisterDelayedCallback creates a new timer with specified delayDuration using workflow clock (not wall clock). When
// the timer fires, the callback will be called. By default, this test suite uses mock clock which automatically move
// forward to fire next timer when workflow is blocked. Use this API to make some event (like activity completion,
// signal or workflow cancellation) at desired time.
func (t *TestWorkflowEnvironment) RegisterDelayedCallback(callback func(), delayDuration time.Duration) {
t.impl.registerDelayedCallback(callback, delayDuration)
}
// SetActivityTaskList set the affinity between activity and tasklist. By default, activity can be invoked by any tasklist
// in this test environment. Use this SetActivityTaskList() to set affinity between activity and a tasklist. Once
// activity is set to a particular tasklist, that activity will only be available to that tasklist.
func (t *TestWorkflowEnvironment) SetActivityTaskList(tasklist string, activityFn ...interface{}) {
t.impl.setActivityTaskList(tasklist, activityFn...)
}
// SetLastCompletionResult sets the result to be returned from workflow.GetLastCompletionResult().
func (t *TestWorkflowEnvironment) SetLastCompletionResult(result interface{}) {
t.impl.setLastCompletionResult(result)
}
// SetMemoOnStart sets the memo when start workflow.
func (t *TestWorkflowEnvironment) SetMemoOnStart(memo map[string]interface{}) error {
memoStruct, err := getWorkflowMemo(memo, t.impl.GetDataConverter())
if err != nil {
return err
}
t.impl.workflowInfo.Memo = memoStruct
return nil
}
// SetSearchAttributesOnStart sets the search attributes when start workflow.
func (t *TestWorkflowEnvironment) SetSearchAttributesOnStart(searchAttributes map[string]interface{}) error {
attr, err := serializeSearchAttributes(searchAttributes)
if err != nil {
return err
}
t.impl.workflowInfo.SearchAttributes = attr
return nil
}