File tree 3 files changed +42
-5
lines changed
3 files changed +42
-5
lines changed Original file line number Diff line number Diff line change 1
1
import { promises as fs } from "fs"
2
- import { tmpdir , useEnv } from "../../test/utils/helpers"
2
+ import { getAvailablePort , tmpdir , useEnv } from "../../test/utils/helpers"
3
3
4
4
/**
5
5
* This file is for testing test helpers (not core code).
@@ -39,3 +39,16 @@ describe("useEnv", () => {
39
39
expect ( process . env [ envKey ] ) . toEqual ( "test environment variable" )
40
40
} )
41
41
} )
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"
6
6
import { proxy } from "../../src/node/proxy"
7
7
import * as httpserver from "../utils/httpserver"
8
8
import * as integration from "../utils/integration"
9
+ import { getAvailablePort } from "../utils/helpers"
9
10
10
11
describe ( "proxy" , ( ) => {
11
12
const nhooyrDevServer = new httpserver . HttpServer ( )
@@ -166,14 +167,16 @@ describe("proxy", () => {
166
167
// src/node/proxy.ts, you should probably add it to
167
168
// this test suite.
168
169
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 = ""
173
172
let testServer : http . Server
174
173
let proxyTarget : http . Server
175
174
176
175
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 } `
177
180
// Define server and a proxy server
178
181
testServer = http . createServer ( ( req , res ) => {
179
182
proxy . web ( req , res , {
Original file line number Diff line number Diff line change 1
1
import { promises as fs } from "fs"
2
2
import * as os from "os"
3
3
import * as path from "path"
4
+ import * as net from "net"
4
5
5
6
/**
6
7
* Return a mock of @coder/logger.
@@ -61,3 +62,23 @@ export function useEnv(key: string): [(nextValue: string | undefined) => string
61
62
62
63
return [ setValue , resetValue ]
63
64
}
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