forked from rocicorp/mono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.test.ts
More file actions
68 lines (55 loc) · 1.59 KB
/
runner.test.ts
File metadata and controls
68 lines (55 loc) · 1.59 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import {resolver} from '@rocicorp/resolver';
import {createSilentLogContext} from 'shared/src/logging-test-utils.js';
import {sleep} from 'shared/src/sleep.js';
import {describe, expect, test} from 'vitest';
import {ServiceRunner} from './runner.js';
import {Service} from './service.js';
describe('services/runner', () => {
class TestService implements Service {
readonly id: string;
resolver = resolver<void>();
valid = true;
constructor(id: string) {
this.id = id;
}
run(): Promise<void> {
return this.resolver.promise;
}
// eslint-disable-next-line require-await
async stop(): Promise<void> {
this.resolver.resolve();
}
}
const runner = new ServiceRunner<TestService>(
createSilentLogContext(),
(id: string) => new TestService(id),
(s: TestService) => s.valid,
);
test('caching', () => {
const s1 = runner.getService('foo');
const s2 = runner.getService('bar');
const s3 = runner.getService('foo');
expect(s1).toBe(s3);
expect(s1).not.toBe(s2);
});
test('stopped', async () => {
const s1 = runner.getService('foo');
s1.resolver.resolve();
await sleep(1);
const s2 = runner.getService('foo');
expect(s1).not.toBe(s2);
});
test('fails', async () => {
const s1 = runner.getService('foo');
s1.resolver.reject('foo');
await sleep(1);
const s2 = runner.getService('foo');
expect(s1).not.toBe(s2);
});
test('validity', () => {
const s1 = runner.getService('foo');
s1.valid = false;
const s2 = runner.getService('foo');
expect(s1).not.toBe(s2);
});
});