5
5
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6
6
*/
7
7
import { expect } from 'chai' ;
8
- import { ThrottledPromiseAll } from '../src/throttledPromiseAll ' ;
9
- import { Duration } from '../src/duration ' ;
8
+ import { ThrottledPromiseAll } from '../src' ;
9
+ import { Duration } from '../src' ;
10
10
11
11
describe ( 'throttledPromiseAll' , ( ) => {
12
12
const numberProducer = (
@@ -19,7 +19,10 @@ describe('throttledPromiseAll', () => {
19
19
const throttledPromiseAll : ThrottledPromiseAll < number , number > = new ThrottledPromiseAll ( { concurrency : 1 } ) ;
20
20
for ( const i of [ 1 , 2 , 3 , 4 , 5 ] ) {
21
21
// eslint-disable-next-line no-await-in-loop
22
- throttledPromiseAll . add ( i , numberProducer ) ;
22
+ throttledPromiseAll . add (
23
+ i ,
24
+ ( source ) => new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( source + 1 ) , ( 5 - i ) * 100 ) )
25
+ ) ;
23
26
}
24
27
await throttledPromiseAll . all ( ) ;
25
28
const results = throttledPromiseAll . results as number [ ] ;
@@ -65,7 +68,7 @@ describe('throttledPromiseAll', () => {
65
68
} ) ;
66
69
throttledPromiseAll . add (
67
70
[ 1 , 2 , 3 , 4 , 5 ] ,
68
- ( source ) => new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( source + 1 ) , 10000 ) )
71
+ ( source ) => new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( source + 1 ) , 200 ) )
69
72
) ;
70
73
await throttledPromiseAll . all ( ) ;
71
74
} catch ( e ) {
@@ -87,4 +90,44 @@ describe('throttledPromiseAll', () => {
87
90
const results = throttledPromiseAll . results as number [ ] ;
88
91
expect ( results ) . to . deep . equal ( [ 1 , 2 , 3 , 4 , 5 ] . map ( ( i ) => i + 1 ) ) ;
89
92
} ) ;
93
+
94
+ it ( 'empty array' , async ( ) => {
95
+ const throttledPromiseAll : ThrottledPromiseAll < number , number > = new ThrottledPromiseAll ( { concurrency : 5 } ) ;
96
+ await throttledPromiseAll . all ( ) ;
97
+ expect ( throttledPromiseAll . results ) . to . deep . equal ( [ ] ) ;
98
+ } ) ;
99
+
100
+ it ( 'add single arg that returns undefined' , async ( ) => {
101
+ const throttledPromiseAll : ThrottledPromiseAll < number , number > = new ThrottledPromiseAll ( { concurrency : 5 } ) ;
102
+ throttledPromiseAll . add ( 0 , ( ) => Promise . resolve ( undefined ) ) ;
103
+ await throttledPromiseAll . all ( ) ;
104
+ expect ( throttledPromiseAll . results ) . to . deep . equal ( [ undefined ] ) ;
105
+ } ) ;
106
+
107
+ it ( 'add single arg that returns null' , async ( ) => {
108
+ const throttledPromiseAll : ThrottledPromiseAll < number , number > = new ThrottledPromiseAll ( { concurrency : 5 } ) ;
109
+ throttledPromiseAll . add ( 0 , ( ) => Promise . resolve ( - 10 ) ) ;
110
+ await throttledPromiseAll . all ( ) ;
111
+ expect ( throttledPromiseAll . results ) . to . deep . equal ( [ - 10 ] ) ;
112
+ } ) ;
113
+
114
+ it ( 'add with producer of undefined' , async ( ) => {
115
+ const throttledPromiseAll : ThrottledPromiseAll < number , number > = new ThrottledPromiseAll ( { concurrency : 5 } ) ;
116
+ throttledPromiseAll . add ( 0 , ( ) => Promise . resolve ( undefined ) ) ;
117
+ [ 1 , 2 , 3 , 4 , 5 ] . forEach ( ( i ) => throttledPromiseAll . add ( i , numberProducer ) ) ;
118
+ throttledPromiseAll . add ( 6 , ( ) => Promise . resolve ( undefined ) ) ;
119
+ await throttledPromiseAll . all ( ) ;
120
+ expect ( throttledPromiseAll . results ) . to . deep . equal ( [ undefined , 2 , 3 , 4 , 5 , 6 , undefined ] ) ;
121
+ } ) ;
122
+
123
+ it ( 'multiple adds to check order/sort' , async ( ) => {
124
+ const throttledPromiseAll : ThrottledPromiseAll < number , number > = new ThrottledPromiseAll ( { concurrency : 2 } ) ;
125
+ throttledPromiseAll . add ( 0 , ( ) => Promise . resolve ( undefined ) ) ;
126
+ [ 1 , 2 ] . forEach ( ( i ) => throttledPromiseAll . add ( i , numberProducer ) ) ;
127
+ throttledPromiseAll . add ( 6 , ( ) => Promise . resolve ( undefined ) ) ;
128
+ [ 6 , 7 ] . forEach ( ( i ) => throttledPromiseAll . add ( i , numberProducer ) ) ;
129
+ throttledPromiseAll . add ( 6 , ( ) => Promise . resolve ( undefined ) ) ;
130
+ await throttledPromiseAll . all ( ) ;
131
+ expect ( throttledPromiseAll . results ) . to . deep . equal ( [ undefined , 2 , 3 , undefined , 7 , 8 , undefined ] ) ;
132
+ } ) ;
90
133
} ) ;
0 commit comments