Skip to content

Commit

Permalink
Merge pull request #523 from ExchangeUnion/proxy-test-port
Browse files Browse the repository at this point in the history
test: use dynamic port for web proxy tests
  • Loading branch information
sangaman authored Sep 27, 2018
2 parents 1579943 + 2c2b861 commit 8d0309e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
6 changes: 4 additions & 2 deletions test/integration/WebProxy.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
16 changes: 1 addition & 15 deletions test/p2p/sanity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>((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,
Expand Down
18 changes: 18 additions & 0 deletions test/utils.ts
Original file line number Diff line number Diff line change
@@ -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<number>((resolve, reject) => {
const server = net.createServer();
server.unref();
server.on('error', reject);
server.listen(0, () => {
const { port } = server.address();
server.close(() => {
resolve(port);
});
});
});
};

0 comments on commit 8d0309e

Please sign in to comment.