forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow_test.go
172 lines (150 loc) · 6.03 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
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
// Copyright (c) 2019 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 canary
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"github.com/uber-go/tally"
"go.uber.org/cadence/.gen/go/shared"
"go.uber.org/cadence/mocks"
"go.uber.org/cadence/testsuite"
"go.uber.org/cadence/worker"
)
type (
workflowTestSuite struct {
suite.Suite
testsuite.WorkflowTestSuite
env *testsuite.TestWorkflowEnvironment
}
mockCadenceClient struct {
client *mocks.Client
domainClient *mocks.DomainClient
}
)
func TestWorkflowTestSuite(t *testing.T) {
suite.Run(t, new(workflowTestSuite))
}
func (s *workflowTestSuite) SetupTest() {
s.env = s.NewTestWorkflowEnvironment()
}
func (s *workflowTestSuite) TearDownTest() {
s.env.AssertExpectations(s.T())
}
func (s *workflowTestSuite) TestConcurrentExecWorkflow() {
s.env.SetWorkerOptions(newTestWorkerOptions(newMockActivityContext(newMockCadenceClient())))
s.env.ExecuteWorkflow(wfTypeConcurrentExec, time.Now().UnixNano(), "")
s.True(s.env.IsWorkflowCompleted())
s.NoError(s.env.GetWorkflowError())
}
func (s *workflowTestSuite) TestQueryWorkflow() {
// DOTO implement this test once client test framework
// SetOnActivityStartedListener is guaranteed to
// be executed and finished before activity started
}
func (s *workflowTestSuite) TestTimeout() {
// DOTO implement this test once client test framework
// can handle timeout error correctly
}
func (s *workflowTestSuite) TestSignalWorkflow() {
mockClient := newMockCadenceClient()
mockClient.client.On("SignalWorkflow",
mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
arg3 := args.Get(3).(string)
arg4 := args.Get(4).(string)
s.env.SignalWorkflow(arg3, arg4)
}).Return(nil)
s.env.SetWorkerOptions(newTestWorkerOptions(newMockActivityContext(mockClient)))
s.env.ExecuteWorkflow(wfTypeSignal, time.Now().UnixNano(), "")
s.True(s.env.IsWorkflowCompleted())
s.NoError(s.env.GetWorkflowError())
}
func (s *workflowTestSuite) TestEchoWorkflow() {
s.env.SetWorkerOptions(newTestWorkerOptions(newMockActivityContext(newMockCadenceClient())))
s.env.ExecuteWorkflow(wfTypeEcho, time.Now().UnixNano(), "")
s.True(s.env.IsWorkflowCompleted())
s.NoError(s.env.GetWorkflowError())
}
func (s *workflowTestSuite) TestVisibilityWorkflow() {
// uncomment below when race condition on s.env.SetOnActivityStartedListener is resolved
// mockClient := newMockCadenceClient()
// // setup the mock for visibility apis after the activity is invoked, because
// // we need the workflow id and run id to construct a response for the mock
// s.env.SetOnActivityStartedListener(func(activityInfo *cadence.ActivityInfo, ctx context.Context, args cadence.EncodedValues) {
// if activityInfo.ActivityType.Name != activityTypeVisibility {
// return
// }
// resp := newMockOpenWorkflowResponse(activityInfo.WorkflowExecution.ID, activityInfo.WorkflowExecution.RunID)
// mockClient.client.On("ListOpenWorkflow", mock.Anything, mock.Anything).Return(resp, nil)
// history := &shared.History{Events: []*shared.HistoryEvent{{}}}
// mockClient.client.On("GetWorkflowHistory", mock.Anything, mock.Anything, mock.Anything).Return(history, nil)
// })
// s.env.SetWorkerOptions(newTestWorkerOptions(newMockActivityContext(mockClient)))
// s.env.ExecuteWorkflow(wfTypeVisibility, time.Now().UnixNano(), &ServiceConfig{})
// s.True(s.env.IsWorkflowCompleted())
// s.NoError(s.env.GetWorkflowError())
}
func (s *workflowTestSuite) TestSanityWorkflow() {
oldWFList := sanityChildWFList
sanityChildWFList = []string{wfTypeEcho}
s.env.ExecuteWorkflow(wfTypeSanity, time.Now().UnixNano(), "")
sanityChildWFList = oldWFList
s.True(s.env.IsWorkflowCompleted())
s.NoError(s.env.GetWorkflowError())
}
func (s *workflowTestSuite) TestLocalActivityWorkflow() {
s.env.OnActivity(getConditionData).Return(int32(20), nil).Once()
s.env.ExecuteWorkflow(localActivityWorkfow)
s.True(s.env.IsWorkflowCompleted())
s.NoError(s.env.GetWorkflowError())
var result string
err := s.env.GetWorkflowResult(&result)
s.NoError(err)
s.Equal("data%2 == 0 and data%5 == 0", result)
}
func newMockOpenWorkflowResponse(wfID string, runID string) *shared.ListOpenWorkflowExecutionsResponse {
return &shared.ListOpenWorkflowExecutionsResponse{
Executions: []*shared.WorkflowExecutionInfo{
{Execution: &shared.WorkflowExecution{WorkflowId: &wfID, RunId: &runID}},
},
}
}
func newTestWorkerOptions(ctx *activityContext) worker.Options {
return worker.Options{
MetricsScope: tally.NoopScope,
BackgroundActivityContext: context.WithValue(context.Background(), ctxKeyActivityRuntime, ctx),
}
}
func newMockActivityContext(client mockCadenceClient) *activityContext {
return &activityContext{
cadence: cadenceClient{
Client: client.client,
DomainClient: client.domainClient,
},
}
}
func newMockCadenceClient() mockCadenceClient {
return mockCadenceClient{
client: new(mocks.Client),
domainClient: new(mocks.DomainClient),
}
}