@@ -9,6 +9,7 @@ import { FileData, Table, TableWiseFiles } from '../types';
9
9
import { DBM } from './dbm' ;
10
10
import { InstanceManagerType } from './instance-manager' ;
11
11
import { DBMConstructorOptions } from './types' ;
12
+
12
13
export class MockFileManager implements FileManagerType {
13
14
private fileBufferStore : Record < string , FileBufferStore > = { } ;
14
15
private tables : Record < string , Table > = { } ;
@@ -139,6 +140,13 @@ const mockDB = {
139
140
} , 200 ) ;
140
141
} ) ;
141
142
} ,
143
+ cancelSent : async ( ) => {
144
+ return new Promise ( ( resolve ) => {
145
+ setTimeout ( ( ) => {
146
+ resolve ( true ) ;
147
+ } , 100 ) ;
148
+ } ) ;
149
+ } ,
142
150
close : async ( ) => {
143
151
// do nothing
144
152
} ,
@@ -203,13 +211,13 @@ describe('DBM', () => {
203
211
buffer : new Uint8Array ( ) ,
204
212
} ) ;
205
213
206
- const result = await dbm . queryWithTableNames (
207
- 'SELECT * FROM table1' ,
208
- [ 'table1' ] ,
209
- {
214
+ const result = await dbm . queryWithTableNames ( {
215
+ query : 'SELECT * FROM table1' ,
216
+ tableNames : [ 'table1' ] ,
217
+ options : {
210
218
preQuery,
211
- }
212
- ) ;
219
+ } ,
220
+ } ) ;
213
221
214
222
expect ( preQuery ) . toBeCalledTimes ( 1 ) ;
215
223
@@ -222,19 +230,22 @@ describe('DBM', () => {
222
230
} ) ;
223
231
224
232
it ( 'should execute a query with table names' , async ( ) => {
225
- const result = await dbm . queryWithTableNames ( 'SELECT * FROM table1' , [
226
- 'table1' ,
227
- ] ) ;
233
+ const result = await dbm . queryWithTableNames ( {
234
+ query : 'SELECT * FROM table1' ,
235
+ tableNames : [ 'table1' ] ,
236
+ } ) ;
228
237
expect ( result ) . toEqual ( [ 'SELECT * FROM table1' ] ) ;
229
238
} ) ;
230
239
231
240
it ( 'should execute multiple queries with table names' , async ( ) => {
232
- const promise1 = dbm . queryWithTableNames ( 'SELECT * FROM table1' , [
233
- 'table1' ,
234
- ] ) ;
235
- const promise2 = dbm . queryWithTableNames ( 'SELECT * FROM table2' , [
236
- 'table1' ,
237
- ] ) ;
241
+ const promise1 = dbm . queryWithTableNames ( {
242
+ query : 'SELECT * FROM table1' ,
243
+ tableNames : [ 'table1' ] ,
244
+ } ) ;
245
+ const promise2 = dbm . queryWithTableNames ( {
246
+ query : 'SELECT * FROM table2' ,
247
+ tableNames : [ 'table1' ] ,
248
+ } ) ;
238
249
/**
239
250
* Number of queries in the queue should be 1 as the first query is running
240
251
*/
@@ -260,9 +271,10 @@ describe('DBM', () => {
260
271
/**
261
272
* Execute another query
262
273
*/
263
- const promise3 = dbm . queryWithTableNames ( 'SELECT * FROM table3' , [
264
- 'table1' ,
265
- ] ) ;
274
+ const promise3 = dbm . queryWithTableNames ( {
275
+ query : 'SELECT * FROM table3' ,
276
+ tableNames : [ 'table1' ] ,
277
+ } ) ;
266
278
267
279
/**
268
280
* Now the queue should be running
@@ -302,16 +314,18 @@ describe('DBM', () => {
302
314
/**
303
315
* Execute a query
304
316
*/
305
- const promise1 = dbm . queryWithTableNames ( 'SELECT * FROM table1' , [
306
- 'table1' ,
307
- ] ) ;
317
+ const promise1 = dbm . queryWithTableNames ( {
318
+ query : 'SELECT * FROM table1' ,
319
+ tableNames : [ 'table1' ] ,
320
+ } ) ;
308
321
309
322
/**
310
323
* Execute another query
311
324
*/
312
- const promise2 = dbm . queryWithTableNames ( 'SELECT * FROM table2' , [
313
- 'table1' ,
314
- ] ) ;
325
+ const promise2 = dbm . queryWithTableNames ( {
326
+ query : 'SELECT * FROM table2' ,
327
+ tableNames : [ 'table1' ] ,
328
+ } ) ;
315
329
316
330
/**
317
331
* Wait for the queries to complete
@@ -361,16 +375,18 @@ describe('DBM', () => {
361
375
/**
362
376
* Execute a query
363
377
*/
364
- const promise1 = dbm . queryWithTableNames ( 'SELECT * FROM table1' , [
365
- 'table1' ,
366
- ] ) ;
378
+ const promise1 = dbm . queryWithTableNames ( {
379
+ query : 'SELECT * FROM table1' ,
380
+ tableNames : [ 'table1' ] ,
381
+ } ) ;
367
382
368
383
/**
369
384
* Execute another query
370
385
*/
371
- const promise2 = dbm . queryWithTableNames ( 'SELECT * FROM table2' , [
372
- 'table1' ,
373
- ] ) ;
386
+ const promise2 = dbm . queryWithTableNames ( {
387
+ query : 'SELECT * FROM table2' ,
388
+ tableNames : [ 'table1' ] ,
389
+ } ) ;
374
390
375
391
/**
376
392
* Wait for the queries to complete
@@ -404,7 +420,10 @@ describe('DBM', () => {
404
420
/**
405
421
* Execute a query
406
422
*/
407
- await dbm . queryWithTableNames ( 'SELECT * FROM table1' , [ 'table1' ] ) ;
423
+ await dbm . queryWithTableNames ( {
424
+ query : 'SELECT * FROM table1' ,
425
+ tableNames : [ 'table1' ] ,
426
+ } ) ;
408
427
409
428
/**
410
429
* wait for 200ms
@@ -417,4 +436,64 @@ describe('DBM', () => {
417
436
expect ( instanceManager . terminateDB ) . not . toBeCalled ( ) ;
418
437
} ) ;
419
438
} ) ;
439
+
440
+ describe ( 'cancel query execution' , ( ) => {
441
+ it ( 'should cancel the current executing query when abort is emitted' , async ( ) => {
442
+ const abortController1 = new AbortController ( ) ;
443
+
444
+ // check the current query throws error abort is emitted
445
+ try {
446
+ const promise = dbm . queryWithTableNames ( {
447
+ query : 'SELECT * FROM table1' ,
448
+ tableNames : [ 'table1' ] ,
449
+ options : {
450
+ signal : abortController1 . signal ,
451
+ } ,
452
+ } ) ;
453
+
454
+ abortController1 . abort ( ) ;
455
+
456
+ await promise ;
457
+
458
+ expect ( promise ) . not . toBeDefined ( ) ;
459
+ } catch ( e ) {
460
+ expect ( e ) . toBeDefined ( ) ;
461
+ }
462
+ } ) ;
463
+
464
+ it ( 'should cancel the query in the queue when abort is emitted' , async ( ) => {
465
+ const abortController1 = new AbortController ( ) ;
466
+ const abortController2 = new AbortController ( ) ;
467
+
468
+ const mockDBMQuery = jest . spyOn ( dbm , 'query' ) ;
469
+
470
+ const promise1 = dbm . queryWithTableNames ( {
471
+ query : 'SELECT * FROM table1' ,
472
+ tableNames : [ 'table1' ] ,
473
+ options : {
474
+ signal : abortController1 . signal ,
475
+ } ,
476
+ } ) ;
477
+
478
+ const promise2 = dbm . queryWithTableNames ( {
479
+ query : 'SELECT * FROM table2' ,
480
+ tableNames : [ 'table2' ] ,
481
+ options : {
482
+ signal : abortController2 . signal ,
483
+ } ,
484
+ } ) ;
485
+
486
+ abortController2 . abort ( ) ;
487
+
488
+ const promises = await Promise . allSettled ( [ promise1 , promise2 ] ) ;
489
+
490
+ // the first query should be fulfilled
491
+ expect ( mockDBMQuery ) . toBeCalledWith ( 'SELECT * FROM table1' ) ;
492
+ expect ( promises [ 0 ] . status ) . toBe ( 'fulfilled' ) ;
493
+
494
+ // the second query should be rejected as it was aborted
495
+ expect ( mockDBMQuery ) . not . toBeCalledWith ( 'SELECT * FROM table2' ) ;
496
+ expect ( promises [ 1 ] . status ) . toBe ( 'rejected' ) ;
497
+ } ) ;
498
+ } ) ;
420
499
} ) ;
0 commit comments