2016-08-18 16:18:17 +02:00
|
|
|
/* eslint-env mocha */
|
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const chai = require('chai')
|
|
|
|
chai.use(require('chai-checkmark'))
|
2019-09-16 16:48:04 +01:00
|
|
|
const { expect } = chai
|
|
|
|
const pair = require('it-pair/duplex')
|
|
|
|
const pipe = require('it-pipe')
|
|
|
|
const { collect, map, consume } = require('streaming-iterables')
|
|
|
|
|
|
|
|
async function closeAndWait (stream) {
|
|
|
|
await pipe([], stream, consume)
|
|
|
|
expect(true).to.be.true.mark()
|
2016-08-18 16:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = (common) => {
|
|
|
|
describe('base', () => {
|
2019-09-16 16:48:04 +01:00
|
|
|
let Muxer
|
2016-08-18 16:18:17 +02:00
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
beforeEach(async () => {
|
|
|
|
Muxer = await common.setup()
|
2016-08-18 16:18:17 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Open a stream from the dialer', (done) => {
|
|
|
|
const p = pair()
|
2019-09-16 16:48:04 +01:00
|
|
|
const dialer = new Muxer()
|
2016-08-18 16:18:17 +02:00
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
const listener = new Muxer(stream => {
|
2016-08-18 16:18:17 +02:00
|
|
|
expect(stream).to.exist.mark()
|
|
|
|
closeAndWait(stream)
|
|
|
|
})
|
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
pipe(p[0], dialer, p[0])
|
|
|
|
pipe(p[1], listener, p[1])
|
|
|
|
|
|
|
|
expect(3).checks(done)
|
|
|
|
|
|
|
|
const conn = dialer.newStream()
|
2016-08-18 16:18:17 +02:00
|
|
|
|
|
|
|
closeAndWait(conn)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Open a stream from the listener', (done) => {
|
|
|
|
const p = pair()
|
2019-09-16 16:48:04 +01:00
|
|
|
const dialer = new Muxer(stream => {
|
2016-08-18 16:18:17 +02:00
|
|
|
expect(stream).to.exist.mark()
|
|
|
|
closeAndWait(stream)
|
|
|
|
})
|
2019-09-16 16:48:04 +01:00
|
|
|
const listener = new Muxer()
|
2016-08-18 16:18:17 +02:00
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
pipe(p[0], dialer, p[0])
|
|
|
|
pipe(p[1], listener, p[1])
|
|
|
|
|
|
|
|
expect(3).check(done)
|
|
|
|
|
|
|
|
const conn = listener.newStream()
|
2016-08-18 16:18:17 +02:00
|
|
|
|
|
|
|
closeAndWait(conn)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Open a stream on both sides', (done) => {
|
|
|
|
const p = pair()
|
2019-09-16 16:48:04 +01:00
|
|
|
const dialer = new Muxer(stream => {
|
2016-08-18 16:18:17 +02:00
|
|
|
expect(stream).to.exist.mark()
|
|
|
|
closeAndWait(stream)
|
|
|
|
})
|
2019-09-16 16:48:04 +01:00
|
|
|
const listener = new Muxer(stream => {
|
2016-08-18 16:18:17 +02:00
|
|
|
expect(stream).to.exist.mark()
|
|
|
|
closeAndWait(stream)
|
|
|
|
})
|
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
pipe(p[0], dialer, p[0])
|
|
|
|
pipe(p[1], listener, p[1])
|
|
|
|
|
|
|
|
expect(6).check(done)
|
|
|
|
|
|
|
|
const listenerConn = listener.newStream()
|
|
|
|
const dialerConn = dialer.newStream()
|
2016-08-18 16:18:17 +02:00
|
|
|
|
|
|
|
closeAndWait(dialerConn)
|
|
|
|
closeAndWait(listenerConn)
|
|
|
|
})
|
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
it('Open a stream on one side, write, open a stream on the other side', (done) => {
|
|
|
|
const toString = map(c => c.slice().toString())
|
2016-08-18 16:18:17 +02:00
|
|
|
const p = pair()
|
2019-09-16 16:48:04 +01:00
|
|
|
const dialer = new Muxer()
|
|
|
|
const listener = new Muxer(stream => {
|
|
|
|
pipe(stream, toString, collect).then(chunks => {
|
|
|
|
expect(chunks).to.be.eql(['hey']).mark()
|
|
|
|
})
|
2016-08-18 16:18:17 +02:00
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
dialer.onStream = onDialerStream
|
2016-08-18 16:18:17 +02:00
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
const listenerConn = listener.newStream()
|
2017-01-24 09:25:24 +00:00
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
pipe(['hello'], listenerConn)
|
2016-08-18 16:18:17 +02:00
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
async function onDialerStream (stream) {
|
|
|
|
const chunks = await pipe(stream, toString, collect)
|
|
|
|
expect(chunks).to.be.eql(['hello']).mark()
|
2016-08-18 16:18:17 +02:00
|
|
|
}
|
|
|
|
})
|
2017-01-11 21:57:37 +01:00
|
|
|
|
2019-09-16 16:48:04 +01:00
|
|
|
pipe(p[0], dialer, p[0])
|
|
|
|
pipe(p[1], listener, p[1])
|
|
|
|
|
|
|
|
expect(2).check(done)
|
|
|
|
|
|
|
|
const dialerConn = dialer.newStream()
|
|
|
|
|
|
|
|
pipe(['hey'], dialerConn)
|
2016-08-18 16:18:17 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|