Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
test: add stream transition test (#72)
Browse files Browse the repository at this point in the history
Co-authored-by: chengcheng <chengcheng@qcraft.ai>
  • Loading branch information
eng-cc and chengcheng committed Jan 30, 2023
1 parent 08687c3 commit 27ec3da
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/stream.browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import * as underTest from '../src/stream'
import { expect, assert } from 'aegir/chai'
import { bytes } from 'multiformats'
import * as pb from '../proto_ts/message.js'
const TEST_MESSAGE = 'test_messgae'

function setup (): { peerConnection: RTCPeerConnection, datachannel: RTCDataChannel, webrtcStream: underTest.WebRTCStream } {
const peerConnection = new RTCPeerConnection()
Expand All @@ -9,6 +12,14 @@ function setup (): { peerConnection: RTCPeerConnection, datachannel: RTCDataChan
return { peerConnection, datachannel, webrtcStream }
}

function generatePbByFlag (flag?: pb.Message_Flag): Uint8Array {
const testPb: pb.Message = {
flag: flag,
message: bytes.fromString(TEST_MESSAGE)
}
return pb.Message.toBinary(testPb)
}

describe('Stream Stats', () => {
it('can construct', () => {
const { webrtcStream } = setup()
Expand Down Expand Up @@ -66,3 +77,46 @@ describe('Stream Stats', () => {
expect(datachannel.readyState).to.be.oneOf(['closing', 'closed'])
})
})

describe('Stream Read Stats Transition By Incoming Flag', () => {
const webrtcStream = setup().webrtcStream
it('no flag, no transition', () => {
expect(webrtcStream.streamState.state).to.equal(underTest.StreamStates.OPEN)
const IncomingBuffer = generatePbByFlag()
const message = webrtcStream.processIncomingProtobuf(IncomingBuffer)
expect(message).not.equal(undefined)
if (message != null) {
expect(bytes.toString(message)).to.equal(TEST_MESSAGE)
}
expect(webrtcStream.streamState.state).to.equal(underTest.StreamStates.OPEN)
})

it('open to read-close by flag:FIN', () => {
expect(webrtcStream.streamState.state).to.equal(underTest.StreamStates.OPEN)
const IncomingBuffer = generatePbByFlag(pb.Message_Flag.FIN)
webrtcStream.processIncomingProtobuf(IncomingBuffer)
expect(webrtcStream.streamState.state).to.equal(underTest.StreamStates.READ_CLOSED)
})

it('read-close to close by flag:STOP_SENDING', () => {
const IncomingBuffer = generatePbByFlag(pb.Message_Flag.STOP_SENDING)
webrtcStream.processIncomingProtobuf(IncomingBuffer)
expect(webrtcStream.streamState.state).to.equal(underTest.StreamStates.CLOSED)
})
})

describe('Stream Write Stats Transition By Incoming Flag', () => {
const webrtcStream = setup().webrtcStream
it('open to write-close by flag:STOP_SENDING', () => {
expect(webrtcStream.streamState.state).to.equal(underTest.StreamStates.OPEN)
const IncomingBuffer = generatePbByFlag(pb.Message_Flag.STOP_SENDING)
webrtcStream.processIncomingProtobuf(IncomingBuffer)
expect(webrtcStream.streamState.state).to.equal(underTest.StreamStates.WRITE_CLOSED)
})

it('write-close to close by flag:FIN', () => {
const IncomingBuffer = generatePbByFlag(pb.Message_Flag.FIN)
webrtcStream.processIncomingProtobuf(IncomingBuffer)
expect(webrtcStream.streamState.state).to.equal(underTest.StreamStates.CLOSED)
})
})

0 comments on commit 27ec3da

Please sign in to comment.