Skip to content

Commit

Permalink
fix: add validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mazyu36 committed Jun 18, 2024
1 parent 0174c8b commit 16e9e26
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/aws-cdk-lib/aws-synthetics/lib/canary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,11 @@ export class Canary extends cdk.Resource implements ec2.IConnectable {

let timeoutInSeconds: number | undefined;
if (!cdk.Token.isUnresolved(props.timeout) && props.timeout !== undefined) {
const timeoutInMillis = props.timeout?.toMilliseconds();
if (timeoutInMillis % 1000 !== 0) {
throw new Error(`\`timeout\` must be set as an integer representing seconds, got ${timeoutInMillis} milliseconds.`);
}

timeoutInSeconds = props.timeout.toSeconds();
if (timeoutInSeconds < 3 || timeoutInSeconds > 840) {
throw new Error(`\`timeout\` must be between 3 seconds and 840 seconds, got ${timeoutInSeconds} seconds.`);
Expand Down
16 changes: 16 additions & 0 deletions packages/aws-cdk-lib/aws-synthetics/test/canary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,22 @@ test('timeout can be set', () => {
});
});

test.each([100, 3100])('throws when timeout is not set as an integer representing seconds , %d milliseconds', (milliseconds: number) => {
// GIVEN
const stack = new Stack();

// WHEN
expect(() => new synthetics.Canary(stack, 'Canary', {
runtime: synthetics.Runtime.SYNTHETICS_NODEJS_PUPPETEER_7_0,
test: synthetics.Test.custom({
handler: 'index.handler',
code: synthetics.Code.fromInline('/* Synthetics handler code */'),
}),
timeout: Duration.millis(milliseconds),
}))
.toThrow(`\`timeout\` property must be set as an integer representing seconds, got ${milliseconds} milliseconds.`);
});

test.each([2, 900])('throws when timeout is out of range, %d seconds', (seconds: number) => {
// GIVEN
const stack = new Stack();
Expand Down

0 comments on commit 16e9e26

Please sign in to comment.