From 2c2b86163f84449bca4dfb4330a7f849a54a0b05 Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Mon, 24 Sep 2018 16:13:05 -0400 Subject: [PATCH] test: use dynamic port for web proxy tests This changes the web proxy tests to use a dynamic, unused port instead of the default 8080 port, which could cause the tests to fail if 8080 is being used. --- test/integration/WebProxy.spec.ts | 6 ++++-- test/p2p/sanity.spec.ts | 16 +--------------- test/utils.ts | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 17 deletions(-) create mode 100644 test/utils.ts diff --git a/test/integration/WebProxy.spec.ts b/test/integration/WebProxy.spec.ts index 2a39490d3..5e0b2b2db 100644 --- a/test/integration/WebProxy.spec.ts +++ b/test/integration/WebProxy.spec.ts @@ -1,18 +1,20 @@ import chai, { expect } from 'chai'; import chaiHttp from 'chai-http'; import Xud from '../../lib/Xud'; +import { getUnusedPort } from '../utils'; -describe('WebProxy', () => { +describe('WebProxy', async () => { let xud: Xud; let config: any; chai.use(chaiHttp); + const port = await getUnusedPort(); before(async () => { config = { dbpath: ':memory:', webproxy: { + port, disable: false, - port: 8080, }, logpath: '', loglevel: 'warn', diff --git a/test/p2p/sanity.spec.ts b/test/p2p/sanity.spec.ts index bc6b4671d..18142d430 100644 --- a/test/p2p/sanity.spec.ts +++ b/test/p2p/sanity.spec.ts @@ -2,24 +2,10 @@ import chai, { expect } from 'chai'; import Xud from '../../lib/Xud'; import chaiAsPromised from 'chai-as-promised'; import { getUri } from '../../lib/utils/utils'; -import net from 'net'; +import { getUnusedPort } from '../utils'; chai.use(chaiAsPromised); -const getUnusedPort = async () => { - return new Promise((resolve, reject) => { - const server = net.createServer(); - server.unref(); - server.on('error', reject); - server.listen(0, () => { - const { port } = server.address(); - server.close(() => { - resolve(port); - }); - }); - }); -}; - const createConfig = (instanceid: number, p2pPort: number) => ({ instanceid, initdb: false, diff --git a/test/utils.ts b/test/utils.ts new file mode 100644 index 000000000..0b0dcfec7 --- /dev/null +++ b/test/utils.ts @@ -0,0 +1,18 @@ +import net from 'net'; + +/** + * Discovers and returns a dynamically assigned, unused port available for testing. + */ +export const getUnusedPort = async () => { + return new Promise((resolve, reject) => { + const server = net.createServer(); + server.unref(); + server.on('error', reject); + server.listen(0, () => { + const { port } = server.address(); + server.close(() => { + resolve(port); + }); + }); + }); +};