tasks: rework the startTask API

it is now async. change was required to reset the pending flag
This commit is contained in:
Girish Ramakrishnan
2025-06-17 18:54:12 +02:00
parent 4770b32287
commit d9c104613c
13 changed files with 178 additions and 202 deletions

View File

@@ -85,58 +85,42 @@ describe('task', function () {
it('can run valid task - success', async function () {
const taskId = await tasks.add(tasks._TASK_IDENTITY, [ 'ping' ]);
return new Promise((resolve, reject) => {
tasks.startTask(taskId, {}, function (error, result) {
if (error) return reject(error);
expect(result).to.equal('ping');
resolve();
});
});
const [error, result] = await safe(tasks.startTask(taskId, {}));
if (error) throw error;
expect(result).to.equal('ping');
});
it('can run valid task - error', async function () {
const taskId = await tasks.add(tasks._TASK_ERROR, [ 'ping' ]);
return new Promise((resolve, reject) => {
tasks.startTask(taskId, {}, function (error, result) {
if (!error) return reject(new Error('expecting task to fail'));
expect(error.message).to.be('Failed for arg: ping');
expect(result).to.be(null);
resolve();
});
});
const [error, result] = await safe(tasks.startTask(taskId, {}));
if (!error) throw new Error('expecting task to fail');
expect(error.message).to.be('Failed for arg: ping');
expect(result).to.not.be.ok();
});
it('can get logs of crash', async function () {
const taskId = await tasks.add(tasks._TASK_CRASH, [ 'ping' ]);
return new Promise((resolve, reject) => {
tasks.startTask(taskId, {}, function (error, result) {
if (!error) return reject(new Error('expecting task to crash'));
expect(error.message).to.contain(`Task ${taskId} crashed`);
expect(result).to.be(null);
const [error, result] = await safe(tasks.startTask(taskId, {}));
if (!error) throw new Error('expecting task to crash');
expect(error.message).to.contain(`Task ${taskId} crashed`);
expect(result).to.not.be.ok();
const logs = fs.readFileSync(`${paths.TASKS_LOG_DIR}/${taskId}.log`, 'utf8');
expect(logs).to.contain('Crashing for arg: ping');
resolve();
});
});
const logs = fs.readFileSync(`${paths.TASKS_LOG_DIR}/${taskId}.log`, 'utf8');
expect(logs).to.contain('Crashing for arg: ping');
});
it('can stop task', async function () {
const taskId = await tasks.add(tasks._TASK_SLEEP, [ 10000 ]);
return new Promise((resolve, reject) => {
tasks.startTask(taskId, {}, function (error, result) {
if (!error) return reject(new Error('expecting task to stop'));
expect(error.message).to.contain('stopped');
expect(result).to.be(null);
resolve();
});
setTimeout(async function () {
await tasks.stopTask(taskId);
}, 2000);
setTimeout(async function () {
await tasks.stopTask(taskId);
}, 2000);
});
const [error, result] = await safe(tasks.startTask(taskId, {}));
if (!error) throw new Error('expecting task to stop');
expect(error.message).to.contain('stopped');
expect(result).to.not.be.ok();
});
});