This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0f4e7c
commit 1728d16
Showing
3 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
import { expect } from 'aegir/chai' | ||
import * as lengthPrefixed from 'it-length-prefixed' | ||
import { pushable } from 'it-pushable' | ||
import { Message } from '../src/pb/message' | ||
import * as underTest from '../src/stream' | ||
|
||
const setup = (cb: { send: (bytes: Uint8Array) => void }): underTest.WebRTCStream => { | ||
const datachannel = { | ||
readyState: 'open', | ||
send: cb.send, | ||
close: () => {} | ||
} | ||
return new underTest.WebRTCStream({ channel: datachannel as RTCDataChannel, stat: underTest.defaultStat('outbound') }) | ||
} | ||
|
||
const MAX_MESSAGE_SIZE = 16 * 1024 | ||
|
||
describe('Max message size', () => { | ||
it(`sends messages smaller or equal to ${MAX_MESSAGE_SIZE} bytes in one`, async () => { | ||
const sent: Uint8Array[] = [] | ||
const data = new Uint8Array(MAX_MESSAGE_SIZE - 5) | ||
const p = pushable() | ||
|
||
// Make sure that the data that ought to be sent will result in a message with exactly MAX_MESSAGE_SIZE | ||
const messageLengthEncoded = lengthPrefixed.encode.single(Message.encode({ message: data })).subarray() | ||
expect(messageLengthEncoded.length).eq(MAX_MESSAGE_SIZE) | ||
const webrtcStream = setup({ | ||
send: (bytes) => { | ||
sent.push(bytes) | ||
if (p.readableLength === 0) { | ||
webrtcStream.close() | ||
} | ||
} | ||
}) | ||
p.push(data) | ||
p.end() | ||
await webrtcStream.sink(p) | ||
expect(sent).to.deep.equals([messageLengthEncoded]) | ||
}) | ||
|
||
it(`sends messages greather ${MAX_MESSAGE_SIZE} bytes in parts`, async () => { | ||
const sent: Uint8Array[] = [] | ||
const data = new Uint8Array(MAX_MESSAGE_SIZE - 4) | ||
const p = pushable() | ||
|
||
// Make sure that the data that ought to be sent will result in a message with exactly MAX_MESSAGE_SIZE + 1 | ||
const messageLengthEncoded = lengthPrefixed.encode.single(Message.encode({ message: data })).subarray() | ||
expect(messageLengthEncoded.length).eq(MAX_MESSAGE_SIZE + 1) | ||
|
||
const webrtcStream = setup({ | ||
send: (bytes) => { | ||
sent.push(bytes) | ||
if (p.readableLength === 0) { | ||
webrtcStream.close() | ||
} | ||
} | ||
}) | ||
p.push(data) | ||
p.end() | ||
await webrtcStream.sink(p) | ||
|
||
// Message is sent in two parts | ||
expect(sent).to.deep.equals([messageLengthEncoded.subarray(0, messageLengthEncoded.length - 1), messageLengthEncoded.subarray(messageLengthEncoded.length - 1)]) | ||
}) | ||
}) |