-
Notifications
You must be signed in to change notification settings - Fork 36
/
worker.test.ts
45 lines (40 loc) · 1.3 KB
/
worker.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import {expect} from '@esm-bundle/chai';
import {sleep} from '../sleep';
import {closeAllReps, dbsToDrop, deleteAllDatabases} from '../test-util';
teardown(async () => {
await closeAllReps();
await deleteAllDatabases();
});
// This started failing on github only with https://github.com/rocicorp/replicache/pull/479.
// It works fine locally. Error message is:
// src/worker.test.ts:
//
// ❌ worker test (failed on Chromium)
// Error: Timed out
// at src/worker.test.ts:42:42
// at async o.<anonymous> (src/worker.test.ts:17:17)
//
// Example failure: https://github.com/rocicorp/replicache/runs/3519251318
test.skip('worker test', async () => {
const url = new URL('./worker-test.ts', import.meta.url);
const w = new Worker(url, {type: 'module'});
const name = 'worker-test';
dbsToDrop.add(name);
const data = await send(w, {name});
expect(data).to.be.undefined;
});
async function send(w: Worker, data: {name: string}): Promise<unknown> {
const p = new Promise((resolve, reject) => {
w.onmessage = e => resolve(e.data);
w.onerror = reject;
w.onmessageerror = reject;
});
w.postMessage(data);
return withTimeout(p);
}
async function withTimeout<T>(p: Promise<T>): Promise<T> {
return Promise.race([
p,
sleep(3000).then(() => Promise.reject(new Error('Timed out'))),
]);
}