Skip to content

Commit 0594e97

Browse files
committed
šŸ› fix(watchers): await emitContainerReport to ensure handler ordering (#282)
Without await, the per-container event handler (digest buffering) ran as a detached promise. The batch event could fire before buffering completed, causing the batch→digest eviction (982b4d7) to miss entries that were still being buffered. Awaiting ensures digest handlers finish before the batch event fires.
1 parent 982b4d7 commit 0594e97

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

ā€Žapp/watchers/providers/docker/Docker.containers.processing-retrieval.test.tsā€Ž

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,38 @@ describe('Docker Watcher', () => {
4343
expect(hEvent.emitContainerReport).toHaveBeenCalled();
4444
});
4545

46+
test('should wait for container report handlers before resolving watchContainer', async () => {
47+
const container = { id: 'test123', name: 'test' };
48+
const mockLog = createMockLogWithChild(['debug']);
49+
docker.log = mockLog;
50+
docker.findNewVersion = vi.fn().mockResolvedValue({ tag: '2.0.0' });
51+
docker.mapContainerToContainerReport = vi.fn().mockReturnValue({ container, changed: false });
52+
53+
let releaseEmitContainerReport: (() => void) | undefined;
54+
hEvent.emitContainerReport.mockImplementation(
55+
() =>
56+
new Promise<void>((resolve) => {
57+
releaseEmitContainerReport = resolve;
58+
}),
59+
);
60+
61+
let resolved = false;
62+
const watchPromise = docker.watchContainer(container).then(() => {
63+
resolved = true;
64+
});
65+
66+
await vi.waitFor(() => {
67+
expect(hEvent.emitContainerReport).toHaveBeenCalled();
68+
});
69+
70+
expect(resolved).toBe(false);
71+
72+
releaseEmitContainerReport?.();
73+
await watchPromise;
74+
75+
expect(resolved).toBe(true);
76+
});
77+
4678
test('should handle container processing error', async () => {
4779
const container = { id: 'test123', name: 'test' };
4880
const mockLog = createMockLogWithChild(['warn', 'debug']);

ā€Žapp/watchers/providers/docker/container-processing.tsā€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function watchContainer(
7171
}
7272

7373
const containerReport = mapContainerToContainerReport(containerWithResult, watchStartedAtMs);
74-
event.emitContainerReport(containerReport);
74+
await event.emitContainerReport(containerReport);
7575
return containerReport;
7676
}
7777

0 commit comments

Comments
Ā (0)