@@ -22,7 +22,7 @@ const { MAX_TTL_SECONDS } = require('../lib/constants')
2222const stateLib = require ( '../index' )
2323const { randomInt } = require ( 'node:crypto' )
2424
25- const uniquePrefix = `${ Date . now ( ) } .${ randomInt ( 10 ) } `
25+ const uniquePrefix = `${ Date . now ( ) } .${ randomInt ( 100 ) } `
2626const testKey = `${ uniquePrefix } __e2e_test_state_key`
2727const testKey2 = `${ uniquePrefix } __e2e_test_state_key2`
2828
@@ -34,13 +34,33 @@ const initStateEnv = async (n = 1) => {
3434 process . env . __OW_API_KEY = process . env [ `TEST_AUTH_${ n } ` ]
3535 process . env . __OW_NAMESPACE = process . env [ `TEST_NAMESPACE_${ n } ` ]
3636 const state = await stateLib . init ( )
37- // // make sure we cleanup the namespace, note that delete might fail as it is an op under test
38- // await state.delete(`${uniquePrefix}*`)
39- await state . delete ( testKey )
40- await state . delete ( testKey2 )
37+ // make sure we cleanup the namespace, note that delete might fail as it is an op under test
38+ await state . deleteAll ( { match : `${ uniquePrefix } *` } )
4139 return state
4240}
4341
42+ // helpers
43+ const genKeyStrings = ( n , identifier ) => {
44+ return ( new Array ( n ) . fill ( 0 ) . map ( ( _ , idx ) => {
45+ const char = String . fromCharCode ( 97 + idx % 26 )
46+ // list-[a-z]-[0-(N-1)]
47+ return `${ uniquePrefix } __${ identifier } _${ char } _${ idx } `
48+ } ) )
49+ }
50+ const putKeys = async ( state , keys , ttl ) => {
51+ const _putKeys = async ( keys , ttl ) => {
52+ await Promise . all ( keys . map ( async ( k , idx ) => await state . put ( k , `value-${ idx } ` , { ttl } ) ) )
53+ }
54+
55+ const batchSize = 20
56+ let i = 0
57+ while ( i < keys . length - batchSize ) {
58+ await _putKeys ( keys . slice ( i , i + batchSize ) , ttl )
59+ i += batchSize
60+ }
61+ // final call
62+ await _putKeys ( keys . slice ( i ) , ttl )
63+ }
4464const waitFor = ( ms ) => new Promise ( resolve => setTimeout ( resolve , ms ) )
4565
4666test ( 'env vars' , ( ) => {
@@ -146,34 +166,12 @@ describe('e2e tests using OpenWhisk credentials (as env vars)', () => {
146166 await expect ( state . put ( testKey , testValue , { ttl : - 1 } ) ) . rejects . toThrow ( )
147167 } )
148168
149- test ( 'listKeys test: few < 128 keys , many, and expired entries' , async ( ) => {
169+ test ( 'listKeys test: few, many, and expired entries' , async ( ) => {
150170 const state = await initStateEnv ( )
151171
152- const genKeyStrings = ( n ) => {
153- return ( new Array ( n ) . fill ( 0 ) . map ( ( _ , idx ) => {
154- const char = String . fromCharCode ( 97 + idx % 26 )
155- // list-[a-z]-[0-(N-1)]
156- return `${ uniquePrefix } __list_${ char } _${ idx } `
157- } ) )
158- }
159- const putKeys = async ( keys , ttl ) => {
160- const _putKeys = async ( keys , ttl ) => {
161- await Promise . all ( keys . map ( async ( k , idx ) => await state . put ( k , `value-${ idx } ` , { ttl } ) ) )
162- }
163-
164- const batchSize = 20
165- let i = 0
166- while ( i < keys . length - batchSize ) {
167- await _putKeys ( keys . slice ( i , i + batchSize ) , ttl )
168- i += batchSize
169- }
170- // final call
171- await _putKeys ( keys . slice ( i ) , ttl )
172- }
173-
174172 // 1. test with not many elements, one iteration should return all
175- const keys90 = genKeyStrings ( 90 ) . sort ( )
176- await putKeys ( keys90 , 60 )
173+ const keys90 = genKeyStrings ( 90 , 'list' ) . sort ( )
174+ await putKeys ( state , keys90 , 60 )
177175
178176 let it , ret
179177
@@ -203,8 +201,8 @@ describe('e2e tests using OpenWhisk credentials (as env vars)', () => {
203201 expect ( await it . next ( ) ) . toEqual ( { done : true , value : undefined } )
204202
205203 // 2. test with many elements and large countHint
206- const keys900 = genKeyStrings ( 900 )
207- await putKeys ( keys900 , 60 )
204+ const keys900 = genKeyStrings ( 900 , 'list' )
205+ await putKeys ( state , keys900 , 60 )
208206
209207 // note: we can't list in isolation without prefix
210208 it = state . list ( { countHint : 1000 } )
@@ -255,7 +253,7 @@ describe('e2e tests using OpenWhisk credentials (as env vars)', () => {
255253 expect ( retArray . length ) . toEqual ( 1 )
256254
257255 // 4. make sure expired keys aren't listed
258- await putKeys ( keys90 , 1 )
256+ await putKeys ( state , keys90 , 1 )
259257 await waitFor ( 2000 )
260258
261259 it = state . list ( { countHint : 1000 , match : `${ uniquePrefix } __list_*` } )
@@ -264,6 +262,23 @@ describe('e2e tests using OpenWhisk credentials (as env vars)', () => {
264262 expect ( await it . next ( ) ) . toEqual ( { done : true , value : undefined } )
265263 } )
266264
265+ test ( 'deleteAll test' , async ( ) => {
266+ const state = await initStateEnv ( )
267+
268+ // < 100 keys
269+ const keys90 = genKeyStrings ( 90 , 'deleteAll' ) . sort ( )
270+ await putKeys ( state , keys90 , 60 )
271+ expect ( await state . deleteAll ( { match : `${ uniquePrefix } __deleteAll_a*` } ) ) . toEqual ( { keys : 4 } )
272+ expect ( await state . deleteAll ( { match : `${ uniquePrefix } __deleteAll_*` } ) ) . toEqual ( { keys : 86 } )
273+
274+ // > 1000 keys
275+ const keys1100 = genKeyStrings ( 1100 , 'deleteAll' ) . sort ( )
276+ await putKeys ( state , keys1100 , 60 )
277+ expect ( await state . deleteAll ( { match : `${ uniquePrefix } __deleteAll_*_1` } ) ) . toEqual ( { keys : 1 } )
278+ expect ( await state . deleteAll ( { match : `${ uniquePrefix } __deleteAll_*_1*0` } ) ) . toEqual ( { keys : 21 } ) // 10, 100 - 190, 1000-1090
279+ expect ( await state . deleteAll ( { match : `${ uniquePrefix } __deleteAll_*` } ) ) . toEqual ( { keys : 1078 } )
280+ } )
281+
267282 test ( 'throw error when get/put with invalid keys' , async ( ) => {
268283 const invalidKey = 'some/invalid:key'
269284 const state = await initStateEnv ( )
0 commit comments