@@ -15,6 +15,7 @@ import { IFindRedisClientInstanceByOptions } from 'src/modules/redis/redis.servi
15
15
import { IGetNodeKeysResult } from 'src/modules/browser/services/keys-business/scanner/scanner.interface' ;
16
16
import IORedis from 'ioredis' ;
17
17
import { SettingsService } from 'src/modules/settings/settings.service' ;
18
+ import * as Utils from 'src/modules/database/utils/database.total.util' ;
18
19
import { StandaloneStrategy } from './standalone.strategy' ;
19
20
20
21
const REDIS_SCAN_CONFIG = config . get ( 'redis_scan' ) ;
@@ -38,10 +39,10 @@ const mockNodeEmptyResult: IGetNodeKeysResult = {
38
39
keys : [ ] ,
39
40
} ;
40
41
41
- const mockRedisKeyspaceInfoResponse_1 : string = '# Keyspace\r\ndb0:keys=1,expires=0,avg_ttl=0\r\n' ;
42
- const mockRedisKeyspaceInfoResponse_2 : string = '# Keyspace\r\ndb0:keys= 1000000,expires=0,avg_ttl=0\r\n' ;
43
- const mockRedisKeyspaceInfoResponse_3 : string = '# Keyspace\r\n \r\n' ;
44
- const mockRedisKeyspaceInfoResponse_4 : string = '# Keyspace\r\ndb0:keys=10,expires=0,avg_ttl=0\r\n' ;
42
+ const mockGetTotalResponse_1 : number = 1 ;
43
+ const mockGetTotalResponse_2 : number = 1000000 ;
44
+ const mockGetTotalResponse_3 : number = 0 ;
45
+ const mockGetTotalResponse_4 : number = 10 ;
45
46
46
47
let strategy ;
47
48
let browserTool ;
@@ -72,6 +73,7 @@ describe('Standalone Scanner Strategy', () => {
72
73
const getKeysDto : GetKeysDto = { cursor : '0' , count : 15 , keysInfo : true } ;
73
74
it ( 'should return appropriate value with filter by type' , async ( ) => {
74
75
const args = { ...getKeysDto , type : 'string' , match : 'pattern*' } ;
76
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_1 ) ;
75
77
76
78
when ( browserTool . execCommand )
77
79
. calledWith (
@@ -81,14 +83,6 @@ describe('Standalone Scanner Strategy', () => {
81
83
null ,
82
84
)
83
85
. mockResolvedValue ( [ 0 , [ getKeyInfoResponse . name ] ] ) ;
84
- when ( browserTool . execCommand )
85
- . calledWith (
86
- mockClientOptions ,
87
- BrowserToolKeysCommands . InfoKeyspace ,
88
- expect . anything ( ) ,
89
- 'utf8' ,
90
- )
91
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_1 ) ;
92
86
93
87
strategy . getKeysInfo = jest . fn ( ) . mockResolvedValue ( [ getKeyInfoResponse ] ) ;
94
88
@@ -104,7 +98,7 @@ describe('Standalone Scanner Strategy', () => {
104
98
] ) ;
105
99
expect ( strategy . getKeysInfo ) . toHaveBeenCalled ( ) ;
106
100
expect ( browserTool . execCommand ) . toHaveBeenNthCalledWith (
107
- 2 ,
101
+ 1 ,
108
102
mockClientOptions ,
109
103
BrowserToolKeysCommands . Scan ,
110
104
[ '0' , 'MATCH' , args . match , 'COUNT' , args . count , 'TYPE' , args . type ] ,
@@ -115,6 +109,7 @@ describe('Standalone Scanner Strategy', () => {
115
109
const args = {
116
110
...getKeysDto , type : 'string' , match : 'pattern*' , keysInfo : false ,
117
111
} ;
112
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_1 ) ;
118
113
119
114
when ( browserTool . execCommand )
120
115
. calledWith (
@@ -124,14 +119,6 @@ describe('Standalone Scanner Strategy', () => {
124
119
null ,
125
120
)
126
121
. mockResolvedValue ( [ 0 , [ getKeyInfoResponse . name ] ] ) ;
127
- when ( browserTool . execCommand )
128
- . calledWith (
129
- mockClientOptions ,
130
- BrowserToolKeysCommands . InfoKeyspace ,
131
- expect . anything ( ) ,
132
- 'utf8' ,
133
- )
134
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_1 ) ;
135
122
136
123
strategy . getKeysInfo = jest . fn ( ) ;
137
124
@@ -148,6 +135,8 @@ describe('Standalone Scanner Strategy', () => {
148
135
] ) ;
149
136
} ) ;
150
137
it ( 'should call scan 3 times and return appropriate value' , async ( ) => {
138
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_2 ) ;
139
+
151
140
when ( browserTool . execCommand )
152
141
. calledWith ( mockClientOptions , BrowserToolKeysCommands . Scan , [
153
142
'0' ,
@@ -175,14 +164,6 @@ describe('Standalone Scanner Strategy', () => {
175
164
getKeysDto . count ,
176
165
] , null )
177
166
. mockResolvedValue ( [ '0' , new Array ( 3 ) . fill ( getKeyInfoResponse . name ) ] ) ;
178
- when ( browserTool . execCommand )
179
- . calledWith (
180
- mockClientOptions ,
181
- BrowserToolKeysCommands . InfoKeyspace ,
182
- expect . anything ( ) ,
183
- 'utf8' ,
184
- )
185
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_2 ) ;
186
167
187
168
strategy . getKeysInfo = jest
188
169
. fn ( )
@@ -199,37 +180,32 @@ describe('Standalone Scanner Strategy', () => {
199
180
} ,
200
181
] ) ;
201
182
expect ( strategy . getKeysInfo ) . toHaveBeenCalled ( ) ;
202
- expect ( browserTool . execCommand ) . toBeCalledTimes ( 4 ) ;
183
+ expect ( browserTool . execCommand ) . toBeCalledTimes ( 3 ) ;
203
184
expect ( browserTool . execCommand ) . toHaveBeenNthCalledWith (
204
185
1 ,
205
186
mockClientOptions ,
206
- BrowserToolKeysCommands . InfoKeyspace ,
207
- expect . anything ( ) ,
208
- 'utf8' ,
209
- ) ;
210
- expect ( browserTool . execCommand ) . toHaveBeenNthCalledWith (
211
- 2 ,
212
- mockClientOptions ,
213
187
BrowserToolKeysCommands . Scan ,
214
188
[ '0' , 'MATCH' , '*' , 'COUNT' , getKeysDto . count ] ,
215
189
null ,
216
190
) ;
217
191
expect ( browserTool . execCommand ) . toHaveBeenNthCalledWith (
218
- 3 ,
192
+ 2 ,
219
193
mockClientOptions ,
220
194
BrowserToolKeysCommands . Scan ,
221
195
[ '1' , 'MATCH' , '*' , 'COUNT' , getKeysDto . count ] ,
222
196
null ,
223
197
) ;
224
198
expect ( browserTool . execCommand ) . toHaveBeenNthCalledWith (
225
- 4 ,
199
+ 3 ,
226
200
mockClientOptions ,
227
201
BrowserToolKeysCommands . Scan ,
228
202
[ '2' , 'MATCH' , '*' , 'COUNT' , getKeysDto . count ] ,
229
203
null ,
230
204
) ;
231
205
} ) ;
232
206
it ( 'should call scan N times until threshold exceeds' , async ( ) => {
207
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_2 ) ;
208
+
233
209
when ( browserTool . execCommand )
234
210
. calledWith (
235
211
mockClientOptions ,
@@ -238,14 +214,6 @@ describe('Standalone Scanner Strategy', () => {
238
214
null ,
239
215
)
240
216
. mockResolvedValue ( [ '1' , [ ] ] ) ;
241
- when ( browserTool . execCommand )
242
- . calledWith (
243
- mockClientOptions ,
244
- BrowserToolKeysCommands . InfoKeyspace ,
245
- expect . anything ( ) ,
246
- 'utf8' ,
247
- )
248
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_2 ) ;
249
217
250
218
strategy . getKeysInfo = jest . fn ( ) . mockResolvedValue ( [ ] ) ;
251
219
@@ -264,23 +232,9 @@ describe('Standalone Scanner Strategy', () => {
264
232
} ,
265
233
] ) ;
266
234
expect ( strategy . getKeysInfo ) . toHaveBeenCalledTimes ( 0 ) ;
267
- expect ( browserTool . execCommand ) . toHaveBeenNthCalledWith (
268
- 1 ,
269
- mockClientOptions ,
270
- BrowserToolKeysCommands . InfoKeyspace ,
271
- expect . anything ( ) ,
272
- 'utf8' ,
273
- ) ;
274
235
} ) ;
275
236
it ( 'should not call scan when total is 0' , async ( ) => {
276
- when ( browserTool . execCommand )
277
- . calledWith (
278
- mockClientOptions ,
279
- BrowserToolKeysCommands . InfoKeyspace ,
280
- expect . anything ( ) ,
281
- 'utf8' ,
282
- )
283
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_3 ) ;
237
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_3 ) ;
284
238
285
239
strategy . getKeysInfo = jest . fn ( ) . mockResolvedValue ( [ ] ) ;
286
240
@@ -291,24 +245,12 @@ describe('Standalone Scanner Strategy', () => {
291
245
...mockNodeEmptyResult ,
292
246
} ,
293
247
] ) ;
294
- expect ( browserTool . execCommand ) . toBeCalledTimes ( 1 ) ;
295
- expect ( browserTool . execCommand ) . toHaveBeenLastCalledWith (
296
- mockClientOptions ,
297
- BrowserToolKeysCommands . InfoKeyspace ,
298
- expect . anything ( ) ,
299
- 'utf8' ,
300
- ) ;
248
+ expect ( browserTool . execCommand ) . toBeCalledTimes ( 0 ) ;
301
249
expect ( strategy . getKeysInfo ) . toBeCalledTimes ( 0 ) ;
302
250
} ) ;
303
251
it ( 'should call scan with required args' , async ( ) => {
304
- when ( browserTool . execCommand )
305
- . calledWith (
306
- mockClientOptions ,
307
- BrowserToolKeysCommands . InfoKeyspace ,
308
- expect . anything ( ) ,
309
- 'utf8' ,
310
- )
311
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_3 ) ;
252
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_3 ) ;
253
+
312
254
strategy . getKeysInfo = jest . fn ( ) . mockResolvedValue ( [ ] ) ;
313
255
strategy . scan = jest . fn ( ) . mockResolvedValue ( undefined ) ;
314
256
@@ -327,30 +269,8 @@ describe('Standalone Scanner Strategy', () => {
327
269
expect ( result ) . toEqual ( [ mockNodeEmptyResult ] ) ;
328
270
expect ( strategy . getKeysInfo ) . toBeCalledTimes ( 0 ) ;
329
271
} ) ;
330
- it ( 'should throw error on Info Keyspace command' , async ( ) => {
331
- const replyError : ReplyError = {
332
- ...mockRedisNoPermError ,
333
- command : 'info keyspace' ,
334
- } ;
335
- when ( browserTool . execCommand )
336
- . calledWith ( mockClientOptions , BrowserToolKeysCommands . InfoKeyspace , [ ] , 'utf8' )
337
- . mockRejectedValue ( replyError ) ;
338
- try {
339
- await strategy . getKeys ( mockClientOptions , getKeysDto ) ;
340
- fail ( 'Should throw an error' ) ;
341
- } catch ( err ) {
342
- expect ( err . message ) . toEqual ( replyError . message ) ;
343
- }
344
- } ) ;
345
272
it ( 'should throw error on scan command' , async ( ) => {
346
- when ( browserTool . execCommand )
347
- . calledWith (
348
- mockClientOptions ,
349
- BrowserToolKeysCommands . InfoKeyspace ,
350
- expect . anything ( ) ,
351
- 'utf8' ,
352
- )
353
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_1 ) ;
273
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_1 ) ;
354
274
355
275
const replyError : ReplyError = {
356
276
...mockRedisNoPermError ,
@@ -374,14 +294,8 @@ describe('Standalone Scanner Strategy', () => {
374
294
} ) ;
375
295
describe ( 'get keys by glob patter' , ( ) => {
376
296
beforeEach ( async ( ) => {
377
- when ( browserTool . execCommand )
378
- . calledWith (
379
- mockClientOptions ,
380
- BrowserToolKeysCommands . InfoKeyspace ,
381
- expect . anything ( ) ,
382
- 'utf8' ,
383
- )
384
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_4 ) ;
297
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_4 ) ;
298
+
385
299
strategy . scan = jest . fn ( ) ;
386
300
} ) ;
387
301
it ( "should call scan when math contains '?' glob" , async ( ) => {
@@ -449,14 +363,8 @@ describe('Standalone Scanner Strategy', () => {
449
363
const key = getKeyInfoResponse . name ;
450
364
const total = 10 ;
451
365
beforeEach ( async ( ) => {
452
- when ( browserTool . execCommand )
453
- . calledWith (
454
- mockClientOptions ,
455
- BrowserToolKeysCommands . InfoKeyspace ,
456
- [ ] ,
457
- 'utf8' ,
458
- )
459
- . mockResolvedValue ( mockRedisKeyspaceInfoResponse_4 ) ;
366
+ jest . spyOn ( Utils , 'getTotal' ) . mockResolvedValue ( mockGetTotalResponse_4 ) ;
367
+
460
368
strategy . scan = jest . fn ( ) ;
461
369
} ) ;
462
370
it ( 'should find exact key when match is not glob patter' , async ( ) => {
0 commit comments