Skip to content

Commit 65cdc63

Browse files
authored
chore(cli): LogMonitor test fails randomly due to Date.now() (#18601)
The logs monitor test was using `Date.now()` which sometimes caused the actual result to differ from the expected result. Turns out the reason I needed the dynamic date is because I was generating the timestamp incorrectly. Fixed that which allowed me to use a hardcoded timestamp. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 11fc610 commit 65cdc63

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

Diff for: packages/aws-cdk/test/api/logs/logs-monitor.test.ts

+8-14
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@ afterAll(() => {
1717
monitor.deactivate();
1818
});
1919

20-
let TIMESTAMP: number;
21-
let HUMAN_TIME: string;
22-
23-
beforeAll(() => {
24-
TIMESTAMP = new Date().getTime();
25-
HUMAN_TIME = new Date(TIMESTAMP).toLocaleTimeString();
26-
});
27-
2820
test('continue to the next page if it exists', async () => {
2921
// GIVEN
22+
const eventDate = new Date(T0 + 102 * 1000);
3023
sdk.stubCloudWatchLogs({
3124
filterLogEvents() {
3225
return {
33-
events: [event(102, 'message')],
26+
events: [event(102, 'message', eventDate)],
3427
nextToken: 'some-token',
3528
};
3629
},
@@ -50,22 +43,23 @@ test('continue to the next page if it exists', async () => {
5043
await sleep(1000);
5144

5245
// THEN
46+
const expectedLocaleTimeString = eventDate.toLocaleTimeString();
5347
expect(stderrMock).toHaveBeenCalledTimes(2);
5448
expect(stderrMock.mock.calls[0][0]).toContain(
55-
`[${blue('loggroup')}] ${yellow(HUMAN_TIME)} message`,
49+
`[${blue('loggroup')}] ${yellow(expectedLocaleTimeString)} message`,
5650
);
5751
expect(stderrMock.mock.calls[1][0]).toContain(
58-
`[${blue('loggroup')}] ${yellow(new Date(T100).toLocaleTimeString())} >>> \`watch\` shows only the first 100 log messages - the rest have been truncated...`,
52+
`[${blue('loggroup')}] ${yellow(expectedLocaleTimeString)} >>> \`watch\` shows only the first 100 log messages - the rest have been truncated...`,
5953
);
6054
});
6155

6256
const T0 = 1597837230504;
6357
const T100 = T0 + 100 * 1000;
64-
function event(nr: number, message: string): AWS.CloudWatchLogs.FilteredLogEvent {
58+
function event(nr: number, message: string, timestamp: Date): AWS.CloudWatchLogs.FilteredLogEvent {
6559
return {
6660
eventId: `${nr}`,
6761
message,
68-
timestamp: new Date(T0 * nr * 1000).getTime(),
69-
ingestionTime: new Date(T0 * nr * 1000).getTime(),
62+
timestamp: timestamp.getTime(),
63+
ingestionTime: timestamp.getTime(),
7064
};
7165
}

0 commit comments

Comments
 (0)