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(cli): LogMonitor test fails randomly due to Date.now() #18601

Merged
merged 4 commits into from
Jan 21, 2022
Merged
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
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(),
};
}