Skip to content

Commit dd61d12

Browse files
authored
chore(scheduler-alpha): increase unit test coverage (#32033)
### Issue # (if applicable) tracking issue: #31785 ### Reason for this change Satisfy 90% unit test coverage required for developer preview (see https://github.com/cdklabs/team-internal/blob/main/docs/construct-library-lifecycle.md#exit-criteria-1) ### Description of changes Added parameterized unit tests for metric methods Previous coverage summary: ``` =============================== Coverage summary =============================== Statements : 83.06% ( 103/124 ) Branches : 76.19% ( 32/42 ) Functions : 70.9% ( 39/55 ) Lines : 83.47% ( 101/121 ) ================================================================================ ``` Current coverage summary: ``` =============================== Coverage summary =============================== Statements : 95.16% ( 118/124 ) Branches : 83.33% ( 35/42 ) Functions : 96.36% ( 53/55 ) Lines : 95.86% ( 116/121 ) ================================================================================ ``` ### Description of how you validated changes Unit tests pass ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1f39cb9 commit dd61d12

File tree

4 files changed

+87
-14
lines changed

4 files changed

+87
-14
lines changed

packages/@aws-cdk/aws-scheduler-alpha/lib/group.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ abstract class GroupBase extends Resource implements IGroup {
202202
*
203203
* @default - sum over 5 minutes
204204
*/
205-
metricSentToDLQ(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
205+
public metricSentToDLQ(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
206206
return this.metric('InvocationsSentToDeadLetterCount', props);
207207
}
208208

packages/@aws-cdk/aws-scheduler-alpha/test/group.test.ts

+51-4
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,33 @@ describe('Schedule Group', () => {
279279
},
280280
});
281281
});
282+
});
282283

283-
test('Target Error Metrics', () => {
284+
describe('Schedule Group Metrics', () => {
285+
test.each([
286+
['metricTargetErrors', 'TargetErrorCount'],
287+
['metricThrottled', 'InvocationThrottleCount'],
288+
['metricAttempts', 'InvocationAttemptCount'],
289+
['metricTargetThrottled', 'TargetErrorThrottledCount'],
290+
['metricDropped', 'InvocationDroppedCount'],
291+
['metricSentToDLQ', 'InvocationsSentToDeadLetterCount'],
292+
['metricSentToDLQTruncated', 'InvocationsSentToDeadLetterCount_Truncated_MessageSizeExceeded'],
293+
])('calling %s creates alarm for %s metric', (metricMethodName, metricName) => {
284294
// GIVEN
295+
const app = new App();
285296
const props: GroupProps = {
286297
groupName: 'MyGroup',
287298
};
299+
const stack = new Stack(app, 'Stack', { env: { region: 'us-east-1', account: '123456789012' } });
288300
const group = new Group(stack, 'TestGroup', props);
289301

290302
// WHEN
291-
const metricTargetErrors = group.metricTargetErrors({
303+
const metricMethod = (group as any)[metricMethodName].bind(group); // Get the method dynamically
304+
const metricTargetErrors = metricMethod({
292305
period: Duration.minutes(1),
293306
});
294307

295-
new cw.Alarm(stack, 'GroupTargetErrorAlarm', {
308+
new cw.Alarm(stack, `Group${metricName}Alarm`, {
296309
metric: metricTargetErrors,
297310
evaluationPeriods: 1,
298311
threshold: 1,
@@ -306,7 +319,41 @@ describe('Schedule Group', () => {
306319
Value: 'MyGroup',
307320
}),
308321
]),
309-
MetricName: 'TargetErrorCount',
322+
MetricName: metricName,
323+
Namespace: 'AWS/Scheduler',
324+
});
325+
});
326+
327+
test('Invocations Failed to Deliver to DLQ Metrics', () => {
328+
// GIVEN
329+
const app = new App();
330+
const props: GroupProps = {
331+
groupName: 'MyGroup',
332+
};
333+
const stack = new Stack(app, 'Stack', { env: { region: 'us-east-1', account: '123456789012' } });
334+
const group = new Group(stack, 'TestGroup', props);
335+
const errorCode = '403';
336+
337+
// WHEN
338+
const metricFailedToBeSentToDLQ = group.metricFailedToBeSentToDLQ(errorCode, {
339+
period: Duration.minutes(1),
340+
});
341+
342+
new cw.Alarm(stack, 'GroupFailedInvocationsToDLQAlarm', {
343+
metric: metricFailedToBeSentToDLQ,
344+
evaluationPeriods: 1,
345+
threshold: 1,
346+
});
347+
348+
// THEN
349+
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
350+
Dimensions: Match.arrayWith([
351+
Match.objectLike({
352+
Name: 'ScheduleGroup',
353+
Value: 'MyGroup',
354+
}),
355+
]),
356+
MetricName: `InvocationsFailedToBeSentToDeadLetterCount_${errorCode}`,
310357
Namespace: 'AWS/Scheduler',
311358
});
312359
});

packages/@aws-cdk/aws-scheduler-alpha/test/schedule-expression.test.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('schedule expression', () => {
118118
});
119119

120120
test('one-time expression with invalid date throws', () => {
121-
expect(() => ScheduleExpression.at(new Date('13-20-1969'))).toThrowError('Invalid date');
121+
expect(() => ScheduleExpression.at(new Date('13-20-1969'))).toThrow('Invalid date');
122122
});
123123
});
124124

@@ -130,13 +130,13 @@ describe('fractional minutes checks', () => {
130130
});
131131

132132
test('rate cannot be a fractional amount of minutes (defined with minutes)', () => {
133-
expect(()=> {
134-
ScheduleExpression.rate(Duration.minutes(5/3));
133+
expect(() => {
134+
ScheduleExpression.rate(Duration.minutes(5 / 3));
135135
}).toThrow(/must be a whole number of/);
136136
});
137137

138138
test('rate cannot be a fractional amount of minutes (defined with hours)', () => {
139-
expect(()=> {
139+
expect(() => {
140140
ScheduleExpression.rate(Duration.hours(1.03));
141141
}).toThrow(/cannot be converted into a whole number of/);
142142
});
@@ -149,7 +149,7 @@ describe('fractional minutes checks', () => {
149149

150150
test('rate cannot be less than 1 minute (defined with minutes as fractions)', () => {
151151
expect(() => {
152-
ScheduleExpression.rate(Duration.minutes(1/2));
152+
ScheduleExpression.rate(Duration.minutes(1 / 2));
153153
}).toThrow(/must be a whole number of/);
154154
});
155155

@@ -164,4 +164,9 @@ describe('fractional minutes checks', () => {
164164
ScheduleExpression.rate(Duration.minutes(10))
165165
.expressionString);
166166
});
167-
});
167+
168+
test('literal schedule expression', () => {
169+
expect('rate(1 hour)').toEqual(
170+
ScheduleExpression.expression('rate(1 hour)').expressionString);
171+
});
172+
});

packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts

+24-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,22 @@ describe('Schedule', () => {
6464
});
6565
});
6666

67-
test('returns metric for delivery of failed invocations to DLQ', () => {
67+
test.each([
68+
['metricAllThrottled', 'InvocationThrottleCount'],
69+
['metricAllErrors', 'TargetErrorCount'],
70+
['metricAllAttempts', 'InvocationAttemptCount'],
71+
['metricAllTargetThrottled', 'TargetErrorThrottledCount'],
72+
['metricAllDropped', 'InvocationDroppedCount'],
73+
['metricAllSentToDLQ', 'InvocationsSentToDeadLetterCount'],
74+
['metricAllSentToDLQTruncated', 'InvocationsSentToDeadLetterCount_Truncated_MessageSizeExceeded'],
75+
76+
])('returns expected metric for %s', (metricMethodName: string, metricName: string) => {
6877
// WHEN
69-
const metric = Schedule.metricAllFailedToBeSentToDLQ();
78+
const metric = (Schedule as any)[metricMethodName]();
7079

7180
// THEN
7281
expect(metric.namespace).toEqual('AWS/Scheduler');
73-
expect(metric.metricName).toEqual('InvocationsFailedToBeSentToDeadLetterCount');
82+
expect(metric.metricName).toEqual(metricName);
7483
expect(metric.dimensions).toBeUndefined();
7584
expect(metric.statistic).toEqual('Sum');
7685
expect(metric.period).toEqual(Duration.minutes(5));
@@ -88,6 +97,18 @@ describe('Schedule', () => {
8897
expect(metric.period).toEqual(Duration.minutes(5));
8998
});
9099

100+
test('returns metric for delivery of failed invocations to DLQ with no error code', () => {
101+
// WHEN
102+
const metric = Schedule.metricAllFailedToBeSentToDLQ();
103+
104+
// THEN
105+
expect(metric.namespace).toEqual('AWS/Scheduler');
106+
expect(metric.metricName).toEqual('InvocationsFailedToBeSentToDeadLetterCount');
107+
expect(metric.dimensions).toBeUndefined();
108+
expect(metric.statistic).toEqual('Sum');
109+
expect(metric.period).toEqual(Duration.minutes(5));
110+
});
111+
91112
test('returns metric for all errors with provided statistic and period', () => {
92113
// WHEN
93114
const metric = Schedule.metricAllErrors({

0 commit comments

Comments
 (0)