Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(scheduler-alpha): increase unit test coverage #32033

Merged
merged 4 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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