@@ -40,7 +40,8 @@ import type { RegisteredListener } from 'playwright-core/lib/utils';
4040export type EnvByProjectId = Map < string , Record < string , string | undefined > > ;
4141
4242export class Dispatcher {
43- private _workerSlots : { busy : boolean , worker ?: WorkerHost , jobDispatcher ?: JobDispatcher } [ ] = [ ] ;
43+ // Worker slot is claimed when it has jobDispatcher assigned.
44+ private _workerSlots : { worker ?: WorkerHost , jobDispatcher ?: JobDispatcher } [ ] = [ ] ;
4445 private _queue : TestGroup [ ] = [ ] ;
4546 private _workerLimitPerProjectId = new Map < string , number > ( ) ;
4647 private _queuedOrRunningHashCount = new Map < string , number > ( ) ;
@@ -71,7 +72,7 @@ export class Dispatcher {
7172 const projectIdWorkerLimit = this . _workerLimitPerProjectId . get ( job . projectId ) ;
7273 if ( ! projectIdWorkerLimit )
7374 return index ;
74- const runningWorkersWithSameProjectId = this . _workerSlots . filter ( w => w . busy && w . worker && w . worker . projectId ( ) === job . projectId ) . length ;
75+ const runningWorkersWithSameProjectId = this . _workerSlots . filter ( w => w . jobDispatcher ?. job . projectId === job . projectId ) . length ;
7576 if ( runningWorkersWithSameProjectId < projectIdWorkerLimit )
7677 return index ;
7778 }
@@ -92,9 +93,9 @@ export class Dispatcher {
9293 const job = this . _queue [ jobIndex ] ;
9394
9495 // 2. Find a worker with the same hash, or just some free worker.
95- let workerIndex = this . _workerSlots . findIndex ( w => ! w . busy && w . worker && w . worker . hash ( ) === job . workerHash && ! w . worker . didSendStop ( ) ) ;
96+ let workerIndex = this . _workerSlots . findIndex ( w => ! w . jobDispatcher && w . worker && w . worker . hash ( ) === job . workerHash && ! w . worker . didSendStop ( ) ) ;
9697 if ( workerIndex === - 1 )
97- workerIndex = this . _workerSlots . findIndex ( w => ! w . busy ) ;
98+ workerIndex = this . _workerSlots . findIndex ( w => ! w . jobDispatcher ) ;
9899 if ( workerIndex === - 1 ) {
99100 // No workers available, bail out.
100101 return ;
@@ -103,15 +104,13 @@ export class Dispatcher {
103104 // 3. Claim both the job and the worker slot.
104105 this . _queue . splice ( jobIndex , 1 ) ;
105106 const jobDispatcher = new JobDispatcher ( job , this . _config , this . _reporter , this . _failureTracker , ( ) => this . stop ( ) . catch ( ( ) => { } ) ) ;
106- this . _workerSlots [ workerIndex ] . busy = true ;
107107 this . _workerSlots [ workerIndex ] . jobDispatcher = jobDispatcher ;
108108
109109 // 4. Run the job. This is the only async operation.
110110 void this . _runJobInWorker ( workerIndex , jobDispatcher ) . then ( ( ) => {
111111
112112 // 5. Release the worker slot.
113113 this . _workerSlots [ workerIndex ] . jobDispatcher = undefined ;
114- this . _workerSlots [ workerIndex ] . busy = false ;
115114
116115 // 6. Check whether we are done or should schedule another job.
117116 this . _checkFinished ( ) ;
@@ -178,7 +177,7 @@ export class Dispatcher {
178177 return ;
179178
180179 // Make sure all workers have finished the current job.
181- if ( this . _workerSlots . some ( w => w . busy ) )
180+ if ( this . _workerSlots . some ( w => ! ! w . jobDispatcher ) )
182181 return ;
183182
184183 this . _finished . resolve ( ) ;
@@ -209,7 +208,7 @@ export class Dispatcher {
209208 void this . stop ( ) ;
210209 // 1. Allocate workers.
211210 for ( let i = 0 ; i < this . _config . config . workers ; i ++ )
212- this . _workerSlots . push ( { busy : false } ) ;
211+ this . _workerSlots . push ( { } ) ;
213212 // 2. Schedule enough jobs.
214213 for ( let i = 0 ; i < this . _workerSlots . length ; i ++ )
215214 this . _scheduleJob ( ) ;
0 commit comments