Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/testcontainers/src/utils/port-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ describe("PortGenerator", () => {
await expect(fixedPortGenerator.generatePort()).resolves.toBe(1000);
await expect(fixedPortGenerator.generatePort()).resolves.toBe(1001);
});

it("should reject when no more ports are available", async () => {
const fixedPortGenerator = new FixedPortGenerator([1000]);

await expect(fixedPortGenerator.generatePort()).resolves.toBe(1000);
await expect(fixedPortGenerator.generatePort()).rejects.toThrowError(
"FixedPortGenerator has no more ports available"
);
});
});

describe("RandomPortGenerator", () => {
Expand Down
7 changes: 5 additions & 2 deletions packages/testcontainers/src/utils/port-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ export class FixedPortGenerator implements PortGenerator {

constructor(private readonly ports: number[]) {}

public generatePort(): Promise<number> {
return Promise.resolve(this.ports[this.portIndex++]);
public async generatePort(): Promise<number> {
if (this.portIndex >= this.ports.length) {
throw new Error("FixedPortGenerator has no more ports available");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve the promise contract on exhaustion

generatePort() is typed through PortGenerator as returning Promise<number>, but this branch throws before returning a Promise. In contexts that consume the promised API directly, such as generator.generatePort().catch(...) or collecting calls for Promise.all, exhaustion will escape synchronously instead of being handled as a rejection; RandomPortGenerator and the interface both expose asynchronous failure semantics, so this should reject the returned promise (for example by making the method async or returning Promise.reject(...)).

Useful? React with 👍 / 👎.

}
return this.ports[this.portIndex++];
}
}
Loading