@@ -4,8 +4,19 @@ import { ExtensionContext, Memento } from "vscode";
44import { InvocationRateLimiter } from "../../helpers" ;
55
66describe ( "Invocation rate limiter" , ( ) => {
7+ // 1 January 2020
8+ let currentUnixTime = 1577836800 ;
9+
10+ function createDate ( dateString ?: string ) : Date {
11+ if ( dateString ) {
12+ return new Date ( dateString ) ;
13+ }
14+ const numMillisecondsPerSecond = 1000 ;
15+ return new Date ( currentUnixTime * numMillisecondsPerSecond ) ;
16+ }
17+
718 function createInvocationRateLimiter < T > ( funcIdentifier : string , func : ( ) => Promise < T > ) : InvocationRateLimiter < T > {
8- return new InvocationRateLimiter ( new MockExtensionContext ( ) , funcIdentifier , func ) ;
19+ return new InvocationRateLimiter ( new MockExtensionContext ( ) , funcIdentifier , func , s => createDate ( s ) ) ;
920 }
1021
1122 it ( "initially invokes function" , async ( ) => {
@@ -17,7 +28,7 @@ describe("Invocation rate limiter", () => {
1728 expect ( numTimesFuncCalled ) . to . equal ( 1 ) ;
1829 } ) ;
1930
20- it ( "doesn't invoke function within time period " , async ( ) => {
31+ it ( "doesn't invoke function again if no time has passed " , async ( ) => {
2132 let numTimesFuncCalled = 0 ;
2233 const invocationRateLimiter = createInvocationRateLimiter ( "funcid" , async ( ) => {
2334 numTimesFuncCalled ++ ;
@@ -27,7 +38,18 @@ describe("Invocation rate limiter", () => {
2738 expect ( numTimesFuncCalled ) . to . equal ( 1 ) ;
2839 } ) ;
2940
30- it ( "invoke function again after 0s time period has elapsed" , async ( ) => {
41+ it ( "doesn't invoke function again if requested time since last invocation hasn't passed" , async ( ) => {
42+ let numTimesFuncCalled = 0 ;
43+ const invocationRateLimiter = createInvocationRateLimiter ( "funcid" , async ( ) => {
44+ numTimesFuncCalled ++ ;
45+ } ) ;
46+ await invocationRateLimiter . invokeFunctionIfIntervalElapsed ( 100 ) ;
47+ currentUnixTime += 1 ;
48+ await invocationRateLimiter . invokeFunctionIfIntervalElapsed ( 2 ) ;
49+ expect ( numTimesFuncCalled ) . to . equal ( 1 ) ;
50+ } ) ;
51+
52+ it ( "invokes function again immediately if requested time since last invocation is 0 seconds" , async ( ) => {
3153 let numTimesFuncCalled = 0 ;
3254 const invocationRateLimiter = createInvocationRateLimiter ( "funcid" , async ( ) => {
3355 numTimesFuncCalled ++ ;
@@ -37,13 +59,13 @@ describe("Invocation rate limiter", () => {
3759 expect ( numTimesFuncCalled ) . to . equal ( 2 ) ;
3860 } ) ;
3961
40- it ( "invoke function again after 1s time period has elapsed" , async ( ) => {
62+ it ( "invokes function again after requested time since last invocation has elapsed" , async ( ) => {
4163 let numTimesFuncCalled = 0 ;
4264 const invocationRateLimiter = createInvocationRateLimiter ( "funcid" , async ( ) => {
4365 numTimesFuncCalled ++ ;
4466 } ) ;
4567 await invocationRateLimiter . invokeFunctionIfIntervalElapsed ( 1 ) ;
46- await new Promise ( ( resolve , _reject ) => setTimeout ( ( ) => resolve ( ) , 1000 ) ) ;
68+ currentUnixTime += 1 ;
4769 await invocationRateLimiter . invokeFunctionIfIntervalElapsed ( 1 ) ;
4870 expect ( numTimesFuncCalled ) . to . equal ( 2 ) ;
4971 } ) ;
0 commit comments