import { delay , nonNullProp } from '../extension.bundle' ; import { cleanTestWorkspace , longRunningTestsEnabled } from './global.test' ; export interface ParallelTest { title : string ; callback ( ) : Promise < void > ; task ? : Promise < void > ; skip ? : boolean ; /**
     * Turn off parallel behavior for this test
     */ suppressParallel ? : boolean ; } export interface ParallelSuiteOptions { title : string ; timeoutMS ? : number ; isLongRunning ? : boolean ; suiteSetup ? : ( testContext : Mocha . Context ) => Promise < void > ; suiteTeardown ? : ( testContext : Mocha . Context ) => Promise < void > ; /**
     * Turn off parallel behavior for this suite
     */ suppressParallel ? : boolean ; /**
     * Add any other tests you may want for this suite
     */ addTests ? : ( ) => void ; } export function addParallelSuite ( parallelTests , options ) { suite ( options . title , function ( this ) : void { this . timeout ( options . timeoutMS || 30 * 1000 ) ; suiteSetup ( async function ( this ) : Promise < void > { if ( options . isLongRunning && ! longRunningTestsEnabled ) { this . skip ( ) ; } await cleanTestWorkspace ( ) ; if ( options . suiteSetup ) { await options . suiteSetup ( this ) ; } if ( ! options . suppressParallel ) { for ( const t of parallelTests ) { if ( ! t . suppressParallel ) { const mochaGrep = process . env [ 'MOCHA_grep' ] ; t . skip = t . skip || ( ! ! mochaGrep && ! ( new RegExp ( mochaGrep ) . test ( `${ options . title } ${ t . title }` ) ) ) ; if ( ! t . skip ) { t . task = t . callback ( ) ; } } } } } ) ; suiteTeardown ( async function ( this ) : Promise < void > { if ( ! options . isLongRunning || longRunningTestsEnabled ) { if ( options . suiteTeardown ) { await options . suiteTeardown ( this ) ; } await cleanTestWorkspace ( ) ; } } ) ; for ( const t of parallelTests ) { test ( t . title , async function ( this ) : Promise < void > { if ( t . skip ) { this . skip ( ) ; } else if ( options . suppressParallel || t . suppressParallel ) { await t . callback ( ) ; } else { await nonNullProp ( t , 'task' ) ; } } ) ; } if ( options . addTests ) { options . addTests ( ) ; } } ) ; } const isRunningMap = new Map < string , boolean > ( ) ; export async function runInSeries ( key , callback ) { while ( isRunningMap . get ( key ) ) { await delay ( 1000 ) ; } try { isRunningMap . set ( key , true ) ; await callback ( ) ; } finally { isRunningMap . set ( key , false ) ; } }