Skip to content

Commit

Permalink
Fixed Rooms unit test, and added additional ice server unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sebjf committed Dec 5, 2023
1 parent 26d01e4 commit d310028
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Node/modules/ice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ function unlink (room: Room, uri: string): void {
}

// Remove all ice servers with matching uri
const iceServers: IceServer[] = JSON.parse(args.values[propI])
const iceServerProperty = JSON.parse(args.values[propI])
const iceServers: IceServer[] = iceServerProperty.servers

let modified = false
while (true) {
Expand All @@ -221,7 +222,6 @@ function unlink (room: Room, uri: string): void {
}

if (modified) {
//FIXME:
//room.updateRoom(args);
room.appendProperties('ice-servers', JSON.stringify(iceServerProperty))
}
}
73 changes: 71 additions & 2 deletions Node/tests/ice.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Uuid } from 'ubiq'
import { NetworkScene, UbiqTcpConnection, Uuid, WrappedTcpServer } from 'ubiq'

// This module hooks into a room, so we use some convenience methods from the
// rooms tests

import { cleanupRoomClient, createNewRoomClient } from './rooms.test'
import { IceServerProvider, RoomServer } from 'modules'
import { RoomClient } from 'components'
import { type AddressInfo } from 'net'

describe('Ice Module Tests', () => {
test('Ice Credentials supplied with room', (done) => {
Expand All @@ -15,7 +18,7 @@ describe('Ice Module Tests', () => {
expect(room.properties.has('ice-servers')).toBeTruthy()
const servers = JSON.parse(room.properties.get('ice-servers'))
expect(servers).toHaveProperty('servers')
expect(servers.servers.length).toBeGreaterThan(0)
expect(servers.servers.length).toBeGreaterThan(0) // (This test does assume the server we are connecting to has at least one ice server, such as Google's STUN)
resolve()
})
})
Expand All @@ -24,4 +27,70 @@ describe('Ice Module Tests', () => {

rc.join(uuid)
})

test('Ice Credentials update on expiration', (done) => {
// This particular test runs its own server

const serverConnection = new WrappedTcpServer({
port: 0 // Specifying 0 binds the server to a random free port provided by the OS
})

const port = (serverConnection.server.address() as AddressInfo).port

const roomServer = new RoomServer()
roomServer.addServer(serverConnection)

const iceServerProvider = new IceServerProvider(roomServer)

iceServerProvider.addIceServer(
'stun.none.cs.ucl.ac.uk',
'mysecret',
1.5,
1,
'',
''
)

const connection = UbiqTcpConnection('localhost', port)
const scene = new NetworkScene()
scene.addConnection(connection)
const rc = new RoomClient(scene)

const uuid = Uuid.generate()

let numUpdates = 0
let previousPassword = ''

const completed = new Promise<void>(resolve => {
rc.addListener('OnRoomUpdated', (room) => {
expect(room.properties.has('ice-servers')).toBeTruthy()
const serversProperty = JSON.parse(room.properties.get('ice-servers'))
const servers = serversProperty.servers
if (servers.length > 0) { // (During clean-up, all the servers will be removed leaving this array empty)
expect(servers[0].password).not.toEqual(previousPassword)
previousPassword = servers[0].password // The password is recomputed each timeout
numUpdates++
if (numUpdates === 2) {
resolve()
}
}
})
})

const serverFinished = new Promise<void>(resolve => {
roomServer.addListener('destroy', (room) => {
resolve()
})
})

completed
.then(() => { iceServerProvider.clearIceServers() })
.then(() => { cleanupRoomClient(rc) })
.then(async () => { await serverFinished })
.then(async () => { await roomServer.exit() })
.then(done)
.catch((error) => done(error))

rc.join(uuid)
})
})
6 changes: 5 additions & 1 deletion Node/tests/rooms.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,12 @@ describe('Room Server', () => {
// notification if they are already members at the time the property
// is set.

// roomclients[1] is going to set the property. Setting a Room property
// is a 'request' - there is no guarantee so all clients, including the
// one sending the update, need to wait until it is acknowledged.

const onRoomUpdatedReceived = Promise.all(
[roomclients[0], roomclients[2]].map(async roomclient => {
roomclients.map(async roomclient => {
await new Promise<void>(resolve => {
roomclient.addListener('OnRoomUpdated', () => {
resolve()
Expand Down

0 comments on commit d310028

Please sign in to comment.