forked from uber/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
219 lines (193 loc) · 7.01 KB
/
util_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
// The MIT License (MIT)
//
// 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 common
import (
"context"
"errors"
"fmt"
"strconv"
"testing"
"time"
"github.com/pborman/uuid"
"github.com/stretchr/testify/require"
"go.uber.org/yarpc/yarpcerrors"
workflow "github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common/types"
)
func TestIsServiceTransientError_ContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
time.Sleep(100 * time.Millisecond)
require.False(t, IsServiceTransientError(ctx.Err()))
}
func TestIsServiceTransientError_YARPCDeadlineExceeded(t *testing.T) {
yarpcErr := yarpcerrors.DeadlineExceededErrorf("yarpc deadline exceeded")
require.False(t, IsServiceTransientError(yarpcErr))
}
func TestIsServiceTransientError_ContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
require.False(t, IsServiceTransientError(ctx.Err()))
}
func TestIsContextTimeoutError(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
time.Sleep(50 * time.Millisecond)
require.True(t, IsContextTimeoutError(ctx.Err()))
require.True(t, IsContextTimeoutError(&types.InternalServiceError{Message: ctx.Err().Error()}))
yarpcErr := yarpcerrors.DeadlineExceededErrorf("yarpc deadline exceeded")
require.True(t, IsContextTimeoutError(yarpcErr))
require.False(t, IsContextTimeoutError(errors.New("some random error")))
ctx, cancel = context.WithCancel(context.Background())
cancel()
require.False(t, IsContextTimeoutError(ctx.Err()))
}
func TestConvertDynamicConfigMapPropertyToIntMap(t *testing.T) {
dcValue := make(map[string]interface{})
for idx, value := range []interface{}{int(0), int32(1), int64(2), float64(3.0)} {
dcValue[strconv.Itoa(idx)] = value
}
intMap, err := ConvertDynamicConfigMapPropertyToIntMap(dcValue)
require.NoError(t, err)
require.Len(t, intMap, 4)
for i := 0; i != 4; i++ {
require.Equal(t, i, intMap[i])
}
}
func TestCreateHistoryStartWorkflowRequest_ExpirationTimeWithCron(t *testing.T) {
domainID := uuid.New()
request := &types.StartWorkflowExecutionRequest{
RetryPolicy: &types.RetryPolicy{
InitialIntervalInSeconds: 60,
ExpirationIntervalInSeconds: 60,
},
CronSchedule: "@every 300s",
}
now := time.Now()
startRequest := CreateHistoryStartWorkflowRequest(domainID, request, now)
require.NotNil(t, startRequest)
expirationTime := startRequest.GetExpirationTimestamp()
require.NotNil(t, expirationTime)
require.True(t, time.Unix(0, expirationTime).Sub(now) > 60*time.Second)
}
func TestCreateHistoryStartWorkflowRequest_DelayStart(t *testing.T) {
testDelayStart(t, 0)
}
func TestCreateHistoryStartWorkflowRequest_DelayStartWithCron(t *testing.T) {
testDelayStart(t, 300)
}
func testDelayStart(t *testing.T, cronSeconds int) {
domainID := uuid.New()
delayStartSeconds := 100
request := &types.StartWorkflowExecutionRequest{
RetryPolicy: &types.RetryPolicy{
InitialIntervalInSeconds: 60,
ExpirationIntervalInSeconds: 60,
},
DelayStartSeconds: Int32Ptr(int32(delayStartSeconds)),
}
if cronSeconds > 0 {
request.CronSchedule = fmt.Sprintf("@every %ds", cronSeconds)
}
minDelay := delayStartSeconds + cronSeconds
maxDelay := delayStartSeconds + 2*cronSeconds
now := time.Now()
startRequest := CreateHistoryStartWorkflowRequest(domainID, request, now)
require.NotNil(t, startRequest)
expirationTime := startRequest.GetExpirationTimestamp()
require.NotNil(t, expirationTime)
// Since we assign the expiration time after we create the workflow request,
// There's a chance that the test thread might sleep or get deprioritized and
// expirationTime - now may not be equal to DelayStartSeconds. Adding 2 seconds
// buffer to avoid this test being flaky
require.True(
t,
time.Unix(0, expirationTime).Sub(now) >= (time.Duration(minDelay)+58)*time.Second,
"Integration test took too short: %f seconds vs %f seconds",
time.Duration(time.Unix(0, expirationTime).Sub(now)).Round(time.Millisecond).Seconds(),
time.Duration((time.Duration(minDelay)+58)*time.Second).Round(time.Millisecond).Seconds(),
)
require.True(
t,
time.Unix(0, expirationTime).Sub(now) < (time.Duration(maxDelay)+68)*time.Second,
"Integration test took too long: %f seconds vs %f seconds",
time.Duration(time.Unix(0, expirationTime).Sub(now)).Round(time.Millisecond).Seconds(),
time.Duration((time.Duration(minDelay)+68)*time.Second).Round(time.Millisecond).Seconds(),
)
}
func TestCreateHistoryStartWorkflowRequest_ExpirationTimeWithoutCron(t *testing.T) {
domainID := uuid.New()
request := &types.StartWorkflowExecutionRequest{
RetryPolicy: &types.RetryPolicy{
InitialIntervalInSeconds: 60,
ExpirationIntervalInSeconds: 60,
},
}
now := time.Now()
startRequest := CreateHistoryStartWorkflowRequest(domainID, request, now)
require.NotNil(t, startRequest)
expirationTime := startRequest.GetExpirationTimestamp()
require.NotNil(t, expirationTime)
delta := time.Unix(0, expirationTime).Sub(now)
require.True(t, delta > 58*time.Second)
require.True(t, delta < 62*time.Second)
}
func TestConvertIndexedValueTypeToThriftType(t *testing.T) {
expected := workflow.IndexedValueType_Values()
for i := 0; i < len(expected); i++ {
require.Equal(t, expected[i], ConvertIndexedValueTypeToThriftType(i, nil))
require.Equal(t, expected[i], ConvertIndexedValueTypeToThriftType(float64(i), nil))
}
}
func TestValidateDomainUUID(t *testing.T) {
testCases := []struct {
msg string
domainUUID string
valid bool
}{
{
msg: "empty",
domainUUID: "",
valid: false,
},
{
msg: "invalid",
domainUUID: "some random uuid",
valid: false,
},
{
msg: "valid",
domainUUID: uuid.New(),
valid: true,
},
}
for _, tc := range testCases {
t.Run(tc.msg, func(t *testing.T) {
err := ValidateDomainUUID(tc.domainUUID)
if tc.valid {
require.NoError(t, err)
} else {
require.Error(t, err)
}
})
}
}