-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD4C.spec.ts
861 lines (759 loc) · 21.5 KB
/
D4C.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
import autobind from 'autobind-decorator'
import test from 'ava'
import { concurrent, D4C, ErrMsg, QConcurrency, synchronized } from './D4C'
const fixture = ['hello']
const fixture2 = 'world'
const queueTag = 'queue1'
const funcAsync = async (
input?: string[],
input2?: string
): Promise<string> => {
return input[0] + input2
}
const funcSync = (input: string[], input2: string): string => {
return input[0] + input2
}
const funcPromise = (input: string[], input2: string): Promise<string> => {
return Promise.resolve(input[0] + input2)
}
const timeout = (seconds: number, target?: { str: string }) => {
return new Promise<void>((resolve, _) =>
setTimeout(() => {
if (target?.str != undefined && target?.str != null) {
target.str += seconds
}
resolve()
}, seconds * 100)
)
}
const timeoutError = (seconds: number, result: string | Error) => {
return new Promise((_, reject) =>
setTimeout(() => {
reject(result)
}, seconds * 100)
)
}
const immediateFun = (seconds: number, target: { str: string }) => {
target.str += seconds
}
const immediateFunPromise = (seconds: number, target: { str: string }) => {
target.str += seconds
return Promise.resolve()
}
test('Class usage: test concurrency', async (t) => {
@QConcurrency([
{ limit: 100, isStatic: true },
{ limit: 100, isStatic: false },
{ limit: 1, isStatic: true, tag: '2' },
{ limit: 1, tag: '2' },
])
class TestController {
@concurrent
static async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@concurrent
async instanceTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@concurrent({ tag: '2' })
static async staticTimeout2(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@concurrent({ tag: '2' })
async instanceTimeout2(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@synchronized({ tag: '3' })
async random(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
// test: concurrency on static method, and use default tag
let test = { str: '' }
await Promise.all([
TestController.staticTimeout(0.5, test),
TestController.staticTimeout(0.1, test),
])
t.is(test.str, '0.10.5')
// test: concurrency on static method,
test = { str: '' }
await Promise.all([
TestController.staticTimeout2(0.5, test),
TestController.staticTimeout2(0.1, test),
])
t.is(test.str, '0.50.1')
// test: concurrency on instance method, and use default tag
const testController = new TestController()
test = { str: '' }
await Promise.all([
testController.instanceTimeout(0.5, test),
testController.instanceTimeout(0.1, test),
])
t.is(test.str, '0.10.5')
// test: concurrency on instance method
test = { str: '' }
await Promise.all([
testController.instanceTimeout2(0.5, test),
testController.instanceTimeout2(0.1, test),
])
t.is(test.str, '0.50.1')
// test: no use class decorator so @concurrent will use default @concurrency Infinity
class TestController2 {
@concurrent
static async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
test = { str: '' }
await Promise.all([
TestController2.staticTimeout(0.5, test),
TestController2.staticTimeout(0.1, test),
])
t.is(test.str, '0.10.5')
@QConcurrency([
{ limit: 1, isStatic: true },
{ limit: 1, isStatic: false },
{ limit: 1, isStatic: true, tag: '2' },
{ limit: 1, isStatic: false, tag: '2' },
])
class TestController3 {
static async staticTimeoutNoDecorator(
seconds: number,
obj: { str: string }
) {
await timeout(seconds, obj)
}
@concurrent
static async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@concurrent
async instanceTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@concurrent({ tag: '2' })
static async staticTimeout2(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@concurrent({ tag: '2' })
async instanceTimeout2(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
// Test: applying @classConcurrency but testing method not decorated will not be affected
test = { str: '' }
await Promise.all([
TestController3.staticTimeoutNoDecorator(0.5, test),
TestController3.staticTimeoutNoDecorator(0.1, test),
])
t.is(test.str, '0.10.5')
// Test: different queues are isolated with each other
const testController3 = new TestController3()
test = { str: '' }
await Promise.all([
TestController.staticTimeout(0.5, test),
testController.instanceTimeout(0.4, test),
TestController.staticTimeout2(0.3, test),
testController.instanceTimeout2(0.2, test),
])
t.is(test.str, '0.20.30.40.5')
/** @classConcurrency tries to setup default queue's concurrency but is conflicted with synchronized (implicitly concurrency =1) */
let error = null
try {
@QConcurrency([{ limit: 1, isStatic: true }])
class TestController4 {
static async staticTimeoutNoDecorator(
seconds: number,
obj: { str: string }
) {
await timeout(seconds, obj)
}
@synchronized
static async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.ClassAndMethodDecoratorsIncompatible)
/** for test coverage */
error = null
try {
@QConcurrency([{ limit: 1, isStatic: true, tag: '2' }])
class TestController5 {
static async staticTimeoutNoDecorator(
seconds: number,
obj: { str: string }
) {
await timeout(seconds, obj)
}
@synchronized
static async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
} catch (err) {
error = err
}
t.is(error, null)
/** for test coverage */
error = null
try {
@QConcurrency([{ limit: 1, isStatic: true, tag: '2' }])
class TestController6 {
@concurrent
static async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
} catch (err) {
error = err
}
t.is(error, null)
/** for test coverage */
error = null
try {
@QConcurrency([{ limit: 1, tag: '2' }])
class TestController7 {
@synchronized
async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
} catch (err) {
error = err
}
t.is(error, null)
/** for test coverage */
error = null
try {
@QConcurrency([
{ limit: 1, tag: '2' },
{ limit: 1, tag: '3', isStatic: true },
])
class TestController8 {
async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
} catch (err) {
error = err
}
t.is(error, null)
/** for the same tag queue, @concurrent & @synchronized can not be applied both */
error = null
try {
@QConcurrency([{ limit: 1, isStatic: true }])
class TestController9 {
static async staticTimeoutNoDecorator(
seconds: number,
obj: { str: string }
) {
await timeout(seconds, obj)
}
@synchronized
static async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@concurrent
static async staticTimeout2(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.TwoDecoratorsIncompatible)
/** @classConcurrency' parameters should be an array */
error = null
try {
@QConcurrency({ limit: 1, tag: '2' } as any)
class TestController10 {
async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidClassDecoratorParameter)
/** limit should be a number */
error = null
try {
@QConcurrency([null, { limit: '3' }] as any)
class TestController11 {
async staticTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidClassDecoratorParameter)
})
test('Instance usage: test concurrency', async (t) => {
/** Fixme: dummy cases for code coverage */
let d4c = new D4C([null] as any)
d4c = new D4C([{}] as any)
d4c.setConcurrency(null)
/** default queue: concurrency 100 test */
let test = { str: '' }
d4c = new D4C([{ concurrency: { limit: 100 } }])
let fn1 = d4c.wrap(timeout)
let fn2 = d4c.wrap(immediateFun)
let fn3 = d4c.wrap(immediateFunPromise)
await Promise.all([
fn1(2, test),
fn2(1, test),
fn1(0.5, test),
fn3(0.2, test),
fn1(0.05, test),
])
t.is(test.str, '10.20.050.52')
/** tag queue: concurrency 100 test */
test = { str: '' }
d4c = new D4C([{ concurrency: { limit: 100, tag: '2' } }])
fn1 = d4c.wrap(timeout, { tag: '2' })
fn2 = d4c.wrap(immediateFun, { tag: '2' })
fn3 = d4c.wrap(immediateFunPromise, { tag: '2' })
await Promise.all([
fn1(2, test),
fn2(1, test),
fn1(0.5, test),
fn3(0.2, test),
fn1(0.05, test),
])
t.is(test.str, '10.20.050.52')
/** default queue: use setConcurrency to change concurrency on default to 100 */
test = { str: '' }
d4c = new D4C()
d4c.setConcurrency([{ limit: 100 }])
fn1 = d4c.wrap(timeout)
fn2 = d4c.wrap(immediateFun)
fn3 = d4c.wrap(immediateFunPromise)
await Promise.all([
fn1(2, test),
fn2(1, test),
fn1(0.5, test),
fn3(0.2, test),
fn1(0.05, test),
])
t.is(test.str, '10.20.050.52')
/** new tag with setConcurrency to set concurrency 1 */
test = { str: '' }
d4c.setConcurrency([{ limit: 1, tag: '2' }])
fn1 = d4c.wrap(timeout, { tag: '2' })
fn2 = d4c.wrap(immediateFun, { tag: '2' })
fn3 = d4c.wrap(immediateFunPromise, { tag: '2' })
await Promise.all([
fn1(2, test),
fn2(1, test),
fn1(0.5, test),
fn3(0.2, test),
fn1(0.05, test),
])
t.is(test.str, '210.50.20.05')
/** default queue: use setConcurrency to set it back to 1 */
test = { str: '' }
d4c.setConcurrency([{ limit: 1 }])
fn1 = d4c.wrap(timeout)
fn2 = d4c.wrap(immediateFun)
fn3 = d4c.wrap(immediateFunPromise)
await Promise.all([
fn1(2, test),
fn2(1, test),
fn1(0.5, test),
fn3(0.2, test),
fn1(0.05, test),
])
t.is(test.str, '210.50.20.05')
let error = null
try {
d4c.setConcurrency([undefined] as any)
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidQueueConcurrency)
error = null
try {
d4c.setConcurrency([{ limit: -100 }])
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidQueueConcurrency)
error = null
try {
d4c.setConcurrency([{ tag: true, limit: 100 }] as any)
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidQueueTag)
error = null
try {
d4c.setConcurrency([{ tag: true }] as any)
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidQueueConcurrency)
error = null
try {
d4c.setConcurrency([{ tag: true, limit: '100' }] as any)
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidQueueConcurrency)
error = null
try {
d4c = new D4C([{ concurrency: { limit: '11' } }] as any)
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidQueueConcurrency)
})
test('Instance usage: pass a class arrow function property', async (t) => {
class TestController {
greeting: string
constructor(message: string) {
this.greeting = message
}
greet = async (text: string) => {
const str = 'Hello, ' + text + this.greeting
return str
}
}
const d4c = new D4C()
const test = new TestController('!!')
const newFunc = d4c.wrap(test.greet, { tag: queueTag })
const job = newFunc(fixture2)
const resp = await job
t.is(resp, 'Hello, world!!')
/** wrap_exec part */
const resp2 = await d4c.apply(test.greet, {
tag: queueTag,
args: [fixture2],
})
t.is(resp2, 'Hello, world!!')
})
test('Decorator usage', async (t) => {
class TestController {
greeting: string
constructor(message: string) {
this.greeting = message
this.testManualBind = this.testManualBind.bind(this)
}
@synchronized
greet(text: string) {
const str = 'Hello, ' + text + this.greeting
return str
}
@synchronized()
async testManualBind(text: string) {
const str = 'Hello, ' + text + this.greeting
return str
}
@autobind
@synchronized()
async testAutoBind(text: string) {
const str = 'Hello, ' + text + this.greeting
return str
}
@synchronized({})
static async staticMethod(text: string) {
return text
}
@synchronized
static async timeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@synchronized({ tag: '2' })
static async timeoutAnotherQueue(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@synchronized({ inheritPreErr: true })
async instanceTimeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
@synchronized({})
async instanceTimeoutError(seconds: number, obj) {
await timeoutError(seconds, obj)
}
@synchronized({ noBlockCurr: true })
testNoBlockCurr(seconds: number, obj: { str: string }) {
obj.str += seconds
}
@autobind
autobindMethodNoQueue(text: string) {
const str = 'Hello, ' + text + this.greeting
return str
}
}
// /** instance method */
const testController = new TestController('!!')
t.is(await testController.greet(fixture2), 'Hello, world!!')
/** test if this lib working with manual bind */
const testManualBind = testController.testManualBind
t.is(await testManualBind(fixture2), 'Hello, world!!')
/** test if this lib working with auto bind */
const testAutoBind = testController.testAutoBind
t.is(await testAutoBind(fixture2), 'Hello, world!!')
/** static method part */
t.is(await TestController.staticMethod(fixture2), 'world')
/** Test if they are really executed one by one */
let test = { str: '' }
await Promise.all([
TestController.timeout(0.5, test),
TestController.timeout(0.1, test),
])
t.is(test.str, '0.50.1')
/** Test if these are really use different queues */
test = { str: '' }
await Promise.all([
TestController.timeout(0.5, test),
TestController.timeoutAnotherQueue(0.1, test),
])
t.is(test.str, '0.10.5')
//** Static and Instance method should have different queues */
test = { str: '' }
await Promise.all([
TestController.timeout(0.5, test),
testController.instanceTimeout(0.1, test),
])
t.is(test.str, '0.10.5')
//** Two instances should have different queues */
const testController2 = new TestController('!!')
test = { str: '' }
await Promise.all([
testController2.instanceTimeout(0.5, test),
testController.instanceTimeout(0.1, test),
])
t.is(test.str, '0.10.5')
/** Class instance and D4C instance should have different queues */
test = { str: '' }
const fn = new D4C().wrap(timeout)
await Promise.all([fn(0.5, test), testController.instanceTimeout(0.1, test)])
t.is(test.str, '0.10.5')
/** composite case: D4C instance on no autobind decorated method */
let error = null
try {
const d4c = new D4C()
const newFunc = d4c.wrap(testController.greet)
const resp = await newFunc('')
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.MissingThisDueBindIssue)
/** composite case: D4C instance on autobind decorated method */
const d4c = new D4C()
const result = await d4c.apply(testController.testAutoBind, {
args: ['world'],
})
t.is(result, 'Hello, world!!')
/** composite case: D4C instance on autobind non-decorated method */
t.is(
await d4c.apply(testController.autobindMethodNoQueue),
'Hello, undefined!!'
)
/** Two class should not affect each other */
class TestController2 {
greeting: string
constructor(message: string) {
this.greeting = message
}
@synchronized({})
static async timeout(seconds: number, obj: { str: string }) {
await timeout(seconds, obj)
}
}
test = { str: '' }
await Promise.all([
TestController.timeout(0.5, test),
TestController2.timeout(0.1, test),
])
t.is(test.str, '0.10.5')
/** test invalid decorator */
error = null
try {
class TestController4 {
@synchronized({ tag: true } as any)
static async greet(text: string) {
return text
}
}
await TestController4.greet(fixture2), 'world'
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InvalidDecoratorOption)
/** test if option inheritPreErr works on decorator */
;(async () => {
try {
await testController.instanceTimeoutError(1, 'some_error')
} catch (err) {
// console.log(" err by purpose")
}
})()
error = null
try {
await testController.instanceTimeout(0.1, { str: '' })
} catch (err) {
error = err
}
t.is(error.message, 'some_error')
/** test if option noBlockCurr works on decorator */
test = { str: '' }
const job = testController.testNoBlockCurr(2, test)
test.str = test.str + '1'
await job
t.is(test.str, '12')
})
test('Instance usage: funcAsync, symbol tag', async (t) => {
const d4c = new D4C()
const job = d4c.wrap(funcAsync, { tag: Symbol('123') })(fixture, fixture2)
t.is(await job, 'helloworld')
})
test('Instance usage: funcAsync, a invalid null tag case', async (t) => {
const d4c = new D4C()
let error
try {
await d4c.wrap(funcAsync, { tag: null })
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.InstanceInvalidTag)
})
test('Instance usage: async function', async (t) => {
const d4c = new D4C()
const newFunc = d4c.wrap(funcAsync, { tag: queueTag })
const job = newFunc(fixture, fixture2)
const resp = await job
t.is(resp, 'helloworld')
})
test('Instance usage: non-async function', async (t) => {
const d4c = new D4C()
const newFunc = d4c.wrap(funcSync, { tag: queueTag })
const job = newFunc(fixture, fixture2)
const resp = await job
t.is(resp, 'helloworld')
})
test('Instance usage: promise-returning function', async (t) => {
const d4c = new D4C()
const job = d4c.wrap(funcPromise, { tag: queueTag })(fixture, fixture2)
const resp = await job
t.is(resp, 'helloworld')
})
test('Instance usage: apply a funcAsync', async (t) => {
const d4c = new D4C()
/** apply */
const resp = await d4c.apply(funcAsync, {
tag: queueTag,
args: [['33', '44'], '5'],
})
t.is(resp, '335')
})
test('Instance usage: test if queue really work, execute one by one', async (t) => {
let test = { str: '' }
await Promise.all([
timeout(2, test),
immediateFun(1, test),
timeout(0.5, test),
immediateFunPromise(0.2, test),
timeout(0.05, test),
])
t.is(test.str, '10.20.050.52') // 1, 0.2, 0.05, 0.5, 2
test = { str: '' }
const d4c = new D4C()
const fn1 = d4c.wrap(timeout)
const fn2 = d4c.wrap(immediateFun)
const fn3 = d4c.wrap(immediateFunPromise)
await Promise.all([
fn1(2, test),
fn2(1, test),
fn1(0.5, test),
fn3(0.2, test),
fn1(0.05, test),
])
t.is(test.str, '210.50.20.05')
test = { str: '' }
const d4c2 = new D4C()
const fn11 = d4c2.wrap(timeout, { tag: '1' })
const fn21 = d4c2.wrap(immediateFun, { tag: '2' })
const fn31 = d4c2.wrap(immediateFunPromise, { tag: '3' })
await Promise.all([
fn11(2, test),
fn21(1, test),
fn11(0.5, test),
fn31(0.2, test),
fn11(0.05, test),
])
t.is(test.str, '10.220.50.05') // 1, 0.2, 2, 0.5, 0.05
test = { str: '' }
const fn12 = new D4C().wrap(timeout)
const fn22 = new D4C().wrap(immediateFun)
const fn32 = new D4C().wrap(immediateFunPromise)
await Promise.all([
fn12(2, test),
fn22(1, test),
fn12(0.5, test),
fn32(0.2, test),
fn12(0.05, test),
])
t.is(test.str, '10.220.50.05')
})
test('Instance usage: option noBlockCurr enable, with two non-async function ', async (t) => {
const d4c = new D4C()
let testStr = ''
testStr += '1'
const newFunc = d4c.wrap(
() => {
testStr += 'inFuncSyn'
},
{ tag: queueTag, noBlockCurr: true }
)
testStr += '2'
const job = newFunc()
testStr += '3'
const newFunc2 = d4c.wrap(
() => {
testStr += 'inFuncSyn2'
},
{ tag: queueTag }
)
await Promise.all([job, newFunc2()])
testStr += '4'
t.is(testStr, '123inFuncSyninFuncSyn24')
})
test("Instance usage: option inheritPreErr enable: task2 inherit task1's error in object queue", async (t) => {
const d4c = new D4C()
const fun2 = async () => {
console.log('dummy fun2')
}
const fun1ErrorProducer = async () => {
try {
await d4c.wrap(timeoutError)(1, new Error('some_error'))
} catch (_) {
// console.log(" err by purpose")
}
}
fun1ErrorProducer()
let error
try {
await d4c.wrap(fun2, { inheritPreErr: true })()
} catch (err) {
error = err
}
t.is(error.message, 'some_error')
})
test('Instance usage: test option dropWhenReachLimit', async (t) => {
const d4c = new D4C([{ concurrency: { limit: 2 } }])
const fn1 = d4c.wrap(timeout, { dropWhenReachLimit: true })
let error = null
try {
await fn1(3)
await Promise.all([fn1(3), fn1(3), fn1(3)])
} catch (err) {
error = err
}
t.is(error.message, ErrMsg.QueueIsFull)
})