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'
57import assert from 'assert'
68import { 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'
815import { LogStreamFilterResponse } from '../../../../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu'
16+ import { installFakeClock } from '../../../testUtil'
917
1018describe ( '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