Skip to content

Commit 682c5ab

Browse files
committed
fix more tests
1 parent 14e4ec1 commit 682c5ab

1 file changed

Lines changed: 22 additions & 15 deletions

File tree

packages/ws-worker/test/api/workloop.test.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { sleep } from '../util';
55
import { mockChannel } from '../../src/mock/sockets';
66
import startWorkloop from '../../src/api/workloop';
77
import { CLAIM } from '../../src/events';
8+
import EventEmitter from 'node:events';
89

910
let workloop: any;
1011

@@ -14,20 +15,31 @@ test.afterEach(() => {
1415
workloop?.stop(); // cancel any workloops
1516
});
1617

18+
const createMockApp = (props: any) => ({
19+
workflows: {},
20+
queueChannel: mockChannel({
21+
[CLAIM]: () => {
22+
return { runs: [] };
23+
},
24+
}),
25+
execute: () => {},
26+
27+
events: new EventEmitter(),
28+
...props,
29+
});
30+
1731
test('workloop can be cancelled', async (t) => {
1832
let count = 0;
1933

20-
const app = {
21-
workflows: {},
34+
const app = createMockApp({
2235
queueChannel: mockChannel({
2336
[CLAIM]: () => {
2437
count++;
2538
workloop.stop();
2639
return { runs: [] };
2740
},
2841
}),
29-
execute: () => {},
30-
};
42+
});
3143

3244
workloop = startWorkloop(app as any, logger, 1, 1);
3345
t.false(workloop.isStopped());
@@ -40,26 +52,23 @@ test('workloop can be cancelled', async (t) => {
4052

4153
test('workloop sends the runs:claim event', (t) => {
4254
return new Promise((done) => {
43-
const app = {
44-
workflows: {},
55+
const app = createMockApp({
4556
queueChannel: mockChannel({
4657
[CLAIM]: () => {
4758
t.pass();
4859
done();
4960
return { runs: [] };
5061
},
5162
}),
52-
execute: () => {},
53-
};
63+
});
5464
workloop = startWorkloop(app as any, logger, 1, 1);
5565
});
5666
});
5767

5868
test('workloop sends the runs:claim event several times ', (t) => {
5969
return new Promise((done) => {
6070
let count = 0;
61-
const app = {
62-
workflows: {},
71+
const app = createMockApp({
6372
queueChannel: mockChannel({
6473
[CLAIM]: () => {
6574
count++;
@@ -70,16 +79,14 @@ test('workloop sends the runs:claim event several times ', (t) => {
7079
return { runs: [] };
7180
},
7281
}),
73-
execute: () => {},
74-
};
82+
});
7583
workloop = startWorkloop(app as any, logger, 1, 1);
7684
});
7785
});
7886

7987
test('workloop calls execute if runs:claim returns runs', (t) => {
8088
return new Promise((done) => {
81-
const app = {
82-
workflows: {},
89+
const app = createMockApp({
8390
queueChannel: mockChannel({
8491
[CLAIM]: () => ({
8592
runs: [{ id: 'a', token: 'x.y.z' }],
@@ -90,7 +97,7 @@ test('workloop calls execute if runs:claim returns runs', (t) => {
9097
t.pass();
9198
done();
9299
},
93-
};
100+
});
94101

95102
workloop = startWorkloop(app as any, logger, 1, 1);
96103
});

0 commit comments

Comments
 (0)