forked from temporalio/temporal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity_api_update_test.go
325 lines (274 loc) · 11.1 KB
/
activity_api_update_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
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
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 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 tests
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
activitypb "go.temporal.io/api/activity/v1"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/api/workflowservice/v1"
sdkclient "go.temporal.io/sdk/client"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
"go.temporal.io/server/common/testing/testvars"
"go.temporal.io/server/tests/testcore"
"google.golang.org/protobuf/types/known/durationpb"
"google.golang.org/protobuf/types/known/fieldmaskpb"
)
type ActivityApiUpdateClientTestSuite struct {
testcore.FunctionalTestSdkSuite
tv *testvars.TestVars
}
func TestActivityApiUpdateClientTestSuite(t *testing.T) {
s := new(ActivityApiUpdateClientTestSuite)
suite.Run(t, s)
}
func (s *ActivityApiUpdateClientTestSuite) SetupTest() {
s.FunctionalTestSdkSuite.SetupTest()
s.tv = testvars.New(s.T()).WithTaskQueue(s.TaskQueue()).WithNamespaceName(s.Namespace())
}
type (
ActivityFunctions func() (string, error)
WorkflowFunction func(context2 workflow.Context) error
)
func (s *ActivityApiUpdateClientTestSuite) makeWorkflowFunc(
activityFunction ActivityFunctions,
scheduleToCloseTimeout time.Duration,
initialRetryInterval time.Duration,
) WorkflowFunction {
return func(ctx workflow.Context) error {
activityRetryPolicy := &temporal.RetryPolicy{
InitialInterval: initialRetryInterval,
BackoffCoefficient: 1,
}
var ret string
err := workflow.ExecuteActivity(workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
ActivityID: "activity-id",
DisableEagerExecution: true,
ScheduleToCloseTimeout: scheduleToCloseTimeout,
StartToCloseTimeout: scheduleToCloseTimeout,
RetryPolicy: activityRetryPolicy,
}), activityFunction).Get(ctx, &ret)
return err
}
}
func (s *ActivityApiUpdateClientTestSuite) TestActivityUpdateApi_ChangeRetryInterval() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
activityUpdated := make(chan struct{})
var startedActivityCount atomic.Int32
activityFunction := func() (string, error) {
startedActivityCount.Add(1)
if startedActivityCount.Load() == 1 {
activityErr := errors.New("bad-luck-please-retry")
return "", activityErr
}
s.WaitForChannel(ctx, activityUpdated)
return "done!", nil
}
scheduleToCloseTimeout := 30 * time.Minute
retryTimeout := 10 * time.Minute
workflowFn := s.makeWorkflowFunc(activityFunction, scheduleToCloseTimeout, retryTimeout)
s.Worker().RegisterWorkflow(workflowFn)
s.Worker().RegisterActivity(activityFunction)
workflowOptions := sdkclient.StartWorkflowOptions{
ID: s.tv.WorkflowID(),
TaskQueue: s.TaskQueue(),
}
workflowRun, err := s.SdkClient().ExecuteWorkflow(ctx, workflowOptions, workflowFn)
s.NoError(err)
s.EventuallyWithT(func(t *assert.CollectT) {
description, err := s.SdkClient().DescribeWorkflowExecution(ctx, workflowRun.GetID(), workflowRun.GetRunID())
assert.NoError(t, err)
if err != nil {
assert.Len(t, description.GetPendingActivities(), 1)
assert.Equal(t, int32(1), startedActivityCount.Load())
}
}, 10*time.Second, 500*time.Millisecond)
updateRequest := &workflowservice.UpdateActivityOptionsByIdRequest{
Namespace: s.Namespace().String(),
WorkflowId: workflowRun.GetID(),
ActivityId: "activity-id",
ActivityOptions: &activitypb.ActivityOptions{
RetryPolicy: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(1 * time.Second),
},
},
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"retry_policy.initial_interval"}},
}
resp, err := s.FrontendClient().UpdateActivityOptionsById(ctx, updateRequest)
s.NoError(err)
s.NotNil(resp)
description, err := s.SdkClient().DescribeWorkflowExecution(ctx, workflowRun.GetID(), workflowRun.GetRunID())
s.NoError(err)
s.Equal(1, len(description.PendingActivities))
activityUpdated <- struct{}{}
s.EventuallyWithT(func(t *assert.CollectT) {
description, err = s.SdkClient().DescribeWorkflowExecution(ctx, workflowRun.GetID(), workflowRun.GetRunID())
assert.NoError(t, err)
if err != nil {
assert.Len(t, description.GetPendingActivities(), 0)
assert.Equal(t, int32(2), startedActivityCount.Load())
}
}, 3*time.Second, 100*time.Millisecond)
var out string
err = workflowRun.Get(ctx, &out)
s.NoError(err)
}
func (s *ActivityApiUpdateClientTestSuite) TestActivityUpdateApi_ChangeScheduleToClose() {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var startedActivityCount atomic.Int32
activityFunction := func() (string, error) {
startedActivityCount.Add(1)
if startedActivityCount.Load() == 1 {
activityErr := errors.New("bad-luck-please-retry")
return "", activityErr
}
return "done!", nil
}
scheduleToCloseTimeout := 30 * time.Minute
retryTimeout := 10 * time.Minute
workflowFn := s.makeWorkflowFunc(activityFunction, scheduleToCloseTimeout, retryTimeout)
s.Worker().RegisterWorkflow(workflowFn)
s.Worker().RegisterActivity(activityFunction)
workflowOptions := sdkclient.StartWorkflowOptions{
ID: s.tv.WorkflowID(),
TaskQueue: s.TaskQueue(),
}
workflowRun, err := s.SdkClient().ExecuteWorkflow(ctx, workflowOptions, workflowFn)
s.NoError(err)
// wait for activity to start (and fail)
s.EventuallyWithT(func(t *assert.CollectT) {
description, err := s.SdkClient().DescribeWorkflowExecution(ctx, workflowRun.GetID(), workflowRun.GetRunID())
assert.NoError(t, err)
if err != nil {
assert.Len(t, description.GetPendingActivities(), 1)
assert.Equal(t, int32(1), startedActivityCount.Load())
}
}, 2*time.Second, 200*time.Millisecond)
// update schedule_to_close_timeout
updateRequest := &workflowservice.UpdateActivityOptionsByIdRequest{
Namespace: s.Namespace().String(),
WorkflowId: workflowRun.GetID(),
ActivityId: "activity-id",
ActivityOptions: &activitypb.ActivityOptions{
ScheduleToCloseTimeout: durationpb.New(1 * time.Second),
},
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"schedule_to_close_timeout"}},
}
resp, err := s.FrontendClient().UpdateActivityOptionsById(ctx, updateRequest)
s.NoError(err)
s.NotNil(resp)
// activity should fail immediately
s.EventuallyWithT(func(t *assert.CollectT) {
description, err := s.SdkClient().DescribeWorkflowExecution(ctx, workflowRun.GetID(), workflowRun.GetRunID())
assert.NoError(t, err)
if err != nil {
assert.Len(t, description.GetPendingActivities(), 0)
assert.Equal(t, int32(1), startedActivityCount.Load())
}
}, 2*time.Second, 200*time.Millisecond)
var out string
err = workflowRun.Get(ctx, &out)
var activityError *temporal.ActivityError
s.True(errors.As(err, &activityError))
s.Equal(enumspb.RETRY_STATE_NON_RETRYABLE_FAILURE, activityError.RetryState())
var timeoutError *temporal.TimeoutError
s.True(errors.As(activityError.Unwrap(), &timeoutError))
s.Equal(enumspb.TIMEOUT_TYPE_SCHEDULE_TO_CLOSE, timeoutError.TimeoutType())
s.Equal(int32(1), startedActivityCount.Load())
}
func (s *ActivityApiUpdateClientTestSuite) TestActivityUpdateApi_ChangeScheduleToCloseAndRetry() {
// change both schedule to close and retry policy
// initial values are chosen in such a way that activity will fail due to schedule to close timeout
// we change schedule to close to a longer value and retry policy to a shorter value
// after that activity should succeed
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var startedActivityCount atomic.Int32
activityFunction := func() (string, error) {
startedActivityCount.Add(1)
if startedActivityCount.Load() == 1 {
activityErr := errors.New("bad-luck-please-retry")
return "", activityErr
}
return "done!", nil
}
// make scheduleToClose shorter than retry 2nd retry interval
scheduleToCloseTimeout := 8 * time.Second
retryInterval := 5 * time.Second
workflowFn := s.makeWorkflowFunc(
activityFunction, scheduleToCloseTimeout, retryInterval)
s.Worker().RegisterWorkflow(workflowFn)
s.Worker().RegisterActivity(activityFunction)
workflowOptions := sdkclient.StartWorkflowOptions{
ID: s.tv.WorkflowID(),
TaskQueue: s.TaskQueue(),
}
workflowRun, err := s.SdkClient().ExecuteWorkflow(ctx, workflowOptions, workflowFn)
s.NoError(err)
// wait for activity to start (and fail)
s.EventuallyWithT(func(t *assert.CollectT) {
assert.True(t, startedActivityCount.Load() > 0)
}, 2*time.Second, 200*time.Millisecond)
// update schedule_to_close_timeout, make it longer
// also update retry policy interval, make it shorter
newScheduleToCloseTimeout := 10 * time.Second
updateRequest := &workflowservice.UpdateActivityOptionsByIdRequest{
Namespace: s.Namespace().String(),
WorkflowId: workflowRun.GetID(),
ActivityId: "activity-id",
ActivityOptions: &activitypb.ActivityOptions{
ScheduleToCloseTimeout: durationpb.New(newScheduleToCloseTimeout),
RetryPolicy: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(1 * time.Second),
},
},
UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"schedule_to_close_timeout", "retry_policy.initial_interval"}},
}
resp, err := s.FrontendClient().UpdateActivityOptionsById(ctx, updateRequest)
s.NoError(err)
s.NotNil(resp)
// check that the update was successful
s.Equal(int64(newScheduleToCloseTimeout.Seconds()), resp.GetActivityOptions().ScheduleToCloseTimeout.GetSeconds())
// check that field we didn't update is the same
s.Equal(int64(scheduleToCloseTimeout.Seconds()), resp.GetActivityOptions().StartToCloseTimeout.GetSeconds())
// now activity should succeed
s.EventuallyWithT(func(t *assert.CollectT) {
description, err := s.SdkClient().DescribeWorkflowExecution(ctx, workflowRun.GetID(), workflowRun.GetRunID())
assert.NoError(t, err)
assert.Len(t, description.GetPendingActivities(), 0)
assert.Equal(t, int32(2), startedActivityCount.Load())
}, 5*time.Second, 200*time.Millisecond)
var out string
err = workflowRun.Get(ctx, &out)
s.NoError(err)
}