@@ -22,12 +22,12 @@ describe('DynamicWorkerPool', () => {
22
22
} ) ;
23
23
24
24
test ( 'size of new pool should be 0' , ( ) => {
25
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 0 , 5 ) ;
25
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 0 , 5 , 0 ) ;
26
26
expect ( testPool . size ) . toEqual ( 0 ) ;
27
27
} ) ;
28
28
29
29
test ( 'execute task should aquire new workers and execute tasks on them' , async ( ) => {
30
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 0 , 2 ) ;
30
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 0 , 2 , 0 ) ;
31
31
32
32
const workerDisposeSpy = jest . spyOn ( testWorkerInstance , 'dispose' ) ;
33
33
const createWorkerSpy = jest . spyOn ( workerFactory , 'createWorker' ) ;
@@ -47,7 +47,7 @@ describe('DynamicWorkerPool', () => {
47
47
} ) ;
48
48
49
49
test ( 'worker pool should hould minSize workers' , async ( ) => {
50
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 3 , 5 ) ;
50
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 3 , 5 , 0 ) ;
51
51
52
52
const workerDisposeSpy = jest . spyOn ( testWorkerInstance , 'dispose' ) ;
53
53
const createWorkerSpy = jest . spyOn ( workerFactory , 'createWorker' ) ;
@@ -72,7 +72,7 @@ describe('DynamicWorkerPool', () => {
72
72
} ) ;
73
73
74
74
test ( 'executeTask should reject if worker pool max size is reached' , async ( ) => {
75
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 0 , 1 ) ;
75
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 0 , 1 , 0 ) ;
76
76
77
77
const workerDisposeSpy = jest . spyOn ( testWorkerInstance , 'dispose' ) ;
78
78
const createWorkerSpy = jest . spyOn ( workerFactory , 'createWorker' ) ;
@@ -91,7 +91,7 @@ describe('DynamicWorkerPool', () => {
91
91
} ) ;
92
92
93
93
test ( 'worker pool should re-use existing workers in pool' , async ( ) => {
94
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 1 , 1 ) ;
94
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 1 , 1 , 0 ) ;
95
95
96
96
const workerDisposeSpy = jest . spyOn ( testWorkerInstance , 'dispose' ) ;
97
97
const createWorkerSpy = jest . spyOn ( workerFactory , 'createWorker' ) ;
@@ -110,7 +110,7 @@ describe('DynamicWorkerPool', () => {
110
110
} ) ;
111
111
112
112
test ( 'worker pool should emit an error on error$ if worker disposal fails' , async ( ) => {
113
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 0 , 1 ) ;
113
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 0 , 1 , 0 ) ;
114
114
115
115
jest . spyOn ( testWorkerInstance , 'dispose' ) . mockImplementationOnce ( async ( ) => {
116
116
throw new Error ( 'DisposeError' ) ;
@@ -126,13 +126,13 @@ describe('DynamicWorkerPool', () => {
126
126
} ) ;
127
127
128
128
test ( 'executeTask on a stopped DynamicWorkerPool should reject' , async ( ) => {
129
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
129
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
130
130
testPool . stop ( ) ;
131
131
await expect ( testPool . executeTask ( '1' ) ) . rejects . toThrowError ( 'Cannot aquire worker from stopped worker pool!' ) ;
132
132
} ) ;
133
133
134
134
test ( 'stop should terminate all current workers' , async ( ) => {
135
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
135
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
136
136
const workerDisposeSpy = jest . spyOn ( testWorkerInstance , 'dispose' ) ;
137
137
138
138
await Promise . all ( [
@@ -149,7 +149,7 @@ describe('DynamicWorkerPool', () => {
149
149
} ) ;
150
150
151
151
test ( 'busyWorkers$ should emit number of workers in use' , async ( ) => {
152
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
152
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
153
153
154
154
const busyWorkersHistoryPromise = testPool . busyWorkers$ . pipe ( bufferCount ( 13 ) , first ( ) ) . toPromise ( ) ;
155
155
@@ -167,7 +167,7 @@ describe('DynamicWorkerPool', () => {
167
167
} ) ;
168
168
169
169
test ( 'idleWorkers$ should emit number of idle workers' , async ( ) => {
170
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
170
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
171
171
172
172
const idleWorkersHistoryPromise = testPool . idleWorkers$ . pipe ( bufferCount ( 5 ) , first ( ) ) . toPromise ( ) ;
173
173
@@ -185,7 +185,7 @@ describe('DynamicWorkerPool', () => {
185
185
} ) ;
186
186
187
187
test ( 'availableWorkers$ should emit number of available workers' , async ( ) => {
188
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
188
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
189
189
190
190
const availableWorkersHistoryPromise = testPool . availableWorkers$ . pipe ( bufferCount ( 13 ) , first ( ) ) . toPromise ( ) ;
191
191
@@ -203,7 +203,7 @@ describe('DynamicWorkerPool', () => {
203
203
} ) ;
204
204
205
205
test ( 'availableWorkers$ should emit 0 for a stopped pool' , async ( ) => {
206
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
206
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
207
207
208
208
const availableWorkersHistoryPromise = testPool . availableWorkers$ . pipe ( bufferCount ( 4 ) , first ( ) ) . toPromise ( ) ;
209
209
@@ -215,13 +215,13 @@ describe('DynamicWorkerPool', () => {
215
215
} ) ;
216
216
217
217
test ( 'availableWorkers should return 0 for a stopped pool' , async ( ) => {
218
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
218
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
219
219
await testPool . stop ( ) ;
220
220
expect ( testPool . availableWorkers ) . toEqual ( 0 ) ;
221
221
} ) ;
222
222
223
223
test ( 'availableWorkers should return number of possible free workers' , async ( ) => {
224
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
224
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
225
225
expect ( testPool . availableWorkers ) . toEqual ( 5 ) ;
226
226
const taskResultPromise = testPool . executeTask ( '1' ) ;
227
227
expect ( testPool . availableWorkers ) . toEqual ( 4 ) ;
@@ -230,13 +230,13 @@ describe('DynamicWorkerPool', () => {
230
230
} ) ;
231
231
232
232
test ( 'idleWorkers should return 0 for a stopped pool' , async ( ) => {
233
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
233
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
234
234
await testPool . stop ( ) ;
235
235
expect ( testPool . idleWorkers ) . toEqual ( 0 ) ;
236
236
} ) ;
237
237
238
238
test ( 'idleWorkers should return the number of created workers in idle state' , async ( ) => {
239
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
239
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
240
240
expect ( testPool . idleWorkers ) . toEqual ( 0 ) ;
241
241
const taskResultPromise = testPool . executeTask ( '1' ) ;
242
242
expect ( testPool . idleWorkers ) . toEqual ( 0 ) ;
@@ -245,17 +245,44 @@ describe('DynamicWorkerPool', () => {
245
245
} ) ;
246
246
247
247
test ( 'busyWorkers should return 0 for a stopped pool' , async ( ) => {
248
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
248
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
249
249
await testPool . stop ( ) ;
250
250
expect ( testPool . busyWorkers ) . toEqual ( 0 ) ;
251
251
} ) ;
252
252
253
253
test ( 'busyWorkers should return the number of working workers' , async ( ) => {
254
- const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 ) ;
254
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 0 ) ;
255
255
expect ( testPool . busyWorkers ) . toEqual ( 0 ) ;
256
256
const taskResultPromise = testPool . executeTask ( '1' ) ;
257
257
expect ( testPool . busyWorkers ) . toEqual ( 1 ) ;
258
258
await taskResultPromise ;
259
259
expect ( testPool . busyWorkers ) . toEqual ( 0 ) ;
260
260
} ) ;
261
+
262
+ test ( 'DynamicWorkerPool with idleTimeout should keep idle workers for specified time' , async ( ) => {
263
+ const testPool = new DynamicWorkerPool < string , string , TestWorker > ( workerFactory , 2 , 5 , 100 ) ;
264
+ const workerDisposeSpy = jest . spyOn ( testWorkerInstance , 'dispose' ) ;
265
+
266
+ await Promise . all ( [
267
+ testPool . executeTask ( '1' ) ,
268
+ testPool . executeTask ( '2' ) ,
269
+ testPool . executeTask ( '3' ) ,
270
+ testPool . executeTask ( '4' ) ,
271
+ testPool . executeTask ( '5' )
272
+ ] ) ;
273
+
274
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) ) ;
275
+
276
+ expect ( workerDisposeSpy ) . toHaveBeenCalledTimes ( 0 ) ;
277
+ expect ( testPool . size ) . toEqual ( 5 ) ;
278
+
279
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
280
+
281
+ expect ( testPool . size ) . toEqual ( 2 ) ;
282
+ expect ( workerDisposeSpy ) . toHaveBeenCalledTimes ( 3 ) ;
283
+
284
+ await testPool . stop ( ) ;
285
+ expect ( testPool . size ) . toEqual ( 0 ) ;
286
+ expect ( workerDisposeSpy ) . toHaveBeenCalledTimes ( 5 ) ;
287
+ } ) ;
261
288
} ) ;
0 commit comments