Skip to content

Commit

Permalink
test: listen on 0 port and remove get-port
Browse files Browse the repository at this point in the history
  • Loading branch information
nwtgck committed Apr 28, 2023
1 parent 7fd8e71 commit e863c37
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
19 changes: 0 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"@types/request": "^2.48.8",
"@types/yargs": "^17.0.23",
"espower-typescript": "^10.0.1",
"get-port": "^5.1.1",
"mocha": "^9.2.2",
"nodemon": "^2.0.22",
"power-assert": "^1.6.1",
Expand Down
32 changes: 14 additions & 18 deletions test/piping.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import getPort from "get-port";
import * as net from "net";
import * as http from "http";
import * as http2 from "http2";
Expand All @@ -13,13 +12,14 @@ import {EventEmitter} from "events";
import {URL, UrlObject} from "url";

/**
* Listen on the specify port
* Listen and return port
* @param server
* @param port
*/
function listenPromise(server: http.Server | http2.Http2Server, port: number): Promise<void> {
return new Promise<void>((resolve) => {
server.listen(port, resolve);
function listenPromise(server: http.Server | http2.Http2Server): Promise<number> {
return new Promise<number>((resolve) => {
server.listen(0, () => {
resolve((server.address() as net.AddressInfo).port);
});
});
}

Expand Down Expand Up @@ -59,14 +59,12 @@ describe("piping.Server", () => {
let pipingUrl: string;

beforeEach(async () => {
// Get available port
pipingPort = await getPort();
// Define Piping URL
pipingUrl = `http://127.0.0.1:${pipingPort}`;
// Create a Piping server
pipingServer = http.createServer(new piping.Server({logger}).generateHandler(false));
// Listen on the port
await listenPromise(pipingServer, pipingPort);
// Listen
pipingPort = await listenPromise(pipingServer);
// Define Piping URL
pipingUrl = `http://127.0.0.1:${pipingPort}`;
});

afterEach(async () => {
Expand Down Expand Up @@ -334,16 +332,14 @@ Host: 127.0.0.1:${pipingPort}
});

it("should handle connection over HTTP/2 (receiver O, sender: O)", async () => {
// Get available port
const http2PipingPort = await getPort();
// Define Piping URL
const http2PipingUrl = `http://127.0.0.1:${http2PipingPort}`;

// Create a Piping server on HTTP/2
const http2PipingServer = http2.createServer(new piping.Server({logger}).generateHandler(false));
const sessions: http2.Http2Session[] = [];
http2PipingServer.on("session", (session) => sessions.push(session));
await listenPromise(http2PipingServer, http2PipingPort);
// Listen
const http2PipingPort = await listenPromise(http2PipingServer);
// Define Piping URL
const http2PipingUrl = `http://127.0.0.1:${http2PipingPort}`;

// Get request
const getReq = http2.connect(`${http2PipingUrl}`)
Expand Down

0 comments on commit e863c37

Please sign in to comment.