Skip to content

Commit

Permalink
fix: replace delay with setTimeout (#23123)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Jul 6, 2023
1 parent b98ca94 commit 8432e76
Show file tree
Hide file tree
Showing 17 changed files with 19 additions and 33 deletions.
2 changes: 0 additions & 2 deletions lib/config/presets/npm/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as httpMock from '../../../../test/http-mock';
import { GlobalConfig } from '../../global';
import * as npm from '.';

jest.mock('delay');

describe('config/presets/npm/index', () => {
beforeEach(() => {
GlobalConfig.reset();
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/datasource/crate/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import delay from 'delay';
import { setTimeout } from 'timers/promises';
import fs from 'fs-extra';
import _simpleGit, { SimpleGit } from 'simple-git';
import { DirectoryResult, dir } from 'tmp-promise';
Expand Down Expand Up @@ -30,7 +30,7 @@ function setupGitMocks(delayMs?: number): { mockClone: jest.Mock<any, any> } {
.mockImplementation(
async (_registryUrl: string, clonePath: string, _opts) => {
if (delayMs && delayMs > 0) {
await delay(delayMs);
await setTimeout(delayMs);
}

const path = `${clonePath}/my/pk/mypkg`;
Expand Down
2 changes: 0 additions & 2 deletions lib/modules/datasource/npm/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { NpmDatasource, setNpmrc } from '.';

const datasource = NpmDatasource.id;

jest.mock('delay');

let npmResponse: any;

describe('modules/datasource/npm/index', () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/git-submodules/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const Git = jest.requireActual('simple-git') as SimpleGitFactory;

describe('modules/manager/git-submodules/extract', () => {
// flaky ci tests
//jest.setTimeout(10 * 1000);
// jest.setTimeout(10 * 1000);

beforeAll(() => {
simpleGit.mockImplementation((basePath: string) => {
Expand Down
5 changes: 1 addition & 4 deletions lib/modules/platform/azure/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('modules/platform/azure/index', () => {
jest.mock('../../../util/git');
jest.mock('../../../util/host-rules');
jest.mock('../../../logger');
jest.mock('delay');
jest.mock('timers/promises');
hostRules = require('../../../util/host-rules');
require('../../../util/sanitize').sanitize = jest.fn((input) => input);
azure = await import('.');
Expand Down Expand Up @@ -1425,7 +1425,6 @@ describe('modules/platform/azure/index', () => {
branchName: branchNameMock,
id: pullRequestIdMock,
});

expect(getPullRequestByIdMock).toHaveBeenCalledTimes(2);
expect(res).toBeTrue();
});
Expand Down Expand Up @@ -1453,12 +1452,10 @@ describe('modules/platform/azure/index', () => {
azureHelper.getMergeMethod = jest
.fn()
.mockReturnValue(GitPullRequestMergeStrategy.Squash);

const res = await azure.mergePr({
branchName: branchNameMock,
id: pullRequestIdMock,
});

expect(getPullRequestByIdMock).toHaveBeenCalledTimes(
expectedNumRetries + 1
);
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/azure/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from 'timers/promises';
import is from '@sindresorhus/is';
import {
GitPullRequest,
Expand All @@ -8,7 +9,6 @@ import {
GitVersionDescriptor,
PullRequestStatus,
} from 'azure-devops-node-api/interfaces/GitInterfaces.js';
import delay from 'delay';
import JSON5 from 'json5';
import {
REPOSITORY_ARCHIVED,
Expand Down Expand Up @@ -752,7 +752,7 @@ export async function mergePr({
`Updated PR to closed status but change has not taken effect yet. Retrying...`
);

await delay(sleepMs);
await setTimeout(sleepMs);
pr = await azureApiGit.getPullRequestById(pullRequestId, config.project);
isClosed = pr.status === PullRequestStatus.Completed;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/bitbucket-server/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ describe('modules/platform/bitbucket-server/index', () => {
beforeEach(async () => {
// reset module
jest.resetModules();
jest.mock('delay');
jest.mock('timers/promises');
jest.mock('../../../util/git');
jest.mock('../../../util/host-rules');
hostRules = require('../../../util/host-rules');
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/bitbucket-server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import delay from 'delay';
import { setTimeout } from 'timers/promises';
import JSON5 from 'json5';
import type { PartialDeep } from 'type-fest';
import {
Expand Down Expand Up @@ -349,7 +349,7 @@ export async function getBranchPr(branchName: string): Promise<BbsPr | null> {
// istanbul ignore next
export async function refreshPr(number: number): Promise<void> {
// wait for pr change propagation
await delay(1000);
await setTimeout(1000);
// refresh cache
await getPr(number, true);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as github from '.';

const githubApiHost = 'https://api.github.com';

jest.mock('delay');
jest.mock('timers/promises');

jest.mock('../../../util/host-rules');
jest.mock('../../../util/http/queue');
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// TODO: types (#7154)
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import URL from 'node:url';
import { setTimeout } from 'timers/promises';
import is from '@sindresorhus/is';
import delay from 'delay';
import JSON5 from 'json5';
import { DateTime } from 'luxon';
import semver from 'semver';
Expand Down Expand Up @@ -338,7 +338,7 @@ export async function createFork(
}
logger.info({ forkedRepo: forkedRepo.full_name }, 'Created forked repo');
logger.debug(`Sleeping 30s after creating fork`);
await delay(30000);
await setTimeout(30000);
return forkedRepo;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/modules/platform/gitlab/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('modules/platform/gitlab/index', () => {
jest.mock('../../../logger');
logger = (await import('../../../logger')).logger as never;
jest.mock('../../../util/host-rules');
jest.mock('delay');
jest.mock('timers/promises');
hostRules = require('../../../util/host-rules');
jest.mock('../../../util/git');
git = require('../../../util/git');
Expand Down
6 changes: 3 additions & 3 deletions lib/modules/platform/gitlab/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import URL from 'node:url';
import { setTimeout } from 'timers/promises';
import is from '@sindresorhus/is';
import delay from 'delay';
import JSON5 from 'json5';
import semver from 'semver';
import {
Expand Down Expand Up @@ -617,7 +617,7 @@ async function tryPrAutomerge(
if (body.merge_status === desiredStatus && body.pipeline !== null) {
break;
}
await delay(500 * attempt);
await setTimeout(500 * attempt);
}

await gitlabApi.putJson(
Expand Down Expand Up @@ -866,7 +866,7 @@ export async function setBranchStatus({
}
try {
// give gitlab some time to create pipelines for the sha
await delay(1000);
await setTimeout(1000);

await gitlabApi.postJson(url, { body: options });

Expand Down
2 changes: 1 addition & 1 deletion lib/util/git/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { setNoVerify } from '.';
jest.mock('./conflicts-cache');
jest.mock('./behind-base-branch-cache');
jest.mock('./modified-cache');
jest.mock('delay');
jest.mock('timers/promises');
jest.mock('../cache/repository');
const behindBaseCache = mocked(_behindBaseCache);
const conflictsCache = mocked(_conflictsCache);
Expand Down
4 changes: 2 additions & 2 deletions lib/util/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import URL from 'node:url';
import { setTimeout } from 'timers/promises';
import is from '@sindresorhus/is';
import delay from 'delay';
import fs from 'fs-extra';
// TODO: check if bug is fixed (#7154)
// eslint-disable-next-line import/no-named-as-default
Expand Down Expand Up @@ -103,7 +103,7 @@ export async function gitRetry<T>(gitFunc: () => Promise<T>): Promise<T> {

const nextDelay = delayFactor ^ ((round - 1) * delaySeconds);
logger.trace({ nextDelay }, `Delay next round`);
await delay(1000 * nextDelay);
await setTimeout(1000 * nextDelay);

round++;
}
Expand Down
1 change: 0 additions & 1 deletion lib/workers/global/config/parse/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe('workers/global/config/parse/index', () => {
'./__fixtures__/default.js'
),
};
jest.mock('delay', () => Promise.resolve());
});

it('supports token in env', async () => {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@
"commander": "11.0.0",
"conventional-commits-detector": "1.0.3",
"deepmerge": "4.3.1",
"delay": "5.0.0",
"dequal": "2.0.3",
"detect-indent": "6.1.0",
"editorconfig": "2.0.0",
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4999,11 +4999,6 @@ define-properties@^1.1.3, define-properties@^1.1.4, define-properties@^1.2.0:
has-property-descriptors "^1.0.0"
object-keys "^1.1.1"

delay@5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/delay/-/delay-5.0.0.tgz#137045ef1b96e5071060dd5be60bf9334436bd1d"
integrity sha512-ReEBKkIfe4ya47wlPYf/gu5ib6yUG0/Aez0JQZQz94kiWtRQvZIQbTiehsnwHvLSWJnQdhVeqYue7Id1dKr0qw==

delegates@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
Expand Down

0 comments on commit 8432e76

Please sign in to comment.