mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-02-20 17:52:51 +00:00
Improve naming of a util function & add unit test
isOlderThan -> isValidDateAndOlderThan See: https://github.com/balena-os/balena-supervisor/pull/2170#discussion_r1226809686 Signed-off-by: Christina Ying Wang <christina@balena.io>
This commit is contained in:
parent
ab80f198d8
commit
ace642ea0f
@ -12,7 +12,7 @@ import {
|
||||
generateStep,
|
||||
CompositionStepAction,
|
||||
} from './composition-steps';
|
||||
import { isOlderThan } from './utils';
|
||||
import { isValidDateAndOlderThan } from './utils';
|
||||
import * as targetStateCache from '../device-state/target-state-cache';
|
||||
import { getNetworkGateway } from '../lib/docker-utils';
|
||||
import * as constants from '../lib/constants';
|
||||
@ -671,7 +671,7 @@ export class App {
|
||||
} else if (target.config.running !== current.config.running) {
|
||||
if (
|
||||
this.requirementsMetForSpecialStart(current, target) &&
|
||||
!isOlderThan(current.createdAt, SECONDS_TO_WAIT_FOR_START)
|
||||
!isValidDateAndOlderThan(current.createdAt, SECONDS_TO_WAIT_FOR_START)
|
||||
) {
|
||||
return generateStep('noop', {});
|
||||
}
|
||||
|
@ -694,8 +694,20 @@ export function dockerMountToServiceMount(
|
||||
return mount as LongDefinition;
|
||||
}
|
||||
|
||||
export function isOlderThan(currentTime: Date | null, seconds: number) {
|
||||
if (currentTime == null) {
|
||||
function isDate(d: unknown): d is Date {
|
||||
return d instanceof Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param currentTime Date instance
|
||||
* @param seconds time in seconds to check against currentTime
|
||||
* @returns
|
||||
*/
|
||||
export function isValidDateAndOlderThan(
|
||||
currentTime: unknown,
|
||||
seconds: number,
|
||||
): boolean {
|
||||
if (!isDate(currentTime)) {
|
||||
return false;
|
||||
}
|
||||
return new Date().getTime() - currentTime.getTime() > seconds * 1000;
|
||||
|
@ -1,8 +1,8 @@
|
||||
import { expect } from 'chai';
|
||||
import * as ComposeUtils from '~/src/compose/utils';
|
||||
|
||||
describe('compose/utils', () =>
|
||||
it('Should correctly camel case the configuration', function () {
|
||||
describe('compose/utils', () => {
|
||||
it('should correctly camel case the configuration', () => {
|
||||
const config = {
|
||||
networks: ['test', 'test2'],
|
||||
};
|
||||
@ -10,4 +10,34 @@ describe('compose/utils', () =>
|
||||
expect(ComposeUtils.camelCaseConfig(config)).to.deep.equal({
|
||||
networks: ['test', 'test2'],
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
it('should return whether a date is valid and older than an interval of seconds', () => {
|
||||
const now = new Date(Date.now());
|
||||
expect(ComposeUtils.isValidDateAndOlderThan(now, 60)).to.equal(false);
|
||||
const time59SecondsAgo = new Date(Date.now() - 59 * 1000);
|
||||
expect(ComposeUtils.isValidDateAndOlderThan(time59SecondsAgo, 60)).to.equal(
|
||||
false,
|
||||
);
|
||||
const time61SecondsAgo = new Date(Date.now() - 61 * 1000);
|
||||
expect(ComposeUtils.isValidDateAndOlderThan(time61SecondsAgo, 60)).to.equal(
|
||||
true,
|
||||
);
|
||||
|
||||
const notDates = [
|
||||
null,
|
||||
undefined,
|
||||
'',
|
||||
'test',
|
||||
123,
|
||||
0,
|
||||
-1,
|
||||
Infinity,
|
||||
NaN,
|
||||
{},
|
||||
];
|
||||
notDates.forEach((n) => {
|
||||
expect(ComposeUtils.isValidDateAndOlderThan(n, 0)).to.equal(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user