import sinon from 'sinon' ; import { elasticsearchServiceMock , savedObjectsRepositoryMock , } from '../../../../../src/core/server/mocks' ; import { SavedObjectsErrorHelpers , Logger } from '../../../../../src/core/server' ; import { ADJUST_THROUGHPUT_INTERVAL } from '../lib/create_managed_configuration' ; import { TaskManagerPlugin , TaskManagerStartContract } from '../plugin' ; import { coreMock } from '../../../../../src/core/server/mocks' ; import { TaskManagerConfig } from '../config' ; describe . skip ( 'managed configuration' , ( ) => { let taskManagerStart ; let logger ; let clock ; const savedObjectsClient = savedObjectsRepositoryMock . create ( ) ; const esStart = elasticsearchServiceMock . createStart ( ) ; beforeEach ( async ( ) => { jest . resetAllMocks ( ) ; clock = sinon . useFakeTimers ( ) ; const context = coreMock . createPluginInitializerContext < TaskManagerConfig > ( { max_workers : 10 , max_attempts : 9 , poll_interval : 3000 , version_conflict_threshold : 80 , max_poll_inactivity_cycles : 10 , monitored_aggregated_stats_refresh_rate : 60000 , monitored_stats_health_verbose_log : { enabled : false , warn_delayed_task_start_in_seconds : 60 , } , monitored_stats_required_freshness : 4000 , monitored_stats_running_average_window : 50 , request_capacity : 1000 , monitored_task_execution_thresholds : { default : { error_threshold : 90 , warn_threshold : 80 , } , custom : { } , } , ephemeral_tasks : { enabled : true , request_capacity : 10 , } , unsafe : { exclude_task_types : [ ] , } , } ) ; logger = context . logger . get ( 'taskManager' ) ; const taskManager = new TaskManagerPlugin ( context ) ; ( await taskManager . setup ( coreMock . createSetup ( ) , { usageCollection : undefined } ) ) . registerTaskDefinitions ( { foo : { title : 'Foo' , createTaskRunner : jest . fn ( ) , } , } ) ; const coreStart = coreMock . createStart ( ) ; coreStart . elasticsearch = esStart ; coreStart . savedObjects . createInternalRepository . mockReturnValue ( savedObjectsClient ) ; taskManagerStart = await taskManager . start ( coreStart ) ; clock . tick ( 0 ) ; } ) ; afterEach ( ( ) => clock . restore ( ) ) ; test ( 'should lower max workers when Elasticsearch returns 429 error' , async ( ) => { savedObjectsClient . create . mockRejectedValueOnce ( SavedObjectsErrorHelpers . createTooManyRequestsError ( 'a' , 'b' ) ) ; await expect ( taskManagerStart . schedule ( { taskType : 'foo' , state : { } , params : { } , } ) ) . rejects . toThrowErrorMatchingInlineSnapshot ( `"Too Many Requests"` ) ; clock . tick ( ADJUST_THROUGHPUT_INTERVAL ) ; expect ( logger . warn ) . toHaveBeenCalledWith ( 'Max workers configuration is temporarily reduced after Elasticsearch returned 1 "too many request" and/or "execute [inline] script" error(s).' ) ; expect ( logger . debug ) . toHaveBeenCalledWith ( 'Max workers configuration changing from 10 to 8 after seeing 1 "too many request" and/or "execute [inline] script" error(s)' ) ; expect ( logger . debug ) . toHaveBeenCalledWith ( 'Task pool now using 10 as the max worker value' ) ; } ) ; test ( 'should increase poll interval when Elasticsearch returns 429 error' , async ( ) => { savedObjectsClient . create . mockRejectedValueOnce ( SavedObjectsErrorHelpers . createTooManyRequestsError ( 'a' , 'b' ) ) ; await expect ( taskManagerStart . schedule ( { taskType : 'foo' , state : { } , params : { } , } ) ) . rejects . toThrowErrorMatchingInlineSnapshot ( `"Too Many Requests"` ) ; clock . tick ( ADJUST_THROUGHPUT_INTERVAL ) ; expect ( logger . warn ) . toHaveBeenCalledWith ( 'Poll interval configuration is temporarily increased after Elasticsearch returned 1 "too many request" and/or "execute [inline] script" error(s).' ) ; expect ( logger . debug ) . toHaveBeenCalledWith ( 'Poll interval configuration changing from 3000 to 3600 after seeing 1 "too many request" and/or "execute [inline] script" error(s)' ) ; expect ( logger . debug ) . toHaveBeenCalledWith ( 'Task poller now using interval of 3600ms' ) ; } ) ; test ( 'should lower max workers when Elasticsearch returns "cannot execute [inline] scripts" error' , async ( ) => { esStart . createClient ( 'taskManager' ) . asInternalUser . search . mockRejectedValueOnce ( elasticsearchServiceMock . createErrorTransportRequestPromise ( new Error ( 'cannot execute [inline] scripts" error' ) ) ) ; await expect ( taskManagerStart . fetch ( { } ) ) . rejects . toThrowErrorMatchingInlineSnapshot ( `"cannot execute [inline] scripts" error"` ) ; clock . tick ( ADJUST_THROUGHPUT_INTERVAL ) ; expect ( logger . warn ) . toHaveBeenCalledWith ( 'Max workers configuration is temporarily reduced after Elasticsearch returned 1 "too many request" and/or "execute [inline] script" error(s).' ) ; expect ( logger . debug ) . toHaveBeenCalledWith ( 'Max workers configuration changing from 10 to 8 after seeing 1 "too many request" and/or "execute [inline] script" error(s)' ) ; expect ( logger . debug ) . toHaveBeenCalledWith ( 'Task pool now using 10 as the max worker value' ) ; } ) ; test ( 'should increase poll interval when Elasticsearch returns "cannot execute [inline] scripts" error' , async ( ) => { esStart . createClient ( 'taskManager' ) . asInternalUser . search . mockRejectedValueOnce ( elasticsearchServiceMock . createErrorTransportRequestPromise ( new Error ( 'cannot execute [inline] scripts" error' ) ) ) ; await expect ( taskManagerStart . fetch ( { } ) ) . rejects . toThrowErrorMatchingInlineSnapshot ( `"cannot execute [inline] scripts" error"` ) ; clock . tick ( ADJUST_THROUGHPUT_INTERVAL ) ; expect ( logger . warn ) . toHaveBeenCalledWith ( 'Poll interval configuration is temporarily increased after Elasticsearch returned 1 "too many request" and/or "execute [inline] script" error(s).' ) ; expect ( logger . debug ) . toHaveBeenCalledWith ( 'Poll interval configuration changing from 3000 to 3600 after seeing 1 "too many request" and/or "execute [inline] script" error(s)' ) ; expect ( logger . debug ) . toHaveBeenCalledWith ( 'Task poller now using interval of 3600ms' ) ; } ) ; } ) ;