Skip to content

Commit

Permalink
fix(message): issue with sending messages > 15kb
Browse files Browse the repository at this point in the history
  • Loading branch information
kalitine committed Jun 22, 2018
1 parent be3d44f commit 75c358b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/service/UserMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class UserMessage extends Service<proto.IMessage, proto.Message> {
const length = Math.min(MAX_USER_MSG_SIZE, bytes.length - MAX_USER_MSG_SIZE * i)
const begin = MAX_USER_MSG_SIZE * i
const end = begin + length
msg.chunk.number = i
msg.chunk.nb = i
msg.chunk.content = new Uint8Array(bytes.slice(begin, end))
res[i] = super.encode(msg)
}
Expand Down
26 changes: 26 additions & 0 deletions test/functional/2-bot_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
botJoin,
botLeave,
cleanWebGroup,
copyArrayBuffer,
randomBigArrayBuffer,
randomKey,
SIGNALING_URL,
wait,
Expand Down Expand Up @@ -360,6 +362,30 @@ describe('🤖 🙂 - 2 members: bot first, then client', () => {
client.send(msgClient)
})

/** @test {WebGroup#sendTo} */
it('broadcast message cutted in chunks (> 15kb)', (done) => {
const msgClient = randomBigArrayBuffer()
const msgBot = copyArrayBuffer(msgClient)
msgBot[0] = 42

// Check bot bot
client.onMessage = (id, msg) => {
called++
expect(msg).toEqual(msgBot)

// Check bot bot
wait(1000)
.then((bot) => {
expect(called).toEqual(1)
done()
})
.catch(fail)
}

// Start sending message
client.send(msgClient)
})

/** @test {WebGroup#sendTo} */
it('private String', (done) => {
const msgClient = 'Art is long, life is short'
Expand Down
20 changes: 20 additions & 0 deletions test/functional/2-client_bot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
cleanWebGroup,
IBotData,
Queue,
randomBigArrayBuffer,
SIGNALING_URL,
wait,
} from '../util/helper'
Expand Down Expand Up @@ -378,6 +379,25 @@ describe('🙂 🤖 - 2 members: client invites bot', () => {
client.send(msg1)
})

/** @test {WebGroup#sendTo} */
it('broadcast message cutted in chunks (> 15kb)', (done) => {
const bytes = randomBigArrayBuffer()

// Check bot bot
wait(1000)
.then(() => botGetData(client.key))
.then((bot) => {
expect(bot.onMessageToBeCalled).toEqual(1)
expect(bot.messages[0].msg).toEqual(Array.from(bytes) as any)
expect(bot.messages[0].id).toEqual(client.myId)
done()
})
.catch(fail)

// Start sending message
client.send(bytes)
})

/** @test {WebGroup#sendTo} */
it('private String', (done) => {
const msg1 = 'Art is long, life is short'
Expand Down
29 changes: 28 additions & 1 deletion test/functional/2-clients.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/// <reference types='jasmine' />

import { SignalingState, Topology, WebGroup, WebGroupState } from '../../src/index.browser'
import { areTheSame, cleanWebGroup, Queue, SIGNALING_URL, wait } from '../util/helper'
import {
areTheSame,
cleanWebGroup,
Queue,
randomBigArrayBuffer,
SIGNALING_URL,
wait,
} from '../util/helper'

const WebGroupOptions = {
signalingServer: SIGNALING_URL,
Expand Down Expand Up @@ -455,6 +462,26 @@ describe('🙂 🙂 - 2 clients', () => {
wg2.send(msg2)
})

/** @test {WebGroup#sendTo} */
it('broadcast message cutted in chunks (> 15kb)', (done) => {
const bytes = randomBigArrayBuffer()

// Code for peer 1
wg1.onMessage = (id, msg) => {
expect(id).toEqual(wg2.myId)
expect(msg instanceof Uint8Array)
expect(msg).toEqual(bytes)
called1++
wait(1000).then(() => {
expect(called1).toEqual(1)
done()
})
}

// Start sending message
wg2.send(bytes)
})

/** @test {WebGroup#sendTo} */
it('private String', (done) => {
const msg1 = 'Art is long, life is short'
Expand Down
41 changes: 40 additions & 1 deletion test/functional/3-clients.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/// <reference types='jasmine' />
/* tslint:disable:one-variable-per-declaration */
import { SignalingState, WebGroup, WebGroupState } from '../../src/index.browser'
import { areTheSame, cleanWebGroup, IMessages, Queue, SIGNALING_URL, wait } from '../util/helper'
import {
areTheSame,
cleanWebGroup,
IMessages,
Queue,
randomBigArrayBuffer,
SIGNALING_URL,
wait,
} from '../util/helper'

const WebGroupOptions = {
signalingServer: SIGNALING_URL,
Expand Down Expand Up @@ -343,6 +351,37 @@ describe('🙂 🙂 🙂 - 3 clients', () => {
wg3.send(msg3)
})

/** @test {WebGroup#sendTo} */
it('broadcast message cutted in chunks (> 15kb)', (done) => {
const bytes = randomBigArrayBuffer()
const queue = new Queue(2, () => {
wait(1000).then(() => {
expect(called2).toEqual(1)
expect(called3).toEqual(1)
done()
})
})

// Code for peer 2
wg2.onMessage = (id, msg) => {
expect(msg instanceof Uint8Array).toBeTruthy()
expect(msg).toEqual(bytes)
called2++
queue.done()
}

// Code for peer 3
wg3.onMessage = (id, msg) => {
expect(msg instanceof Uint8Array).toBeTruthy()
expect(msg).toEqual(bytes)
called3++
queue.done()
}

// Start sending message
wg1.send(bytes)
})

/** @test {WebGroup#sendTo} */
it('private String', (done) => {
const queue = new Queue(6, () => {
Expand Down
14 changes: 14 additions & 0 deletions test/util/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export function randomKey(): string {
return result
}

export function randomBigArrayBuffer(): Uint8Array {
const values = new Uint8Array(40000)
crypto.getRandomValues(values)
return values
}

export function copyArrayBuffer(bytes: Uint8Array): Uint8Array {
const result = new Uint8Array(bytes.length)
for (let i = 0; i < bytes.length; i++) {
result[i] = bytes[i]
}
return result
}

export function areTheSame(
array1: Array<number | string | boolean | Uint8Array>,
array2: Array<number | string | boolean | Uint8Array>
Expand Down

0 comments on commit 75c358b

Please sign in to comment.