@@ -11,7 +11,12 @@ import * as path from 'path'
11
11
import * as sinon from 'sinon'
12
12
import { DefaultEc2MetadataClient } from '../../shared/clients/ec2MetadataClient'
13
13
import * as vscode from 'vscode'
14
- import { getComputeRegion , initializeComputeRegion , mostRecentVersionKey } from '../../shared/extensionUtilities'
14
+ import {
15
+ ExtensionUserActivity ,
16
+ getComputeRegion ,
17
+ initializeComputeRegion ,
18
+ mostRecentVersionKey ,
19
+ } from '../../shared/extensionUtilities'
15
20
import {
16
21
createQuickStartWebview ,
17
22
isDifferentVersion ,
@@ -22,6 +27,7 @@ import * as filesystemUtilities from '../../shared/filesystemUtilities'
22
27
import { FakeExtensionContext } from '../fakeExtensionContext'
23
28
import { InstanceIdentity } from '../../shared/clients/ec2MetadataClient'
24
29
import { extensionVersion } from '../../shared/vscode/env'
30
+ import { sleep } from '../../shared/utilities/timeoutUtils'
25
31
26
32
describe ( 'extensionUtilities' , function ( ) {
27
33
describe ( 'safeGet' , function ( ) {
@@ -184,3 +190,51 @@ describe('initializeComputeRegion, getComputeRegion', async function () {
184
190
await assert . rejects ( metadataService . invoke ( '/bogus/path' ) )
185
191
} )
186
192
} )
193
+
194
+ describe ( 'ExtensionUserActivity' , function ( ) {
195
+ let count : number
196
+
197
+ function onEventTriggered ( ) {
198
+ count ++
199
+ }
200
+
201
+ before ( function ( ) {
202
+ count = 0
203
+ } )
204
+
205
+ it ( 'triggers twice when multiple user activities are fired in separate intervals' , async function ( ) {
206
+ // IMPORTANT: This may be flaky in CI, so we may need to increase the intervals for more tolerance
207
+ const throttleDelay = 200
208
+ const eventsFirst = [ delayedFiringEvent ( 100 ) , delayedFiringEvent ( 101 ) , delayedFiringEvent ( 102 ) ]
209
+ const eventsSecond = [
210
+ delayedFiringEvent ( 250 ) ,
211
+ delayedFiringEvent ( 251 ) ,
212
+ delayedFiringEvent ( 251 ) ,
213
+ delayedFiringEvent ( 252 ) ,
214
+ ]
215
+ const waitFor = 500 // some additional buffer to make sure everything triggers
216
+
217
+ const instance = ExtensionUserActivity . instance ( throttleDelay , [ ...eventsFirst , ...eventsSecond ] )
218
+ instance . onUserActivity ( onEventTriggered )
219
+ await sleep ( waitFor )
220
+
221
+ assert . strictEqual ( count , 2 )
222
+ } )
223
+
224
+ it ( 'gives the same instance if the throttle delay is the same' , function ( ) {
225
+ const instance1 = ExtensionUserActivity . instance ( 100 )
226
+ const instance2 = ExtensionUserActivity . instance ( 100 )
227
+ const instance3 = ExtensionUserActivity . instance ( 200 )
228
+ const instance4 = ExtensionUserActivity . instance ( 200 )
229
+
230
+ assert . strictEqual ( instance1 , instance2 )
231
+ assert . strictEqual ( instance3 , instance4 )
232
+ assert . notStrictEqual ( instance1 , instance3 )
233
+ } )
234
+
235
+ function delayedFiringEvent ( fireInMillis : number ) : vscode . Event < any > {
236
+ const event = new vscode . EventEmitter < void > ( )
237
+ setTimeout ( ( ) => event . fire ( ) , fireInMillis )
238
+ return event . event
239
+ }
240
+ } )
0 commit comments