Skip to content

Commit

Permalink
chore(scheduler-alpha): increase unit test coverage (#32033)
Browse files Browse the repository at this point in the history
### 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*
  • Loading branch information
gracelu0 authored Nov 6, 2024
1 parent 1f39cb9 commit dd61d12
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-scheduler-alpha/lib/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ abstract class GroupBase extends Resource implements IGroup {
*
* @default - sum over 5 minutes
*/
metricSentToDLQ(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
public metricSentToDLQ(props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return this.metric('InvocationsSentToDeadLetterCount', props);
}

Expand Down
55 changes: 51 additions & 4 deletions packages/@aws-cdk/aws-scheduler-alpha/test/group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,33 @@ describe('Schedule Group', () => {
},
});
});
});

test('Target Error Metrics', () => {
describe('Schedule Group Metrics', () => {
test.each([
['metricTargetErrors', 'TargetErrorCount'],
['metricThrottled', 'InvocationThrottleCount'],
['metricAttempts', 'InvocationAttemptCount'],
['metricTargetThrottled', 'TargetErrorThrottledCount'],
['metricDropped', 'InvocationDroppedCount'],
['metricSentToDLQ', 'InvocationsSentToDeadLetterCount'],
['metricSentToDLQTruncated', 'InvocationsSentToDeadLetterCount_Truncated_MessageSizeExceeded'],
])('calling %s creates alarm for %s metric', (metricMethodName, metricName) => {
// GIVEN
const app = new App();
const props: GroupProps = {
groupName: 'MyGroup',
};
const stack = new Stack(app, 'Stack', { env: { region: 'us-east-1', account: '123456789012' } });
const group = new Group(stack, 'TestGroup', props);

// WHEN
const metricTargetErrors = group.metricTargetErrors({
const metricMethod = (group as any)[metricMethodName].bind(group); // Get the method dynamically
const metricTargetErrors = metricMethod({
period: Duration.minutes(1),
});

new cw.Alarm(stack, 'GroupTargetErrorAlarm', {
new cw.Alarm(stack, `Group${metricName}Alarm`, {
metric: metricTargetErrors,
evaluationPeriods: 1,
threshold: 1,
Expand All @@ -306,7 +319,41 @@ describe('Schedule Group', () => {
Value: 'MyGroup',
}),
]),
MetricName: 'TargetErrorCount',
MetricName: metricName,
Namespace: 'AWS/Scheduler',
});
});

test('Invocations Failed to Deliver to DLQ Metrics', () => {
// GIVEN
const app = new App();
const props: GroupProps = {
groupName: 'MyGroup',
};
const stack = new Stack(app, 'Stack', { env: { region: 'us-east-1', account: '123456789012' } });
const group = new Group(stack, 'TestGroup', props);
const errorCode = '403';

// WHEN
const metricFailedToBeSentToDLQ = group.metricFailedToBeSentToDLQ(errorCode, {
period: Duration.minutes(1),
});

new cw.Alarm(stack, 'GroupFailedInvocationsToDLQAlarm', {
metric: metricFailedToBeSentToDLQ,
evaluationPeriods: 1,
threshold: 1,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::CloudWatch::Alarm', {
Dimensions: Match.arrayWith([
Match.objectLike({
Name: 'ScheduleGroup',
Value: 'MyGroup',
}),
]),
MetricName: `InvocationsFailedToBeSentToDeadLetterCount_${errorCode}`,
Namespace: 'AWS/Scheduler',
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('schedule expression', () => {
});

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

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

test('rate cannot be a fractional amount of minutes (defined with minutes)', () => {
expect(()=> {
ScheduleExpression.rate(Duration.minutes(5/3));
expect(() => {
ScheduleExpression.rate(Duration.minutes(5 / 3));
}).toThrow(/must be a whole number of/);
});

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

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

Expand All @@ -164,4 +164,9 @@ describe('fractional minutes checks', () => {
ScheduleExpression.rate(Duration.minutes(10))
.expressionString);
});
});

test('literal schedule expression', () => {
expect('rate(1 hour)').toEqual(
ScheduleExpression.expression('rate(1 hour)').expressionString);
});
});
27 changes: 24 additions & 3 deletions packages/@aws-cdk/aws-scheduler-alpha/test/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,22 @@ describe('Schedule', () => {
});
});

test('returns metric for delivery of failed invocations to DLQ', () => {
test.each([
['metricAllThrottled', 'InvocationThrottleCount'],
['metricAllErrors', 'TargetErrorCount'],
['metricAllAttempts', 'InvocationAttemptCount'],
['metricAllTargetThrottled', 'TargetErrorThrottledCount'],
['metricAllDropped', 'InvocationDroppedCount'],
['metricAllSentToDLQ', 'InvocationsSentToDeadLetterCount'],
['metricAllSentToDLQTruncated', 'InvocationsSentToDeadLetterCount_Truncated_MessageSizeExceeded'],

])('returns expected metric for %s', (metricMethodName: string, metricName: string) => {
// WHEN
const metric = Schedule.metricAllFailedToBeSentToDLQ();
const metric = (Schedule as any)[metricMethodName]();

// THEN
expect(metric.namespace).toEqual('AWS/Scheduler');
expect(metric.metricName).toEqual('InvocationsFailedToBeSentToDeadLetterCount');
expect(metric.metricName).toEqual(metricName);
expect(metric.dimensions).toBeUndefined();
expect(metric.statistic).toEqual('Sum');
expect(metric.period).toEqual(Duration.minutes(5));
Expand All @@ -88,6 +97,18 @@ describe('Schedule', () => {
expect(metric.period).toEqual(Duration.minutes(5));
});

test('returns metric for delivery of failed invocations to DLQ with no error code', () => {
// WHEN
const metric = Schedule.metricAllFailedToBeSentToDLQ();

// THEN
expect(metric.namespace).toEqual('AWS/Scheduler');
expect(metric.metricName).toEqual('InvocationsFailedToBeSentToDeadLetterCount');
expect(metric.dimensions).toBeUndefined();
expect(metric.statistic).toEqual('Sum');
expect(metric.period).toEqual(Duration.minutes(5));
});

test('returns metric for all errors with provided statistic and period', () => {
// WHEN
const metric = Schedule.metricAllErrors({
Expand Down

0 comments on commit dd61d12

Please sign in to comment.