Skip to content

Commit dfd4a7b

Browse files
Simplify UDP integration tests
1 parent 43f1fed commit dfd4a7b

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
getRunningContainerNames,
1111
waitForDockerEvent,
1212
} from "../utils/test-helper";
13+
import { Wait } from "../wait-strategies/wait";
1314
import { GenericContainer } from "./generic-container";
1415

1516
describe("GenericContainer", { timeout: 180_000 }, () => {
@@ -24,9 +25,14 @@ describe("GenericContainer", { timeout: 180_000 }, () => {
2425
});
2526

2627
it("should return first mapped port with regardless of protocol", async () => {
27-
await using container = await new GenericContainer("mendhak/udp-listener").withExposedPorts("5005/udp").start();
28+
await using container = await new GenericContainer("mendhak/udp-listener")
29+
.withWaitStrategy(Wait.forLogMessage("Listening on UDP port 5005"))
30+
.withExposedPorts("5005/udp")
31+
.start();
32+
2833
await checkContainerIsHealthyUdp(container);
2934
expect(container.getFirstMappedPort()).toBe(container.getMappedPort("5005/udp"));
35+
expect(container.getFirstMappedPort()).toBe(container.getMappedPort(5005, "udp"));
3036
});
3137

3238
it("should bind to specified host port", async () => {
@@ -45,12 +51,14 @@ describe("GenericContainer", { timeout: 180_000 }, () => {
4551
it("should bind to specified host port with a different protocol", async () => {
4652
const hostPort = await getPort();
4753
await using container = await new GenericContainer("mendhak/udp-listener")
54+
.withWaitStrategy(Wait.forLogMessage("Listening on UDP port 5005"))
4855
.withExposedPorts({
4956
container: 5005,
5057
host: hostPort,
5158
protocol: "udp",
5259
})
5360
.start();
61+
5462
await checkContainerIsHealthyUdp(container);
5563
expect(container.getMappedPort("5005/udp")).toBe(hostPort);
5664
expect(container.getMappedPort(5005, "udp")).toBe(hostPort);

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,14 @@ export const checkContainerIsHealthyTls = async (container: StartedTestContainer
3232
};
3333

3434
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";
3835
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-
}
36+
await using client = createSocket("udp4");
37+
client.send(Buffer.from(testMessage), container.getFirstMappedPort(), container.getHost());
38+
const logs = await container.logs();
39+
for await (const log of logs) {
40+
if (log.includes(testMessage)) {
41+
return;
4942
}
50-
} finally {
51-
client.close();
5243
}
5344
};
5445

0 commit comments

Comments
 (0)