Skip to content

Commit

Permalink
Node: update tests to use portRange
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Apr 8, 2024
1 parent 1d71e4b commit 1a507ec
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 25 deletions.
22 changes: 20 additions & 2 deletions node/src/test/test-PipeTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,16 @@ test('router.createPipeTransport() with wrong arguments rejects with TypeError',
// @ts-ignore
await expect(ctx.router1!.createPipeTransport({})).rejects.toThrow(TypeError);

await expect(
ctx.router1!.createPipeTransport({
listenInfo: {
protocol: 'udp',
ip: '127.0.0.1',
portRange: { min: 4000, max: 3000 },
},
})
).rejects.toThrow(TypeError);

await expect(
ctx.router1!.createPipeTransport({ listenIp: '123' })
).rejects.toThrow(TypeError);
Expand All @@ -479,7 +489,11 @@ test('router.createPipeTransport() with wrong arguments rejects with TypeError',

test('router.createPipeTransport() with enableRtx succeeds', async () => {
const pipeTransport = await ctx.router1!.createPipeTransport({
listenInfo: { protocol: 'udp', ip: '127.0.0.1' },
listenInfo: {
protocol: 'udp',
ip: '127.0.0.1',
portRange: { min: 2000, max: 3000 },
},
enableRtx: true,
});

Expand Down Expand Up @@ -583,7 +597,11 @@ test('pipeTransport.connect() with valid SRTP parameters succeeds', async () =>

test('pipeTransport.connect() with srtpParameters fails if enableSrtp is unset', async () => {
const pipeTransport = await ctx.router1!.createPipeTransport({
listenInfo: { protocol: 'udp', ip: '127.0.0.1' },
listenInfo: {
protocol: 'udp',
ip: '127.0.0.1',
portRange: { min: 2000, max: 3000 },
},
enableRtx: true,
});

Expand Down
17 changes: 16 additions & 1 deletion node/src/test/test-PlainTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ afterEach(async () => {

test('router.createPlainTransport() succeeds', async () => {
const plainTransport = await ctx.router!.createPlainTransport({
listenInfo: { protocol: 'udp', ip: '127.0.0.1' },
listenInfo: {
protocol: 'udp',
ip: '127.0.0.1',
portRange: { min: 2000, max: 3000 },
},
});

await expect(ctx.router!.dump()).resolves.toMatchObject({
Expand All @@ -77,6 +81,7 @@ test('router.createPlainTransport() succeeds', async () => {
protocol: 'udp',
ip: '127.0.0.1',
announcedAddress: '9.9.9.1',
portRange: { min: 2000, max: 3000 },
},
enableSctp: true,
appData: { foo: 'bar' },
Expand Down Expand Up @@ -171,6 +176,16 @@ test('router.createPlainTransport() with wrong arguments rejects with TypeError'
// @ts-ignore
await expect(ctx.router!.createPlainTransport({})).rejects.toThrow(TypeError);

await expect(
ctx.router!.createPlainTransport({
listenInfo: {
protocol: 'udp',
ip: '127.0.0.1',
portRange: { min: 4000, max: 3000 },
},
})
).rejects.toThrow(TypeError);

await expect(
ctx.router!.createPlainTransport({ listenIp: '123' })
).rejects.toThrow(TypeError);
Expand Down
71 changes: 70 additions & 1 deletion node/src/test/test-WebRtcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,76 @@ test('worker.createWebRtcServer() succeeds', async () => {
expect(ctx.worker!.webRtcServersForTesting.size).toBe(0);
}, 2000);

test('worker.createWebRtcServer() without specifying port succeeds', async () => {
test('worker.createWebRtcServer() with portRange succeeds', async () => {
const onObserverNewWebRtcServer = jest.fn();

ctx.worker!.observer.once('newwebrtcserver', onObserverNewWebRtcServer);

const port1 = await pickPort({
type: 'udp',
ip: '127.0.0.1',
reserveTimeout: 0,
});
const port2 = await pickPort({
type: 'udp',
ip: '127.0.0.1',
reserveTimeout: 0,
});

const webRtcServer = await ctx.worker!.createWebRtcServer({
listenInfos: [
{
protocol: 'udp',
ip: '127.0.0.1',
portRange: { min: port1, max: port1 },
},
{
protocol: 'tcp',
ip: '127.0.0.1',
announcedAddress: '1.2.3.4',
portRange: { min: port2, max: port2 },
},
],
appData: { foo: 123 },
});

expect(onObserverNewWebRtcServer).toHaveBeenCalledTimes(1);
expect(onObserverNewWebRtcServer).toHaveBeenCalledWith(webRtcServer);
expect(typeof webRtcServer.id).toBe('string');
expect(webRtcServer.closed).toBe(false);
expect(webRtcServer.appData).toEqual({ foo: 123 });

await expect(ctx.worker!.dump()).resolves.toMatchObject({
pid: ctx.worker!.pid,
webRtcServerIds: [webRtcServer.id],
routerIds: [],
channelMessageHandlers: {
channelRequestHandlers: [webRtcServer.id],
channelNotificationHandlers: [],
},
});

await expect(webRtcServer.dump()).resolves.toMatchObject({
id: webRtcServer.id,
udpSockets: [{ ip: '127.0.0.1', port: port1 }],
tcpServers: [{ ip: '127.0.0.1', port: port2 }],
webRtcTransportIds: [],
localIceUsernameFragments: [],
tupleHashes: [],
});

// Private API.
expect(ctx.worker!.webRtcServersForTesting.size).toBe(1);

ctx.worker!.close();

expect(webRtcServer.closed).toBe(true);

// Private API.
expect(ctx.worker!.webRtcServersForTesting.size).toBe(0);
}, 2000);

test('worker.createWebRtcServer() without specifying port/portRange succeeds', async () => {
const onObserverNewWebRtcServer = jest.fn();

ctx.worker!.observer.once('newwebrtcserver', onObserverNewWebRtcServer);
Expand Down
Loading

0 comments on commit 1a507ec

Please sign in to comment.