import { Executor } from './executor' ; import { WorkNode } from '../planner/work-node' ; import { ExecutionContext } from './execution-context' ; import { listenOnAbort } from '../utils/abort-event' ; export interface ExecutorMock extends Executor { waitForExecution ( nodeId ) : Promise < NodeHandle > ; abort ( nodeId , err ) : void ; end ( nodeId ) : void ; } export interface NodeHandle { fail ( err ) : void ; end ( ) : void ; } export function getExecutorMock ( ) { const waits = { } ; const execs = { } ; return { restore ( ) { return Promise . resolve ( ) ; } , store ( ) { return Promise . resolve ( ) ; } , clean ( ) { return Promise . resolve ( ) ; } , exec ( node , context , cancelDefer ) { return new Promise < void > ( ( resolve , reject ) => { const resultDefer = { end ( ) { delete execs [ node . id ] ; resolve ( ) ; } , fail ( err ) { delete execs [ node . id ] ; reject ( err ) ; } , } ; listenOnAbort ( cancelDefer . signal , ( ) => { reject ( new Error ( 'canceled' ) ) ; } ) ; if ( waits [ node . id ] ) { waits [ node . id ] ( resultDefer ) ; delete waits [ node . id ] ; } else { execs [ node . id ] = resultDefer ; } } ) ; } , abort ( nodeId , err ) { execs [ nodeId ] . fail ( err ) ; } , end ( nodeId ) { execs [ nodeId ] . end ( ) ; } , async waitForExecution ( nodeId ) { const currentExecs = execs [ nodeId ] ; if ( currentExecs ) { return currentExecs ; } const promise = new Promise < NodeHandle > ( ( resolve ) => { waits [ nodeId ] = resolve ; } ) ; return promise ; } , } ; }