Skip to content

Commit 1ff0a72

Browse files
author
Keegan Irby
committed
Add unit tests for LiveTailSession stop/start
1 parent d0d456d commit 1ff0a72

File tree

2 files changed

+70
-4
lines changed

2 files changed

+70
-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: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,45 @@
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+
StartLiveTailCommandOutput,
13+
StartLiveTailResponseStream,
14+
} from '@aws-sdk/client-cloudwatch-logs'
815
import { LogStreamFilterResponse } from '../../../../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu'
16+
import { installFakeClock } from '../../../testUtil'
917

1018
describe('LiveTailSession', async function () {
1119
const testLogGroupArn = 'arn:aws:test-log-group'
1220
const testRegion = 'test-region'
1321
const testFilter = 'test-filter'
1422
const testAwsCredentials = {} as any as AWS.Credentials
1523

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

96+
it('closes a started session', async function () {
97+
sinon.stub(CloudWatchLogsClient.prototype, 'send').callsFake(function () {
98+
return {
99+
responseStream: mockResponseStream(),
100+
}
101+
})
102+
const session = buildLiveTailSession({ type: 'all' }, testFilter)
103+
assert.strictEqual(session.getLiveTailSessionDuration(), 0)
104+
105+
const returnedResponseStream = await session.startLiveTailSession()
106+
assert.strictEqual(session.isAborted, false)
107+
assert.strictEqual(clock.countTimers(), 1)
108+
assert.deepStrictEqual(returnedResponseStream, mockResponseStream())
109+
110+
clock.tick(1000)
111+
assert.strictEqual(session.getLiveTailSessionDuration(), 1000)
112+
113+
session.stopLiveTailSession()
114+
assert.strictEqual(session.isAborted, true)
115+
assert.strictEqual(clock.countTimers(), 0)
116+
117+
//Session is stopped; ticking the clock forward should not change the session duration
118+
clock.tick(1000)
119+
assert.strictEqual(session.getLiveTailSessionDuration(), 1000)
120+
})
121+
68122
function buildLiveTailSession(
69123
logStreamFilter: LogStreamFilterResponse,
70124
logEventFilterPattern: string | undefined
@@ -77,4 +131,16 @@ describe('LiveTailSession', async function () {
77131
awsCredentials: testAwsCredentials,
78132
})
79133
}
134+
135+
async function* mockResponseStream(): AsyncIterable<StartLiveTailResponseStream> {
136+
const frame: StartLiveTailResponseStream = {
137+
sessionUpdate: {
138+
sessionMetadata: {
139+
sampled: false,
140+
},
141+
sessionResults: [],
142+
},
143+
}
144+
yield frame
145+
}
80146
})

0 commit comments

Comments
 (0)