diff --git a/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry.ts b/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry.ts index 614f57064512c..c1dc9fd91d959 100644 --- a/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry.ts +++ b/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry.ts @@ -12,8 +12,13 @@ import { retryForSuccess } from './retry_for_success'; import { retryForTruthy } from './retry_for_truthy'; export interface TryWithRetriesOptions { + // The initial delay before the first attempt + initialDelay?: number; + // The number of retry attempts retryCount: number; + // The delay between retry attempts retryDelay?: number; + // The timeout for the retry attempts timeout?: number; } @@ -108,7 +113,12 @@ export class RetryService extends FtrService { options: TryWithRetriesOptions, onFailureBlock?: () => Promise ): Promise { - const { retryCount, timeout = this.config.get('timeouts.try'), retryDelay = 200 } = options; + const { + retryCount, + timeout = this.config.get('timeouts.try'), + retryDelay = 200, + initialDelay, + } = options; return await retryForSuccess(this.log, { description, @@ -118,6 +128,7 @@ export class RetryService extends FtrService { onFailureBlock, retryDelay, retryCount, + initialDelay, }); } } diff --git a/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry_for_success.test.ts b/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry_for_success.test.ts index 4ad240663d5a8..bcc12644411a7 100644 --- a/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry_for_success.test.ts +++ b/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry_for_success.test.ts @@ -7,10 +7,11 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -import { retryForSuccess } from './retry_for_success'; +import * as retryForSuccessModule from './retry_for_success'; import { ToolingLog, ToolingLogCollectingWriter } from '@kbn/tooling-log'; describe('Retry for success', () => { + const { retryForSuccess } = retryForSuccessModule; it(`should print out attempt counts with the retryCount parameter`, async () => { const retryCount = 3; const log = new ToolingLog(); @@ -67,4 +68,20 @@ describe('Retry for success', () => { ] `); }); + it.skip('should call delay with initialDelay if initialDelay is provided', async () => { + const delaySpy = jest.spyOn(retryForSuccessModule, 'delay').mockResolvedValue(undefined); + const log = new ToolingLog(); + const block = async () => 42; + const initialDelay = 1234; + + await retryForSuccessModule.retryForSuccess(log, { + block, + timeout: 2000, + methodName: 'retryForSuccess initialDelay test', + initialDelay, + }); + + expect(delaySpy).toHaveBeenCalledWith(initialDelay); + delaySpy.mockRestore(); + }); }); diff --git a/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry_for_success.ts b/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry_for_success.ts index f44e2618190fc..f5386b0933607 100644 --- a/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry_for_success.ts +++ b/src/platform/packages/shared/kbn-ftr-common-functional-services/services/retry/retry_for_success.ts @@ -10,9 +10,8 @@ import { ToolingLog } from '@kbn/tooling-log'; import { inspect } from 'util'; -const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); - const returnTrue = () => true; +export const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const defaultOnFailure = (methodName: string) => (lastError: Error | undefined, reason: string) => { throw new Error( @@ -48,6 +47,7 @@ interface Options { description?: string; retryDelay?: number; retryCount?: number; + initialDelay?: number; } export async function retryForSuccess(log: ToolingLog, options: Options) { @@ -61,8 +61,13 @@ export async function retryForSuccess(log: ToolingLog, options: Options) { accept = returnTrue, retryDelay = 502, retryCount, + initialDelay, } = options; + if (typeof initialDelay === 'number') { + await delay(initialDelay); + } + const start = Date.now(); const criticalWebDriverErrors = ['NoSuchSessionError', 'NoSuchWindowError']; let lastError; diff --git a/test/api_integration/apis/ui_counters/ui_counters.ts b/test/api_integration/apis/ui_counters/ui_counters.ts index 1f8823d94eebc..8578ae4e0690c 100644 --- a/test/api_integration/apis/ui_counters/ui_counters.ts +++ b/test/api_integration/apis/ui_counters/ui_counters.ts @@ -15,8 +15,6 @@ import { FtrProviderContext } from '../../ftr_provider_context'; const APP_NAME = 'myApp'; -const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); - export default function ({ getService }: FtrProviderContext) { const supertest = getService('supertest'); const esArchiver = getService('esArchiver'); @@ -94,7 +92,6 @@ export default function ({ getService }: FtrProviderContext) { await sendReport(report); // Wait for the report to be query-able in ES since sending report uses (refresh = false) - await delay(3000); await retry.tryWithRetries( 'reported events to be stored into ES', async () => { @@ -106,7 +103,7 @@ export default function ({ getService }: FtrProviderContext) { expect(countTypeEvents[0].attributes.count).to.eql(1); return true; }, - { retryCount: 6, retryDelay: 1500 } + { retryCount: 6, retryDelay: 1500, initialDelay: 3000 } ); }); @@ -124,7 +121,6 @@ export default function ({ getService }: FtrProviderContext) { await sendReport(report); // Wait for the report to be query-able in ES since sending report uses (refresh = false) - await delay(3000); await retry.tryWithRetries( 'reported events to be stored into ES', async () => { @@ -154,7 +150,7 @@ export default function ({ getService }: FtrProviderContext) { expect(secondEventWithCountTypeEvents[0].attributes.count).to.eql(1); return true; }, - { retryCount: 6, retryDelay: 1500 } + { retryCount: 6, retryDelay: 1500, initialDelay: 3000 } ); }); });