mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-04-07 19:34:17 +00:00
Use timers/promises for promisified setTimeout
Change-type: patch
This commit is contained in:
parent
80f52b796a
commit
b8891ebb08
@ -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<void> {
|
||||
// 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)
|
||||
|
@ -38,7 +38,8 @@ export function withBackoff<T extends (...args: any[]) => any>(
|
||||
// otherwise use the actual return
|
||||
type TReturn = ReturnType<T> extends Promise<infer R> ? R : ReturnType<T>;
|
||||
|
||||
// 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 = {
|
||||
|
@ -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 () => {
|
||||
|
@ -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);
|
||||
|
@ -1,6 +0,0 @@
|
||||
/**
|
||||
* Delays for input ms before resolving
|
||||
*/
|
||||
export async function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user