1
1
import '@aws-cdk/assert/jest' ;
2
+ import { Metric } from '@aws-cdk/aws-cloudwatch' ;
2
3
import * as iam from '@aws-cdk/aws-iam' ;
3
4
import * as cdk from '@aws-cdk/core' ;
4
5
import * as sfn from '../lib' ;
5
6
6
7
describe ( 'Task base' , ( ) => {
7
- test ( 'instantiate a concrete implementation' , ( ) => {
8
- // GIVEN
9
- const stack = new cdk . Stack ( ) ;
8
+ let stack : cdk . Stack ;
9
+ let task : sfn . TaskStateBase ;
10
10
11
+ beforeEach ( ( ) => {
12
+ // GIVEN
13
+ stack = new cdk . Stack ( ) ;
14
+ task = new FakeTask ( stack , 'my-task' , {
15
+ metrics : {
16
+ metricPrefixPlural : '' ,
17
+ metricPrefixSingular : '' ,
18
+ } ,
19
+ } ) ;
20
+ } ) ;
21
+ test ( 'instantiate a concrete implementation with properties' , ( ) => {
11
22
// WHEN
12
- const task = new ConcreteTask ( stack , 'my-task' , {
23
+ task = new FakeTask ( stack , 'my-exciting -task' , {
13
24
comment : 'my exciting task' ,
14
25
heartbeat : cdk . Duration . seconds ( 10 ) ,
15
26
timeout : cdk . Duration . minutes ( 10 ) ,
16
27
} ) ;
17
28
18
29
// THEN
19
30
expect ( render ( task ) ) . toEqual ( {
20
- StartAt : 'my-task' ,
31
+ StartAt : 'my-exciting- task' ,
21
32
States : {
22
- 'my-task' : {
33
+ 'my-exciting- task' : {
23
34
End : true ,
24
35
Type : 'Task' ,
25
36
Comment : 'my exciting task' ,
@@ -34,8 +45,6 @@ describe('Task base', () => {
34
45
35
46
test ( 'add catch configuration' , ( ) => {
36
47
// GIVEN
37
- const stack = new cdk . Stack ( ) ;
38
- const task = new ConcreteTask ( stack , 'my-task' ) ;
39
48
const failure = new sfn . Fail ( stack , 'failed' , {
40
49
error : 'DidNotWork' ,
41
50
cause : 'We got stuck' ,
@@ -68,10 +77,6 @@ describe('Task base', () => {
68
77
} ) ;
69
78
70
79
test ( 'add retry configuration' , ( ) => {
71
- // GIVEN
72
- const stack = new cdk . Stack ( ) ;
73
- const task = new ConcreteTask ( stack , 'my-task' ) ;
74
-
75
80
// WHEN
76
81
task . addRetry ( { errors : [ 'HTTPError' ] , maxAttempts : 2 } )
77
82
. addRetry ( ) ; // adds default retry
@@ -100,270 +105,107 @@ describe('Task base', () => {
100
105
} ) ;
101
106
102
107
test ( 'add a next state to the task in the chain' , ( ) => {
103
- // GIVEN
104
- const stack = new cdk . Stack ( ) ;
105
- const task = new ConcreteTask ( stack , 'mytask' ) ;
106
-
107
108
// WHEN
108
109
task . next ( new sfn . Pass ( stack , 'passState' ) ) ;
109
110
110
111
// THEN
111
112
expect ( render ( task ) ) . toEqual ( {
112
- StartAt : 'mytask ' ,
113
+ StartAt : 'my-task ' ,
113
114
States : {
114
- mytask : {
115
+ 'my-task' : {
115
116
Next : 'passState' ,
116
117
Type : 'Task' ,
117
118
Resource : 'my-resource' ,
118
119
Parameters : { MyParameter : 'myParameter' } ,
119
120
} ,
120
- passState : { Type : 'Pass' , End : true } ,
121
+ ' passState' : { Type : 'Pass' , End : true } ,
121
122
} ,
122
123
} ) ;
123
124
} ) ;
124
125
125
126
test ( 'get named metric for this task' , ( ) => {
126
- // GIVEN
127
- const stack = new cdk . Stack ( ) ;
128
- const task = new ConcreteTask ( stack , 'mytask' , { } ) ;
129
-
130
127
// WHEN
131
- const metric = stack . resolve ( task . metric ( 'my metric' ) ) ;
128
+ const metric = stack . resolve ( task . metric ( 'my- metric' ) ) ;
132
129
133
130
// THEN
134
- expect ( metric ) . toStrictEqual ( {
135
- period : {
136
- amount : 5 ,
137
- unit : {
138
- label : 'minutes' ,
139
- inMillis : 60000 ,
140
- } ,
141
- } ,
142
- namespace : 'AWS/States' ,
143
- metricName : 'my metric' ,
144
- statistic : 'Sum' } ) ;
131
+ verifyMetric ( metric , 'my-metric' , 'Sum' ) ;
145
132
} ) ;
146
133
147
134
test ( 'add metric for task state run time' , ( ) => {
148
- // GIVEN
149
- const stack = new cdk . Stack ( ) ;
150
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixSingular : '' } ;
151
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
152
-
153
135
// WHEN
154
136
const metric = stack . resolve ( task . metricRunTime ( ) ) ;
155
137
156
138
// THEN
157
- expect ( metric ) . toStrictEqual ( {
158
- period : {
159
- amount : 5 ,
160
- unit : {
161
- label : 'minutes' ,
162
- inMillis : 60000 ,
163
- } ,
164
- } ,
165
- namespace : 'AWS/States' ,
166
- metricName : 'RunTime' ,
167
- statistic : 'Average' ,
168
- } ) ;
139
+ verifyMetric ( metric , 'RunTime' , 'Average' ) ;
169
140
} ) ;
170
141
171
142
test ( 'add metric for task schedule time' , ( ) => {
172
- // GIVEN
173
- const stack = new cdk . Stack ( ) ;
174
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixSingular : '' } ;
175
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
176
-
177
143
// WHEN
178
144
const metric = stack . resolve ( task . metricScheduleTime ( ) ) ;
179
145
180
146
// THEN
181
- expect ( metric ) . toStrictEqual ( {
182
- period : {
183
- amount : 5 ,
184
- unit : {
185
- label : 'minutes' ,
186
- inMillis : 60000 ,
187
- } ,
188
- } ,
189
- namespace : 'AWS/States' ,
190
- metricName : 'ScheduleTime' ,
191
- statistic : 'Average' ,
192
- } ) ;
147
+ verifyMetric ( metric , 'ScheduleTime' , 'Average' ) ;
193
148
} ) ;
194
149
195
150
test ( 'add metric for time between task being scheduled to closing' , ( ) => {
196
- // GIVEN
197
- const stack = new cdk . Stack ( ) ;
198
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixSingular : '' } ;
199
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
200
-
201
151
// WHEN
202
152
const metric = stack . resolve ( task . metricTime ( ) ) ;
203
153
204
154
// THEN
205
- expect ( metric ) . toStrictEqual ( {
206
- period : {
207
- amount : 5 ,
208
- unit : {
209
- label : 'minutes' ,
210
- inMillis : 60000 ,
211
- } ,
212
- } ,
213
- namespace : 'AWS/States' ,
214
- metricName : 'Time' ,
215
- statistic : 'Average' ,
216
- } ) ;
155
+ verifyMetric ( metric , 'Time' , 'Average' ) ;
217
156
} ) ;
218
157
219
158
test ( 'add metric for number of times the task is scheduled' , ( ) => {
220
- // GIVEN
221
- const stack = new cdk . Stack ( ) ;
222
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixPlural : '' } ;
223
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
224
-
225
159
// WHEN
226
160
const metric = stack . resolve ( task . metricScheduled ( ) ) ;
227
161
228
162
// THEN
229
- expect ( metric ) . toStrictEqual ( {
230
- period : {
231
- amount : 5 ,
232
- unit : {
233
- label : 'minutes' ,
234
- inMillis : 60000 ,
235
- } ,
236
- } ,
237
- namespace : 'AWS/States' ,
238
- metricName : 'Scheduled' ,
239
- statistic : 'Sum' ,
240
- } ) ;
163
+ verifyMetric ( metric , 'Scheduled' , 'Sum' ) ;
241
164
} ) ;
242
165
243
166
test ( 'add metric for number of times the task times out' , ( ) => {
244
- // GIVEN
245
- const stack = new cdk . Stack ( ) ;
246
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixPlural : '' } ;
247
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
248
-
249
167
// WHEN
250
168
const metric = stack . resolve ( task . metricTimedOut ( ) ) ;
251
169
252
170
// THEN
253
- expect ( metric ) . toStrictEqual ( {
254
- period : {
255
- amount : 5 ,
256
- unit : {
257
- label : 'minutes' ,
258
- inMillis : 60000 ,
259
- } ,
260
- } ,
261
- namespace : 'AWS/States' ,
262
- metricName : 'TimedOut' ,
263
- statistic : 'Sum' ,
264
- } ) ;
171
+ verifyMetric ( metric , 'TimedOut' , 'Sum' ) ;
265
172
} ) ;
266
173
267
174
test ( 'add metric for number of times the task was started' , ( ) => {
268
- // GIVEN
269
- const stack = new cdk . Stack ( ) ;
270
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixPlural : '' } ;
271
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
272
-
273
175
// WHEN
274
176
const metric = stack . resolve ( task . metricStarted ( ) ) ;
275
177
276
178
// THEN
277
- expect ( metric ) . toStrictEqual ( {
278
- period : {
279
- amount : 5 ,
280
- unit : {
281
- label : 'minutes' ,
282
- inMillis : 60000 ,
283
- } ,
284
- } ,
285
- namespace : 'AWS/States' ,
286
- metricName : 'Started' ,
287
- statistic : 'Sum' ,
288
- } ) ;
179
+ verifyMetric ( metric , 'Started' , 'Sum' ) ;
289
180
} ) ;
290
181
291
182
test ( 'add metric for number of times the task succeeded' , ( ) => {
292
- // GIVEN
293
- const stack = new cdk . Stack ( ) ;
294
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixPlural : '' } ;
295
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
296
-
297
183
// WHEN
298
184
const metric = stack . resolve ( task . metricSucceeded ( ) ) ;
299
185
300
186
// THEN
301
- expect ( metric ) . toStrictEqual ( {
302
- period : {
303
- amount : 5 ,
304
- unit : {
305
- label : 'minutes' ,
306
- inMillis : 60000 ,
307
- } ,
308
- } ,
309
- namespace : 'AWS/States' ,
310
- metricName : 'Succeeded' ,
311
- statistic : 'Sum' ,
312
- } ) ;
187
+ verifyMetric ( metric , 'Succeeded' , 'Sum' ) ;
313
188
} ) ;
314
189
315
190
test ( 'add metric for number of times the task failed' , ( ) => {
316
- // GIVEN
317
- const stack = new cdk . Stack ( ) ;
318
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixPlural : '' } ;
319
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
320
-
321
191
// WHEN
322
192
const metric = stack . resolve ( task . metricFailed ( ) ) ;
323
193
324
194
// THEN
325
- expect ( metric ) . toStrictEqual ( {
326
- period : {
327
- amount : 5 ,
328
- unit : {
329
- label : 'minutes' ,
330
- inMillis : 60000 ,
331
- } ,
332
- } ,
333
- namespace : 'AWS/States' ,
334
- metricName : 'Failed' ,
335
- statistic : 'Sum' ,
336
- } ) ;
195
+ verifyMetric ( metric , 'Failed' , 'Sum' ) ;
337
196
} ) ;
338
197
339
198
test ( 'add metric for number of times the metrics heartbeat timed out' , ( ) => {
340
- // GIVEN
341
- const stack = new cdk . Stack ( ) ;
342
- const taskMetrics : sfn . TaskMetricsConfig = { metricPrefixPlural : '' } ;
343
- const task = new ConcreteTask ( stack , 'mytask' , { metrics : taskMetrics } ) ;
344
-
345
199
// WHEN
346
200
const metric = stack . resolve ( task . metricHeartbeatTimedOut ( ) ) ;
347
201
348
202
// THEN
349
- expect ( metric ) . toStrictEqual ( {
350
- period : {
351
- amount : 5 ,
352
- unit : {
353
- label : 'minutes' ,
354
- inMillis : 60000 ,
355
- } ,
356
- } ,
357
- namespace : 'AWS/States' ,
358
- metricName : 'HeartbeatTimedOut' ,
359
- statistic : 'Sum' ,
360
- } ) ;
203
+ verifyMetric ( metric , 'HeartbeatTimedOut' , 'Sum' ) ;
361
204
} ) ;
362
205
363
206
test ( 'metrics must be configured to use metric* APIs' , ( ) => {
364
207
// GIVEN
365
- const stack = new cdk . Stack ( ) ;
366
- const task = new ConcreteTask ( stack , 'mytask' , { } ) ;
208
+ task = new FakeTask ( stack , 'mytask' , { } ) ;
367
209
368
210
// THEN
369
211
expect ( ( ) => {
@@ -422,21 +264,36 @@ describe('Task base', () => {
422
264
} ) ;
423
265
} ) ;
424
266
267
+ function verifyMetric ( metric : Metric , metricName : string , statistic : string ) {
268
+ expect ( metric ) . toStrictEqual ( {
269
+ period : {
270
+ amount : 5 ,
271
+ unit : {
272
+ label : 'minutes' ,
273
+ inMillis : 60000 ,
274
+ } ,
275
+ } ,
276
+ namespace : 'AWS/States' ,
277
+ metricName,
278
+ statistic,
279
+ } ) ;
280
+ }
281
+
425
282
function render ( sm : sfn . IChainable ) {
426
283
return new cdk . Stack ( ) . resolve (
427
284
new sfn . StateGraph ( sm . startState , 'Test Graph' ) . toGraphJson ( ) ,
428
285
) ;
429
286
}
430
287
431
- interface ConcreteTaskProps extends sfn . TaskStateBaseProps {
288
+ interface FakeTaskProps extends sfn . TaskStateBaseProps {
432
289
readonly metrics ?: sfn . TaskMetricsConfig ;
433
290
}
434
291
435
- class ConcreteTask extends sfn . TaskStateBase {
292
+ class FakeTask extends sfn . TaskStateBase {
436
293
protected readonly taskMetrics ?: sfn . TaskMetricsConfig ;
437
294
protected readonly taskPolicies ?: iam . PolicyStatement [ ] ;
438
295
439
- constructor ( scope : cdk . Construct , id : string , props : ConcreteTaskProps = { } ) {
296
+ constructor ( scope : cdk . Construct , id : string , props : FakeTaskProps = { } ) {
440
297
super ( scope , id , props ) ;
441
298
this . taskMetrics = props . metrics ;
442
299
}
0 commit comments