@@ -291,9 +291,9 @@ it('checks that once a sticked task finishes, next time is sent to that worker',
291
291
await promise ;
292
292
293
293
// Note that the stickiness is not created by the method name or the arguments
294
- // it is solely controlled by the provided "computeWorkerKey" method, which in the
295
- // test example always returns the same key, so all calls should be redirected
296
- // to worker 1 (which is the one that resolved the first call).
294
+ // it is solely controlled by the provided "computeWorkerKey" method, which in
295
+ // the test example always returns the same key, so all calls should be
296
+ // redirected to worker 1 (which is the one that resolved the first call).
297
297
farm . bar ( ) ;
298
298
299
299
// The first time, a call with a "1234567890abcdef" hash had never been done
@@ -325,3 +325,42 @@ it('checks that once a non-sticked task finishes, next time is sent to all worke
325
325
expect ( mockWorkers [ 1 ] . send ) . toHaveBeenCalledTimes ( 2 ) ;
326
326
expect ( mockWorkers [ 2 ] . send ) . toHaveBeenCalledTimes ( 2 ) ;
327
327
} ) ;
328
+
329
+ it ( 'rotates workers when they are idling' , async ( ) => {
330
+ let order ;
331
+ let promise ;
332
+
333
+ // Note there is no "computeWorkerKey".
334
+ const farm = new Farm ( '/tmp/baz.js' , {
335
+ exposedMethods : [ 'foo' , 'bar' ] ,
336
+ numWorkers : 3 ,
337
+ } ) ;
338
+
339
+ [ 0 , 1 , 2 ] . forEach ( i => {
340
+ mockWorkers [ i ] . send . mockReset ( ) ;
341
+ mockWorkers [ i ] . send . mockImplementation ( ( ) => order . push ( i ) ) ;
342
+ } ) ;
343
+
344
+ // First time, the order is 0, 1, 2.
345
+ order = [ ] ;
346
+ promise = farm . foo ( 'car' , 'plane' ) ;
347
+ expect ( order ) . toEqual ( [ 0 , 1 , 2 ] ) ;
348
+
349
+ // Worker 1 successfully replies with "17" as a result.
350
+ workerReply ( 1 , null , 17 ) ;
351
+ await promise ;
352
+
353
+ [ 0 , 1 , 2 ] . forEach ( i => {
354
+ mockWorkers [ i ] . send . mockReset ( ) ;
355
+ mockWorkers [ i ] . send . mockImplementation ( ( ) => order . push ( i ) ) ;
356
+ } ) ;
357
+
358
+ // Now, the order is 1, 2, 0 (shifted one).
359
+ order = [ ] ;
360
+ promise = farm . foo ( 'car' , 'plane' ) ;
361
+ expect ( order ) . toEqual ( [ 1 , 2 , 0 ] ) ;
362
+
363
+ // Worker 1 successfully replies again.
364
+ workerReply ( 1 , null , 17 ) ;
365
+ await promise ;
366
+ } ) ;
0 commit comments