Skip to content

Commit daf4c32

Browse files
Use host-port wait strategy of the Reaper (#223)
1 parent 51bb727 commit daf4c32

14 files changed

+125
-87
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@
7171
"rimraf": "^3.0.2",
7272
"ts-jest": "^27.0.1",
7373
"ts-node": "^10.0.0",
74-
"typescript": "^4.3.2"
74+
"typescript": "^4.3.2",
75+
"wait-for-expect": "^3.0.2"
7576
},
7677
"husky": {
7778
"hooks": {

src/generic-container.test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import fetch from "node-fetch";
22
import path from "path";
3-
import { createServer, Server } from "http";
43
import { GenericContainer } from "./generic-container";
54
import { AlwaysPullPolicy } from "./pull-policy";
65
import { Wait } from "./wait";
76
import { RandomUuid } from "./uuid";
8-
import { TestContainers } from "./test-containers";
9-
import { RandomPortClient } from "./port-client";
107
import { getContainerById, getEvents, getRunningContainerNames } from "./test-helper";
118
import { Network } from "./network";
129

src/modules/kafka/kafka-container.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import { DockerClient } from "../../docker-client";
44
import { Network, StartedNetwork } from "../../network";
55
import { Host } from "../../docker-client-instance";
66
import { Port } from "../../port";
7-
import { PortClient, RandomPortClient } from "../../port-client";
87
import { RandomUuid, Uuid } from "../../uuid";
98
import { StartedTestContainer, StoppedTestContainer } from "../..";
109
import { StopOptions } from "../../test-container";
1110
import { log } from "../../logger";
1211
import { AbstractStartedContainer } from "../abstract-started-container";
12+
import { PortGenerator, RandomUniquePortGenerator } from "../../port-generator";
1313

1414
export const KAFKA_IMAGE = "confluentinc/cp-kafka:5.5.4";
1515
export const ZK_IMAGE = "confluentinc/cp-zookeeper:5.5.4";
1616

1717
export class KafkaContainer extends GenericContainer {
1818
private readonly uuid: Uuid = new RandomUuid();
19-
private readonly portClient: PortClient = new RandomPortClient();
19+
private readonly portGenerator: PortGenerator = new RandomUniquePortGenerator();
2020

2121
private isZooKeeperProvided = false;
2222
private zooKeeperHost?: Host;
@@ -61,7 +61,7 @@ export class KafkaContainer extends GenericContainer {
6161
this.withEnv("KAFKA_ZOOKEEPER_CONNECT", `${this.zooKeeperHost}:${this.zooKeeperPort}`);
6262
} else {
6363
const zooKeeperHost = this.uuid.nextUuid();
64-
const zooKeeperPort = await this.portClient.getPort();
64+
const zooKeeperPort = await this.portGenerator.generatePort();
6565

6666
this.withEnv("KAFKA_ZOOKEEPER_CONNECT", `${zooKeeperHost}:${zooKeeperPort}`);
6767

src/port-binder.test.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
import { PortBinder } from "./port-binder";
2-
import { FixedPortClient } from "./port-client";
2+
import { FixedPortGenerator } from "./port-generator";
33

44
describe("PortBinder", () => {
55
it("should bind each port to the host", async () => {
6-
const portClient = new FixedPortClient([1000, 2000]);
7-
const portBinder = new PortBinder(portClient);
8-
9-
const boundPorts = await portBinder.bind([1, 2]);
10-
11-
expect(boundPorts.getBinding(1)).toBe(1000);
12-
expect(boundPorts.getBinding(2)).toBe(2000);
13-
});
14-
15-
it("should not produce the same port more than once", async () => {
16-
const portClient = new FixedPortClient([1000, 1000, 2000]);
17-
const portBinder = new PortBinder(portClient);
6+
const portGenerator = new FixedPortGenerator([1000, 2000]);
7+
const portBinder = new PortBinder(portGenerator);
188

199
const boundPorts = await portBinder.bind([1, 2]);
2010

src/port-binder.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
import { BoundPorts } from "./bound-ports";
22
import { Port } from "./port";
3-
import { PortClient, RandomPortClient } from "./port-client";
3+
import { PortGenerator, RandomUniquePortGenerator } from "./port-generator";
44

55
export class PortBinder {
6-
private readonly ports: Set<number> = new Set();
7-
8-
constructor(private readonly portClient: PortClient = new RandomPortClient()) {}
6+
constructor(private readonly portGenerator: PortGenerator = new RandomUniquePortGenerator()) {}
97

108
public async bind(ports: Port[]): Promise<BoundPorts> {
119
const boundPorts = new BoundPorts();
1210

1311
for (const port of ports) {
14-
let allocatedPort: Port;
15-
do {
16-
allocatedPort = await this.portClient.getPort();
17-
} while (this.ports.has(allocatedPort));
18-
19-
this.ports.add(allocatedPort);
20-
boundPorts.setBinding(port, allocatedPort);
12+
boundPorts.setBinding(port, await this.portGenerator.generatePort());
2113
}
2214

2315
return boundPorts;

src/port-client.test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/port-client.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/port-forwarder.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createServer, Server } from "http";
22
import { GenericContainer } from "./generic-container";
33
import { TestContainers } from "./test-containers";
4-
import { RandomPortClient } from "./port-client";
4+
import { RandomUniquePortGenerator } from "./port-generator";
55
import { Network } from "./network";
66

77
describe("PortForwarder", () => {
@@ -11,13 +11,14 @@ describe("PortForwarder", () => {
1111
let server: Server;
1212

1313
beforeEach(async () => {
14-
randomPort = await new RandomPortClient().getPort();
15-
server = await new Promise((resolve) => {
16-
const server = createServer((req, res) => {
14+
randomPort = await new RandomUniquePortGenerator().generatePort();
15+
16+
await new Promise<void>((resolve) => {
17+
server = createServer((req, res) => {
1718
res.writeHead(200);
1819
res.end("hello world");
1920
});
20-
server.listen(randomPort, () => resolve(server));
21+
server.listen(randomPort, resolve);
2122
});
2223
});
2324

src/port-generator.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { FixedPortGenerator, RandomUniquePortGenerator } from "./port-generator";
2+
3+
describe("PortGenerator", () => {
4+
describe("RandomUniquePortGenerator", () => {
5+
it("should generate a random and unique port across all instances", async () => {
6+
const fixedPortGenerator = new FixedPortGenerator([1000, 1000, 1001]);
7+
expect(await new RandomUniquePortGenerator(fixedPortGenerator).generatePort()).toBe(1000);
8+
expect(await new RandomUniquePortGenerator(fixedPortGenerator).generatePort()).toBe(1001);
9+
});
10+
});
11+
});

0 commit comments

Comments
 (0)