Skip to content

Commit

Permalink
chore(cli): LogMonitor test fails randomly due to Date.now() (#18601)
Browse files Browse the repository at this point in the history
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*
  • Loading branch information
corymhall authored Jan 21, 2022
1 parent 11fc610 commit 65cdc63
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions packages/aws-cdk/test/api/logs/logs-monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,13 @@ afterAll(() => {
monitor.deactivate();
});

let TIMESTAMP: number;
let HUMAN_TIME: string;

beforeAll(() => {
TIMESTAMP = new Date().getTime();
HUMAN_TIME = new Date(TIMESTAMP).toLocaleTimeString();
});

test('continue to the next page if it exists', async () => {
// GIVEN
const eventDate = new Date(T0 + 102 * 1000);
sdk.stubCloudWatchLogs({
filterLogEvents() {
return {
events: [event(102, 'message')],
events: [event(102, 'message', eventDate)],
nextToken: 'some-token',
};
},
Expand All @@ -50,22 +43,23 @@ test('continue to the next page if it exists', async () => {
await sleep(1000);

// THEN
const expectedLocaleTimeString = eventDate.toLocaleTimeString();
expect(stderrMock).toHaveBeenCalledTimes(2);
expect(stderrMock.mock.calls[0][0]).toContain(
`[${blue('loggroup')}] ${yellow(HUMAN_TIME)} message`,
`[${blue('loggroup')}] ${yellow(expectedLocaleTimeString)} message`,
);
expect(stderrMock.mock.calls[1][0]).toContain(
`[${blue('loggroup')}] ${yellow(new Date(T100).toLocaleTimeString())} >>> \`watch\` shows only the first 100 log messages - the rest have been truncated...`,
`[${blue('loggroup')}] ${yellow(expectedLocaleTimeString)} >>> \`watch\` shows only the first 100 log messages - the rest have been truncated...`,
);
});

const T0 = 1597837230504;
const T100 = T0 + 100 * 1000;
function event(nr: number, message: string): AWS.CloudWatchLogs.FilteredLogEvent {
function event(nr: number, message: string, timestamp: Date): AWS.CloudWatchLogs.FilteredLogEvent {
return {
eventId: `${nr}`,
message,
timestamp: new Date(T0 * nr * 1000).getTime(),
ingestionTime: new Date(T0 * nr * 1000).getTime(),
timestamp: timestamp.getTime(),
ingestionTime: timestamp.getTime(),
};
}

0 comments on commit 65cdc63

Please sign in to comment.