-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathworkflow_test.go
78 lines (60 loc) · 2.11 KB
/
workflow_test.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
package cron
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/testsuite"
"go.temporal.io/sdk/workflow"
)
type UnitTestSuite struct {
suite.Suite
testsuite.WorkflowTestSuite
}
func TestUnitTestSuite(t *testing.T) {
suite.Run(t, new(UnitTestSuite))
}
func (s *UnitTestSuite) Test_CronWorkflow() {
testWorkflow := func(ctx workflow.Context) error {
ctx1 := workflow.WithChildOptions(ctx, workflow.ChildWorkflowOptions{
WorkflowRunTimeout: 10 * time.Minute,
CronSchedule: "0 * * * *", // hourly
})
cronFuture := workflow.ExecuteChildWorkflow(ctx1, SampleCronWorkflow) // cron never stop so this future won't return
// wait 2 hours for the cron (cron will execute 3 times)
_ = workflow.Sleep(ctx, time.Hour*2)
s.False(cronFuture.IsReady())
return nil
}
env := s.NewTestWorkflowEnvironment()
env.RegisterWorkflow(testWorkflow)
env.RegisterWorkflow(SampleCronWorkflow)
env.RegisterActivity(DoSomething)
env.OnActivity(DoSomething, mock.Anything, mock.Anything, mock.Anything).Return(nil).Times(3)
var startTimeList, endTimeList []time.Time
env.SetOnActivityStartedListener(func(activityInfo *activity.Info, ctx context.Context, args converter.EncodedValues) {
var startTime, endTime time.Time
err := args.Get(&startTime, &endTime)
s.NoError(err)
startTimeList = append(startTimeList, startTime)
endTimeList = append(endTimeList, endTime)
})
startTime, err := time.Parse(time.RFC3339, "2018-08-22T16:30:00Z")
s.NoError(err)
env.SetStartTime(startTime)
env.ExecuteWorkflow(testWorkflow)
s.True(env.IsWorkflowCompleted())
err = env.GetWorkflowError()
s.NoError(err)
env.AssertExpectations(s.T())
s.Len(startTimeList, 3)
s.Equal(time.Time{}, startTimeList[0].UTC())
s.Equal(startTime, endTimeList[0].UTC())
s.Equal(startTime, startTimeList[1].UTC())
s.Equal(startTime.Add(time.Minute*30), endTimeList[1].UTC())
s.Equal(startTime.Add(time.Minute*30), startTimeList[2].UTC())
s.Equal(startTime.Add(time.Minute*90), endTimeList[2].UTC())
}