File tree Expand file tree Collapse file tree 3 files changed +42
-5
lines changed
Expand file tree Collapse file tree 3 files changed +42
-5
lines changed Original file line number Diff line number Diff line change 11import { 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+ } )
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { HttpCode } from "../../../src/common/http"
66import { proxy } from "../../../src/node/proxy"
77import * as httpserver from "../../utils/httpserver"
88import * as integration from "../../utils/integration"
9+ import { getAvailablePort } from "../../utils/helpers"
910
1011describe ( "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.
168169describe ( "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 , {
Original file line number Diff line number Diff line change 11import { promises as fs } from "fs"
22import * as os from "os"
33import * 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+ } )
You can’t perform that action at this time.
0 commit comments