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
|
import { MainLoop } from "@/consumer/Loop";
import { WidgetRequestProcessor } from "@/consumer/processor";
import { WidgetRequestRetriever } from "@/consumer/retrievers";
import { expect, mock, test } from "bun:test";
import { VoidLogger } from "@/shared/t";
import { WidgetRequest } from "@/shared/schema";
import { randomUUID } from "node:crypto";
test("main loop exits after max poll attempts", async () => {
const logger = new VoidLogger();
const maxPollAttempts = 3;
const timesToGiveData = 5;
let timesGivenData = 0;
const mockRetriever: WidgetRequestRetriever = {
refreshRequests: mock(async () => {
if (timesGivenData < timesToGiveData) {
timesGivenData++;
return [
WidgetRequest.parse({
type: "create",
owner: "simponic",
widgetId: randomUUID(),
requestId: randomUUID(),
}),
];
}
return [];
}),
};
const mockProcessor: WidgetRequestProcessor = {
processRequest: mock(async (_r) => {}),
};
await MainLoop(
[mockRetriever],
[mockProcessor],
maxPollAttempts,
200,
logger,
);
expect(mockRetriever.refreshRequests).toHaveBeenCalledTimes(
timesGivenData + maxPollAttempts,
);
expect(mockProcessor.processRequest).toHaveBeenCalledTimes(timesGivenData);
});
|