Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfitz committed Sep 17, 2024
1 parent 7de3438 commit 47189c5
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions test/server/lib/GristJobs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { delay } from 'app/common/delay';
import { GristJobs } from 'app/server/lib/GristJobs';
import { assert } from 'chai';

describe('GristJobs', function() {
this.timeout(20000);

it('can run immediate jobs', async function() {
const jobs = new GristJobs();
const q = jobs.queue();
try {
let ct = 0;
let defaultCt = 0;
q.handleName('add', async (job) => {
ct += job.data.delta;
});
q.handleDefault(async (job) => {
defaultCt++;
});
q.add('add', {delta: 2});
await waitToPass(async () => {
assert.equal(ct, 2);
assert.equal(defaultCt, 0);
});
q.add('add', {delta: 3});
await waitToPass(async () => {
assert.equal(ct, 5);
assert.equal(defaultCt, 0);
});
q.add('badd', {delta: 4});
await waitToPass(async () => {
assert.equal(ct, 5);
assert.equal(defaultCt, 1);
});
} finally {
jobs.stop({obliterate: true});

Check failure on line 36 in test/server/lib/GristJobs.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 18.x, :lint:python:client:common:smoke:stubs:)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check failure on line 36 in test/server/lib/GristJobs.ts

View workflow job for this annotation

GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 18.x, 3.10)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
});

it('can run delayed jobs', async function() {
const jobs = new GristJobs();
const q = jobs.queue();
try {
let ct = 0;
let defaultCt = 0;
q.handleName('add', async (job) => {
ct += job.data.delta;
});
q.handleDefault(async () => {
defaultCt++;
});
q.add('add', {delta: 2}, {delay: 500});
assert.equal(ct, 0);
assert.equal(defaultCt, 0);
// We need to wait long enough to see the effect.
await delay(100);
assert.equal(ct, 0);
assert.equal(defaultCt, 0);
await delay(900);
assert.equal(ct, 2);
assert.equal(defaultCt, 0);
} finally {
jobs.stop({obliterate: true});

Check failure on line 63 in test/server/lib/GristJobs.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 18.x, :lint:python:client:common:smoke:stubs:)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check failure on line 63 in test/server/lib/GristJobs.ts

View workflow job for this annotation

GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 18.x, 3.10)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
});

it('can run repeated jobs', async function() {
const jobs = new GristJobs();
const q = jobs.queue();
try {
let ct = 0;
let defaultCt = 0;
q.handleName('add', async (job) => {
ct += job.data.delta;
});
q.handleDefault(async () => {
defaultCt++;
});
q.add('add', {delta: 2}, {repeat: {every: 250}});
q.add('badd', {delta: 2}, {repeat: {every: 100}});
assert.equal(ct, 0);
assert.equal(defaultCt, 0);
await delay(1000);
assert.isAtLeast(ct, 8 - 2);
assert.isAtMost(ct, 8 + 2);
assert.isAtLeast(defaultCt, 10 - 1);
assert.isAtMost(defaultCt, 10 + 1);
} finally {
jobs.stop({obliterate: true});

Check failure on line 89 in test/server/lib/GristJobs.ts

View workflow job for this annotation

GitHub Actions / build_and_test (3.11, 18.x, :lint:python:client:common:smoke:stubs:)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator

Check failure on line 89 in test/server/lib/GristJobs.ts

View workflow job for this annotation

GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 18.x, 3.10)

Promises must be awaited, end with a call to .catch, end with a call to .then with a rejection handler or be explicitly marked as ignored with the `void` operator
}
});
});

async function waitToPass(fn: () => Promise<void>,
maxWaitMs: number = 2000) {
const start = Date.now();
while (Date.now() - start < maxWaitMs) {
try {
await fn();
return true;
} catch (e) {
// continue after a small delay.
await delay(10);
}
}
await fn();
return true;
}

0 comments on commit 47189c5

Please sign in to comment.