Skip to content

Commit f748763

Browse files
keeganirbyKeegan Irby
authored andcommitted
test(cwl): Add unit test for starting and stopping LiveTailSession object. (aws#6110)
## Problem The `LiveTailSession` object's start/stop methods aren't directly under test. TailLogGroup tests are mocking their behavior. TailLogGroup command tests are more focused on managing the session registry, opening/writing to the document, and the close tab behaviors. ## Solution Write a test that's scoped to creating a `session` and calling start and stop on it. Assert that timers are handled/disposed of properly, and that the session response stream is returned. Addresses [this comment](aws#6095 (comment)). --- <!--- REMINDER: Ensure that your PR meets the guidelines in CONTRIBUTING.md --> License: I confirm that my contribution is made under the terms of the Apache 2.0 license. --------- Co-authored-by: Keegan Irby <[email protected]>
1 parent 7a0d456 commit f748763

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

packages/core/src/awsService/cloudWatchLogs/registry/liveTailSession.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class LiveTailSession {
9393
if (!commandOutput.responseStream) {
9494
throw new ToolkitError('LiveTail session response stream is undefined.')
9595
}
96-
this.startTime = Date.now()
96+
this.startTime = globals.clock.Date.now()
9797
this.endTime = undefined
9898
this.statusBarUpdateTimer = globals.clock.setInterval(() => {
9999
this.updateStatusBarItemText()
@@ -102,7 +102,7 @@ export class LiveTailSession {
102102
}
103103

104104
public stopLiveTailSession() {
105-
this.endTime = Date.now()
105+
this.endTime = globals.clock.Date.now()
106106
this.statusBarItem.dispose()
107107
globals.clock.clearInterval(this.statusBarUpdateTimer)
108108
this.liveTailClient.abortController.abort()
@@ -116,7 +116,7 @@ export class LiveTailSession {
116116
}
117117
//Currently running
118118
if (this.endTime === undefined) {
119-
return Date.now() - this.startTime
119+
return globals.clock.Date.now() - this.startTime
120120
}
121121
return this.endTime - this.startTime
122122
}

packages/core/src/test/awsService/cloudWatchLogs/registry/liveTailSession.test.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,44 @@
22
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
* SPDX-License-Identifier: Apache-2.0
44
*/
5+
import * as sinon from 'sinon'
6+
import * as FakeTimers from '@sinonjs/fake-timers'
57
import assert from 'assert'
68
import { LiveTailSession } from '../../../../awsService/cloudWatchLogs/registry/liveTailSession'
7-
import { StartLiveTailCommand } from '@aws-sdk/client-cloudwatch-logs'
9+
import {
10+
CloudWatchLogsClient,
11+
StartLiveTailCommand,
12+
StartLiveTailResponseStream,
13+
} from '@aws-sdk/client-cloudwatch-logs'
814
import { LogStreamFilterResponse } from '../../../../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu'
15+
import { installFakeClock } from '../../../testUtil'
916

1017
describe('LiveTailSession', async function () {
1118
const testLogGroupArn = 'arn:aws:test-log-group'
1219
const testRegion = 'test-region'
1320
const testFilter = 'test-filter'
1421
const testAwsCredentials = {} as any as AWS.Credentials
1522

23+
let sandbox: sinon.SinonSandbox
24+
let clock: FakeTimers.InstalledClock
25+
26+
before(function () {
27+
clock = installFakeClock()
28+
})
29+
30+
beforeEach(function () {
31+
clock.reset()
32+
sandbox = sinon.createSandbox()
33+
})
34+
35+
after(function () {
36+
clock.uninstall()
37+
})
38+
39+
afterEach(function () {
40+
sandbox.restore()
41+
})
42+
1643
it('builds StartLiveTailCommand: no stream Filter, no event filter.', function () {
1744
const session = buildLiveTailSession({ type: 'all' }, undefined)
1845
assert.deepStrictEqual(
@@ -65,6 +92,36 @@ describe('LiveTailSession', async function () {
6592
)
6693
})
6794

95+
it('closes a started session', async function () {
96+
const startLiveTailStub = sinon.stub(CloudWatchLogsClient.prototype, 'send').callsFake(function () {
97+
return {
98+
responseStream: mockResponseStream(),
99+
}
100+
})
101+
const session = buildLiveTailSession({ type: 'all' }, testFilter)
102+
assert.strictEqual(session.getLiveTailSessionDuration(), 0)
103+
104+
const returnedResponseStream = await session.startLiveTailSession()
105+
assert.strictEqual(startLiveTailStub.calledOnce, true)
106+
const requestArgs = startLiveTailStub.getCall(0).args
107+
assert.deepEqual(requestArgs[0].input, session.buildStartLiveTailCommand().input)
108+
assert.strictEqual(requestArgs[1].abortSignal !== undefined && !requestArgs[1].abortSignal.aborted, true)
109+
assert.strictEqual(session.isAborted, false)
110+
assert.strictEqual(clock.countTimers(), 1)
111+
assert.deepStrictEqual(returnedResponseStream, mockResponseStream())
112+
113+
clock.tick(1000)
114+
assert.strictEqual(session.getLiveTailSessionDuration(), 1000)
115+
116+
session.stopLiveTailSession()
117+
assert.strictEqual(session.isAborted, true)
118+
assert.strictEqual(clock.countTimers(), 0)
119+
120+
//Session is stopped; ticking the clock forward should not change the session duration
121+
clock.tick(1000)
122+
assert.strictEqual(session.getLiveTailSessionDuration(), 1000)
123+
})
124+
68125
function buildLiveTailSession(
69126
logStreamFilter: LogStreamFilterResponse,
70127
logEventFilterPattern: string | undefined
@@ -77,4 +134,16 @@ describe('LiveTailSession', async function () {
77134
awsCredentials: testAwsCredentials,
78135
})
79136
}
137+
138+
async function* mockResponseStream(): AsyncIterable<StartLiveTailResponseStream> {
139+
const frame: StartLiveTailResponseStream = {
140+
sessionUpdate: {
141+
sessionMetadata: {
142+
sampled: false,
143+
},
144+
sessionResults: [],
145+
},
146+
}
147+
yield frame
148+
}
80149
})

0 commit comments

Comments
 (0)