-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build_and_test (3.11, 18.x, :lint:python:client:common:smoke:stubs:)
Check failure on line 36 in test/server/lib/GristJobs.ts GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 18.x, 3.10)
|
||
} | ||
}); | ||
|
||
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 GitHub Actions / build_and_test (3.11, 18.x, :lint:python:client:common:smoke:stubs:)
Check failure on line 63 in test/server/lib/GristJobs.ts GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 18.x, 3.10)
|
||
} | ||
}); | ||
|
||
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 GitHub Actions / build_and_test (3.11, 18.x, :lint:python:client:common:smoke:stubs:)
Check failure on line 89 in test/server/lib/GristJobs.ts GitHub Actions / build_and_test (:lint:python:client:common:smoke:, 18.x, 3.10)
|
||
} | ||
}); | ||
}); | ||
|
||
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; | ||
} |