Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -108,7 +113,12 @@ export class RetryService extends FtrService {
options: TryWithRetriesOptions,
onFailureBlock?: () => Promise<T>
): Promise<T> {
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<T>(this.log, {
description,
Expand All @@ -118,6 +128,7 @@ export class RetryService extends FtrService {
onFailureBlock,
retryDelay,
retryCount,
initialDelay,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -48,6 +47,7 @@ interface Options<T> {
description?: string;
retryDelay?: number;
retryCount?: number;
initialDelay?: number;
}

export async function retryForSuccess<T>(log: ToolingLog, options: Options<T>) {
Expand All @@ -61,8 +61,13 @@ export async function retryForSuccess<T>(log: ToolingLog, options: Options<T>) {
accept = returnTrue,
retryDelay = 502,
retryCount,
initialDelay,
} = options;

if (typeof initialDelay === 'number') {
await delay(initialDelay);
}

const start = Date.now();
const criticalWebDriverErrors = ['NoSuchSessionError', 'NoSuchWindowError'];
let lastError;
Expand Down
8 changes: 2 additions & 6 deletions test/api_integration/apis/ui_counters/ui_counters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 }
);
});

Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 }
);
});
});
Expand Down