Skip to content

Commit 07f6647

Browse files
committed
feat: add getAvailablePort helper function
1 parent 49bb2fd commit 07f6647

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

test/unit/helpers.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { promises as fs } from "fs"
2-
import { tmpdir, useEnv } from "../../test/utils/helpers"
2+
import { getAvailablePort, tmpdir, useEnv } from "../../test/utils/helpers"
33

44
/**
55
* This file is for testing test helpers (not core code).
@@ -39,3 +39,16 @@ describe("useEnv", () => {
3939
expect(process.env[envKey]).toEqual("test environment variable")
4040
})
4141
})
42+
43+
describe("getAvailablePort", () => {
44+
it("should return a valid port", async () => {
45+
const port = await getAvailablePort()
46+
expect(port).toBeGreaterThan(0)
47+
expect(port).toBeLessThanOrEqual(65535)
48+
})
49+
it("should return different ports for different calls", async () => {
50+
const portOne = await getAvailablePort()
51+
const portTwo = await getAvailablePort()
52+
expect(portOne).not.toEqual(portTwo)
53+
})
54+
})

test/unit/proxy.test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { HttpCode } from "../../src/common/http"
66
import { proxy } from "../../src/node/proxy"
77
import * as httpserver from "../utils/httpserver"
88
import * as integration from "../utils/integration"
9+
import { getAvailablePort } from "../utils/helpers"
910

1011
describe("proxy", () => {
1112
const nhooyrDevServer = new httpserver.HttpServer()
@@ -166,14 +167,16 @@ describe("proxy", () => {
166167
// src/node/proxy.ts, you should probably add it to
167168
// this test suite.
168169
describe("proxy (standalone)", () => {
169-
const PORT = 9003
170-
const PROXY_PORT = 8003
171-
const URL = `http://localhost:${PORT}`
172-
const PROXY_URL = `http://localhost:${PROXY_PORT}`
170+
let URL = ""
171+
let PROXY_URL = ""
173172
let testServer: http.Server
174173
let proxyTarget: http.Server
175174

176175
beforeEach(async () => {
176+
const PORT = await getAvailablePort()
177+
const PROXY_PORT = await getAvailablePort()
178+
URL = `http://localhost:${PORT}`
179+
PROXY_URL = `http://localhost:${PROXY_PORT}`
177180
// Define server and a proxy server
178181
testServer = http.createServer((req, res) => {
179182
proxy.web(req, res, {

test/utils/helpers.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { promises as fs } from "fs"
22
import * as os from "os"
33
import * as path from "path"
4+
import * as net from "net"
45

56
/**
67
* Return a mock of @coder/logger.
@@ -61,3 +62,23 @@ export function useEnv(key: string): [(nextValue: string | undefined) => string
6162

6263
return [setValue, resetValue]
6364
}
65+
66+
/**
67+
* Helper function to get a random port.
68+
*
69+
* Source: https://github.com/sindresorhus/get-port/blob/main/index.js#L23-L33
70+
*/
71+
export const getAvailablePort = (options?: net.ListenOptions): Promise<number> =>
72+
new Promise((resolve, reject) => {
73+
const server = net.createServer()
74+
server.unref()
75+
server.on("error", reject)
76+
server.listen(options, () => {
77+
// NOTE@jsjoeio: not a huge fan of the type assertion
78+
// but it works for now.
79+
const { port } = server.address() as net.AddressInfo
80+
server.close(() => {
81+
resolve(port)
82+
})
83+
})
84+
})

0 commit comments

Comments
 (0)