forked from uber-go/cadence-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internal_worker_interfaces_test.go
185 lines (160 loc) · 5.68 KB
/
internal_worker_interfaces_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
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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 cadence
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
m "go.uber.org/cadence/.gen/go/shared"
"go.uber.org/cadence/mocks"
"go.uber.org/zap"
)
var (
// ErrWorkflowTypeNotExist indicated workflow type doesn't exist in registry.
ErrWorkflowTypeNotExist = errors.New("workflow type doesn't existin the registry")
)
type (
// Workflow decider
helloWorldWorkflow struct {
cancelActivity bool
}
// Greeter activity
greeterActivity struct {
}
InterfacesTestSuite struct {
suite.Suite
}
)
func helloWorldWorkflowFunc(ctx Context, input []byte) error {
var queryResult string
SetQueryHandler(ctx, "test-query", func() (string, error) {
return queryResult, nil
})
activityName := "Greeter_Activity"
ao := ActivityOptions{
TaskList: "taskList",
ActivityID: "0",
ScheduleToStartTimeout: time.Minute,
StartToCloseTimeout: time.Minute,
HeartbeatTimeout: 20 * time.Second,
}
ctx = WithActivityOptions(ctx, ao)
var result []byte
queryResult = "waiting-activity-result"
err := ExecuteActivity(ctx, activityName).Get(ctx, &result)
if err == nil {
queryResult = "done"
fmt.Println("Result", result)
return nil
}
queryResult = "error:" + err.Error()
return err
}
func helloWorldWorkflowCancelFunc(ctx Context, input []byte) error {
activityName := "Greeter_Activity"
ao := ActivityOptions{
TaskList: "taskList",
ActivityID: "0",
ScheduleToStartTimeout: time.Minute,
StartToCloseTimeout: time.Minute,
HeartbeatTimeout: 20 * time.Second,
}
ctx = WithActivityOptions(ctx, ao)
ExecuteActivity(ctx, activityName)
getWorkflowEnvironment(ctx).RequestCancelActivity("0")
return nil
}
// Greeter activity methods
func (ga greeterActivity) ActivityType() ActivityType {
activityName := "Greeter_Activity"
return ActivityType{Name: activityName}
}
func (ga greeterActivity) Execute(ctx context.Context, input []byte) ([]byte, error) {
return []byte("World"), nil
}
func (ga greeterActivity) GetFunction() interface{} {
return ga.Execute
}
// Greeter activity func
func greeterActivityFunc(ctx context.Context, input []byte) ([]byte, error) {
return []byte("Hello world"), nil
}
// Test suite.
func (s *InterfacesTestSuite) SetupTest() {
}
func TestInterfacesTestSuite(t *testing.T) {
suite.Run(t, new(InterfacesTestSuite))
}
func (s *InterfacesTestSuite) TestInterface() {
logger, _ := zap.NewDevelopment()
domain := "testDomain"
// Workflow execution parameters.
workflowExecutionParameters := workerExecutionParameters{
TaskList: "testTaskList",
ConcurrentPollRoutineSize: 4,
Logger: logger,
}
// Create service endpoint
service := new(mocks.TChanWorkflowService)
domainStatus := m.DomainStatus_REGISTERED
domainDesc := &m.DescribeDomainResponse{
DomainInfo: &m.DomainInfo{
Name: &domain,
Status: &domainStatus,
},
}
// mocks
service.On("DescribeDomain", mock.Anything, mock.Anything).Return(domainDesc, nil)
service.On("PollForActivityTask", mock.Anything, mock.Anything).Return(&m.PollForActivityTaskResponse{}, nil)
service.On("RespondActivityTaskCompleted", mock.Anything, mock.Anything).Return(nil)
service.On("PollForDecisionTask", mock.Anything, mock.Anything).Return(&m.PollForDecisionTaskResponse{}, nil)
service.On("RespondDecisionTaskCompleted", mock.Anything, mock.Anything).Return(nil)
service.On("StartWorkflowExecution", mock.Anything, mock.Anything).Return(&m.StartWorkflowExecutionResponse{}, nil)
env := getHostEnvironment()
// Launch worker.
workflowWorker := newWorkflowWorker(service, domain, workflowExecutionParameters, nil, env)
defer workflowWorker.Stop()
workflowWorker.Start()
// Create activity execution parameters.
activityExecutionParameters := workerExecutionParameters{
TaskList: "testTaskList",
ConcurrentPollRoutineSize: 10,
Logger: logger,
}
// Register activity instances and launch the worker.
activityWorker := newActivityWorker(service, domain, activityExecutionParameters, nil, env)
defer activityWorker.Stop()
activityWorker.Start()
// Start a workflow.
workflowOptions := StartWorkflowOptions{
ID: "HelloWorld_Workflow",
TaskList: "testTaskList",
ExecutionStartToCloseTimeout: 10 * time.Second,
DecisionTaskStartToCloseTimeout: 10 * time.Second,
}
workflowClient := NewClient(service, domain, nil)
wfExecution, err := workflowClient.StartWorkflow(context.Background(), workflowOptions, "workflowType")
s.NoError(err)
fmt.Printf("Started workflow: %v \n", wfExecution)
}