Skip to content

Commit 43f1fed

Browse files
committed
Health check for UDP container test
1 parent 23f5cbd commit 43f1fed

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/testcontainers/src/generic-container/generic-container.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getContainerRuntimeClient } from "../container-runtime";
55
import { PullPolicy } from "../utils/pull-policy";
66
import {
77
checkContainerIsHealthy,
8+
checkContainerIsHealthyUdp,
89
getDockerEventStream,
910
getRunningContainerNames,
1011
waitForDockerEvent,
@@ -24,6 +25,7 @@ describe("GenericContainer", { timeout: 180_000 }, () => {
2425

2526
it("should return first mapped port with regardless of protocol", async () => {
2627
await using container = await new GenericContainer("mendhak/udp-listener").withExposedPorts("5005/udp").start();
28+
await checkContainerIsHealthyUdp(container);
2729
expect(container.getFirstMappedPort()).toBe(container.getMappedPort("5005/udp"));
2830
});
2931

@@ -49,6 +51,7 @@ describe("GenericContainer", { timeout: 180_000 }, () => {
4951
protocol: "udp",
5052
})
5153
.start();
54+
await checkContainerIsHealthyUdp(container);
5255
expect(container.getMappedPort("5005/udp")).toBe(hostPort);
5356
expect(container.getMappedPort(5005, "udp")).toBe(hostPort);
5457
});

packages/testcontainers/src/utils/test-helper.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { GetEventsOptions, ImageInspectInfo } from "dockerode";
22
import { createServer, Server } from "http";
3+
import { createSocket } from "node:dgram";
34
import fs from "node:fs";
45
import path from "node:path";
56
import { Readable } from "stream";
@@ -30,6 +31,27 @@ export const checkContainerIsHealthyTls = async (container: StartedTestContainer
3031
expect(response.statusCode).toBe(200);
3132
};
3233

34+
export const checkContainerIsHealthyUdp = async (container: StartedTestContainer): Promise<void> => {
35+
const host = container.getHost();
36+
const port = container.getMappedPort("5005/udp");
37+
const readyMessage = "Listening on UDP port 5005";
38+
const testMessage = "health_check";
39+
const client = createSocket("udp4");
40+
try {
41+
const logs = await container.logs();
42+
for await (const log of logs) {
43+
if (log.includes(readyMessage)) {
44+
client.send(Buffer.from(testMessage), port, host);
45+
}
46+
if (log.includes(testMessage)) {
47+
return;
48+
}
49+
}
50+
} finally {
51+
client.close();
52+
}
53+
};
54+
3355
export const checkEnvironmentContainerIsHealthy = async (
3456
startedEnvironment: StartedDockerComposeEnvironment,
3557
containerName: string

0 commit comments

Comments
 (0)