From 65cdc63526b65a13e2dc8d18b1ab96a71a8d17f5 Mon Sep 17 00:00:00 2001 From: Cory Hall <43035978+corymhall@users.noreply.github.com> Date: Fri, 21 Jan 2022 18:03:47 -0500 Subject: [PATCH] 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* --- .../test/api/logs/logs-monitor.test.ts | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/packages/aws-cdk/test/api/logs/logs-monitor.test.ts b/packages/aws-cdk/test/api/logs/logs-monitor.test.ts index becb3d97dfbf3..930e6427dbab7 100644 --- a/packages/aws-cdk/test/api/logs/logs-monitor.test.ts +++ b/packages/aws-cdk/test/api/logs/logs-monitor.test.ts @@ -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', }; }, @@ -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(), }; }