You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/test/session/unit/utils/job_runner/FakeSleepForJob.ts

99 lines
2.9 KiB
TypeScript

import { isNumber } from 'lodash';
import { v4 } from 'uuid';
import { sleepFor } from '../../../../../session/utils/Promise';
import {
Persistedjob,
SerializedPersistedJob,
} from '../../../../../session/utils/job_runners/PersistedJob';
export class FakeSleepForMultiJob extends Persistedjob {
private readonly sleepDuration: number;
private readonly returnResult: boolean;
constructor({
identifier,
nextAttemptTimestamp,
maxAttempts,
currentRetry,
returnResult,
sleepDuration,
}: {
identifier: string | null;
nextAttemptTimestamp: number | null;
maxAttempts: number | null;
currentRetry: number;
sleepDuration: number;
returnResult: boolean;
}) {
super({
jobType: 'FakeSleepForJobMultiType',
identifier: identifier || v4(),
delayBetweenRetries: 10000,
maxAttempts: isNumber(maxAttempts) ? maxAttempts : 3,
nextAttemptTimestamp: nextAttemptTimestamp || Date.now() + 3000,
singleJobInQueue: false,
currentRetry,
});
this.returnResult = returnResult;
this.sleepDuration = sleepDuration;
if (process.env.NODE_APP_INSTANCE !== undefined) {
throw new Error('FakeSleepForJobMultiType are only meant for testing purposes');
}
}
public async run() {
console.warn(
`running job ${this.jobType} with id:"${this.identifier}". sleeping for ${this.sleepDuration} & returning ${this.returnResult} `
);
await sleepFor(this.sleepDuration);
console.warn(`${this.jobType} with id:"${this.identifier}" done. returning success `);
return this.returnResult;
}
public serializeJob(): SerializedPersistedJob {
const fromParent = super.serializeBase();
fromParent.sleepDuration = this.sleepDuration;
fromParent.returnResult = this.returnResult;
return fromParent;
}
}
export class FakeSleepForJob extends Persistedjob {
constructor({
identifier,
nextAttemptTimestamp,
maxAttempts,
currentRetry,
}: {
identifier: string | null;
nextAttemptTimestamp: number | null;
maxAttempts: number | null;
currentRetry: number;
}) {
super({
jobType: 'FakeSleepForJobType',
identifier: identifier || v4(),
delayBetweenRetries: 10000,
maxAttempts: isNumber(maxAttempts) ? maxAttempts : 3,
nextAttemptTimestamp: nextAttemptTimestamp || Date.now() + 3000,
singleJobInQueue: true,
currentRetry,
});
if (process.env.NODE_APP_INSTANCE !== undefined) {
throw new Error('FakeSleepForJob are only meant for testing purposes');
}
}
public async run() {
console.warn(`running job ${this.jobType} with id:"${this.identifier}" `);
await sleepFor(5000);
console.warn(`${this.jobType} with id:"${this.identifier}" done. returning failed `);
return false;
}
public serializeJob(): SerializedPersistedJob {
const fromParent = super.serializeBase();
return fromParent;
}
}