'use strict' ; import assert from 'assert' ; import { Application , Agent } from 'egg' ; import { prepareTestApplication , testClear , waitFor } from '../../Util' ; import { AgentWorkerJobHandler , SchedulerAgentScheduleEvent } from '../../../app/plugin/scheduler-manager/AgentWorkerJobHandler' ; import { BasicJobHandler } from '../../../app/plugin/scheduler-manager/BasicJobHandler' ; describe ( 'AppSchedulerManager' , ( ) => { let app ; let agent ; beforeEach ( async ( ) => { ( { app , agent } = await prepareTestApplication ( ) ) ; } ) ; afterEach ( ( ) => { testClear ( app , agent ) ; } ) ; class MockJob { iCounter = 0 ; invoke ( ) { this . iCounter ++ ; } } describe ( 'register' , ( ) => { it ( 'should return AgentWorkerJobHandler' , async ( ) => { const result = app . sched . register ( 'testName' , '0' , 'worker' , ( ) => { } ) ; assert ( result instanceof AgentWorkerJobHandler ) ; } ) ; it ( 'should return BasicJobHandler' , async ( ) => { const result = app . sched . register ( 'testName' , '0' , 'workers' , ( ) => { } ) ; assert ( result instanceof BasicJobHandler ) ; } ) ; it ( 'should throw Error' , async ( ) => { let result ; assert . throws ( ( ) => { result = ( app . sched as any ) . register ( 'testName' , '0' , 'agent' , ( ) => { } ) ; } , new Error ( 'Not supported type agent.' ) ) ; assert . deepStrictEqual ( result , undefined ) ; } ) ; } ) ; describe ( 'scheduleAgentJob' , ( ) => { it ( 'should invoke job when handlerMap contains name' , async ( ) => { const job = new MockJob ( ) as any ; ( app . sched as any ) . scheduleAgentJob ( 'testName' ) ; assert . deepStrictEqual ( job . iCounter , 0 ) ; ( app . sched as any ) . handlerMap . set ( 'testName' , job ) ; ( app . sched as any ) . scheduleAgentJob ( 'testName' ) ; assert . deepStrictEqual ( job . iCounter , 1 ) ; } ) ; } ) ; describe ( 'onReady' , ( ) => { it ( 'should call this.scheduleAgentJob' , async ( ) => { let counter = 0 ; ( app . sched as any ) . scheduleAgentJob = ( ( ) => { counter ++ ; } ) ; agent . event . publish ( 'worker' , SchedulerAgentScheduleEvent , { name : 'testName' } ) ; await waitFor ( 5 ) ; assert . strictEqual ( counter , 1 ) ; } ) ; } ) ; } ) ;