@@ -3,11 +3,12 @@ const sinon = require('sinon');
33
44const constants = require ( '../../../../../constants' ) ;
55const {
6+ namespace,
67 configCache,
78 getCachedConfig,
89 setCachedConfig,
910 expireCachedConfigs,
10- invalidateCachedConfig ,
11+ deleteCachedConfig ,
1112} = require ( '../../../../../lib/api/apiUtils/rateLimit/cache' ) ;
1213
1314describe ( 'test limit config cache storage' , ( ) => {
@@ -22,66 +23,70 @@ describe('test limit config cache storage', () => {
2223 clock . restore ( ) ;
2324 } ) ;
2425
26+ beforeEach ( ( ) => {
27+ configCache . clear ( ) ;
28+ } ) ;
29+
2530 it ( 'should add config to cache' , ( ) => {
26- setCachedConfig ( 'foo' , 10 , constants . rateLimitDefaultConfigCacheTTL ) ;
31+ setCachedConfig ( namespace . bucket , 'foo' , 10 , constants . rateLimitDefaultConfigCacheTTL ) ;
2732 assert . deepStrictEqual (
28- configCache . get ( ' foo' ) ,
33+ configCache . get ( ` ${ namespace . bucket } : foo` ) ,
2934 {
3035 expiry : now + constants . rateLimitDefaultConfigCacheTTL ,
31- config : 10 ,
36+ value : 10 ,
3237 }
3338 ) ;
3439 } ) ;
3540
3641 it ( 'should get a non expired config' , ( ) => {
37- setCachedConfig ( 'foo' , 10 , constants . rateLimitDefaultConfigCacheTTL ) ;
38- assert . strictEqual ( getCachedConfig ( 'foo' ) , 10 ) ;
42+ setCachedConfig ( namespace . bucket , 'foo' , 10 , constants . rateLimitDefaultConfigCacheTTL ) ;
43+ assert . strictEqual ( getCachedConfig ( namespace . bucket , 'foo' ) , 10 ) ;
3944 } ) ;
4045
4146 it ( 'should return undefined and delete the key for an expired config' , ( ) => {
42- configCache . set ( ' foo' , {
47+ configCache . set ( ` ${ namespace . bucket } : foo` , {
4348 expiry : now - 10000 ,
44- config : 10 ,
49+ value : 10 ,
4550 } ) ;
46- assert . strictEqual ( getCachedConfig ( 'foo' ) , undefined ) ;
51+ assert . strictEqual ( getCachedConfig ( namespace . bucket , 'foo' ) , undefined ) ;
4752 } ) ;
4853
49- it ( 'should expire configs less than or equal to the given timestamp ' , ( ) => {
54+ it ( 'should expire configs less than or equal to current time ' , ( ) => {
5055 configCache . set ( 'past' , {
5156 expiry : now - 10000 ,
52- config : 10 ,
57+ value : 10 ,
5358 } ) ;
5459 configCache . set ( 'present' , {
5560 expiry : now ,
56- config : 10 ,
61+ value : 10 ,
5762 } ) ;
5863 configCache . set ( 'future' , {
5964 expiry : now + 10000 ,
60- config : 10 ,
65+ value : 10 ,
6166 } ) ;
62- expireCachedConfigs ( now ) ;
67+ // expireCachedConfigs uses Date.now() internally; fake clock is set to `now`
68+ expireCachedConfigs ( ) ;
6369 assert . strictEqual ( configCache . get ( 'past' ) , undefined ) ;
6470 assert . strictEqual ( configCache . get ( 'present' ) , undefined ) ;
6571 assert . deepStrictEqual ( configCache . get ( 'future' ) , {
6672 expiry : now + 10000 ,
67- config : 10 ,
73+ value : 10 ,
6874 } ) ;
6975 } ) ;
7076
71- it ( 'should invalidate cached config for a specific bucket ' , ( ) => {
72- setCachedConfig ( ' bucket: my-bucket', { limit : 100 } , constants . rateLimitDefaultConfigCacheTTL ) ;
73- setCachedConfig ( ' bucket: other-bucket', { limit : 200 } , constants . rateLimitDefaultConfigCacheTTL ) ;
77+ it ( 'should delete cached config for a specific resource ' , ( ) => {
78+ setCachedConfig ( namespace . bucket , ' my-bucket', { limit : 100 } , constants . rateLimitDefaultConfigCacheTTL ) ;
79+ setCachedConfig ( namespace . bucket , ' other-bucket', { limit : 200 } , constants . rateLimitDefaultConfigCacheTTL ) ;
7480
75- const result = invalidateCachedConfig ( 'my-bucket' ) ;
81+ deleteCachedConfig ( namespace . bucket , 'my-bucket' ) ;
7682
77- assert . strictEqual ( result , true ) ;
78- assert . strictEqual ( getCachedConfig ( 'bucket:my-bucket' ) , undefined ) ;
79- assert . deepStrictEqual ( getCachedConfig ( 'bucket:other-bucket' ) , { limit : 200 } ) ;
83+ assert . strictEqual ( getCachedConfig ( namespace . bucket , 'my-bucket' ) , undefined ) ;
84+ assert . deepStrictEqual ( getCachedConfig ( namespace . bucket , 'other-bucket' ) , { limit : 200 } ) ;
8085 } ) ;
8186
82- it ( 'should return false when invalidating non-existent bucket ' , ( ) => {
83- const result = invalidateCachedConfig ( 'non-existent-bucket' ) ;
84-
85- assert . strictEqual ( result , false ) ;
87+ it ( 'should be a no-op when deleting a non-existent key ' , ( ) => {
88+ assert . doesNotThrow ( ( ) => {
89+ deleteCachedConfig ( namespace . bucket , 'non-existent-bucket' ) ;
90+ } ) ;
8691 } ) ;
8792} ) ;
0 commit comments