diff --git a/src/compose/images.ts b/src/compose/images.ts index d7eee21a..69427e6c 100644 --- a/src/compose/images.ts +++ b/src/compose/images.ts @@ -22,6 +22,7 @@ import type { Service } from './service'; import { strict as assert } from 'assert'; import log from '../lib/supervisor-console'; +import { setTimeout } from 'timers/promises'; interface FetchProgressEvent { percentage: number; @@ -695,7 +696,7 @@ async function removeImageIfNotNeeded(image: Image): Promise { // that can lead to weird behavior with the error // `(HTTP code 500) server error - unrecognized image ID`. // This random delay tries to prevent that - await new Promise((resolve) => setTimeout(resolve, Math.random() * 100)); + await setTimeout(Math.random() * 100); // Remove all matching tags in sequence // as removing in parallel causes some engine weirdness (see above) diff --git a/src/lib/backoff.ts b/src/lib/backoff.ts index 2fcf2dd7..f4f463b6 100644 --- a/src/lib/backoff.ts +++ b/src/lib/backoff.ts @@ -38,7 +38,8 @@ export function withBackoff any>( // otherwise use the actual return type TReturn = ReturnType extends Promise ? R : ReturnType; - // TODO use standard lib async setTimout (requires node 16) + // TODO: use standard lib async setTimeout (Node 16). Tests for backoff will fail + // due to Sinon fake timers, so those will need to be redone too. const sleep = promisify(setTimeout); const normalizedOptions: Options = { diff --git a/test/integration/device-state.spec.ts b/test/integration/device-state.spec.ts index 0cef38f8..f194e343 100644 --- a/test/integration/device-state.spec.ts +++ b/test/integration/device-state.spec.ts @@ -13,6 +13,7 @@ import { initializeContractRequirements } from '~/lib/contracts'; import { testfs } from 'mocha-pod'; import { createDockerImage } from '~/test-lib/docker-helper'; import * as Docker from 'dockerode'; +import { setTimeout } from 'timers/promises'; describe('device-state', () => { const docker = new Docker(); @@ -238,7 +239,7 @@ describe('device-state', () => { ).to.be.rejected; }); - it('allows triggering applying the target state', (done) => { + it('allows triggering applying the target state', async () => { const applyTargetStub = sinon .stub(deviceState, 'applyTarget') .returns(Promise.resolve()); @@ -246,14 +247,12 @@ describe('device-state', () => { deviceState.triggerApplyTarget({ force: true }); expect(applyTargetStub).to.not.be.called; - setTimeout(() => { - expect(applyTargetStub).to.be.calledWith({ - force: true, - initial: false, - }); - applyTargetStub.restore(); - done(); - }, 1000); + await setTimeout(1000); + expect(applyTargetStub).to.be.calledWith({ + force: true, + initial: false, + }); + applyTargetStub.restore(); }); it('accepts a target state with an valid contract', async () => { diff --git a/test/legacy/40-target-state.spec.ts b/test/legacy/40-target-state.spec.ts index 778ffd06..814dd829 100644 --- a/test/legacy/40-target-state.spec.ts +++ b/test/legacy/40-target-state.spec.ts @@ -4,12 +4,12 @@ import * as _ from 'lodash'; import rewire = require('rewire'); import { expect } from 'chai'; -import { sleep } from '~/test-lib/helpers'; import * as TargetState from '~/src/device-state/target-state'; import Log from '~/lib/supervisor-console'; import * as request from '~/lib/request'; import * as deviceConfig from '~/src/device-config'; import { UpdatesLockedError } from '~/lib/errors'; +import { setTimeout } from 'timers/promises'; const deviceState = rewire('~/src/device-state'); @@ -210,7 +210,7 @@ describe('Target state', () => { await TargetState.update(false, false); // Wait for interval to tick a few times - await sleep(2000); // 2 seconds + await setTimeout(2000); // 2 seconds // Trigger another update but say it's from the API await TargetState.update(false, true); diff --git a/test/lib/helpers.ts b/test/lib/helpers.ts deleted file mode 100644 index 19afe385..00000000 --- a/test/lib/helpers.ts +++ /dev/null @@ -1,6 +0,0 @@ -/** - * Delays for input ms before resolving - */ -export async function sleep(ms: number): Promise { - return new Promise((resolve) => setTimeout(resolve, ms)); -}