const Protomux = require('./') const SecretStream = require('@hyperswarm/secret-stream') const test = require('brittle') const c = require('compact-encoding') const b4a = require('b4a') test('basic', function (t) { const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const p = a.createChannel({ protocol: 'foo', onopen() { t.pass('a remote opened') } }) p.open() p.addMessage({ encoding: c.string, onmessage(message) { t.is(message, 'hello world') } }) const bp = b.createChannel({ protocol: 'foo' }) t.plan(2) bp.open() bp.addMessage({ encoding: c.string }).send('hello world') }) test('echo message', function (t) { const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const ap = a.createChannel({ protocol: 'foo' }) ap.open() const aEcho = ap.addMessage({ encoding: c.string, onmessage(message) { aEcho.send('echo: ' + message) } }) b.createChannel({ protocol: 'other' }).open() const bp = b.createChannel({ protocol: 'foo', onopen() { t.pass('b remote opened') } }) bp.open() const bEcho = bp.addMessage({ encoding: c.string, onmessage(message) { t.is(message, 'echo: hello world') } }) t.plan(2) bEcho.send('hello world') }) test('channel opened', async function (t) { const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const ap = a.createChannel({ protocol: 'foo' }) ap.open() t.is(await ap.fullyOpened(), false) // call twice to ensure we hit the cached branch also t.is(await ap.fullyOpened(), false) const ap2 = a.createChannel({ protocol: 'foo' }) ap2.open() const bp = b.createChannel({ protocol: 'foo', onopen() { t.pass('b remote opened') } }) bp.open() t.is(await ap2.fullyOpened(), true) t.is(await bp.fullyOpened(), true) // call twice to ensure we hit the cached branch also t.is(await ap2.fullyOpened(), true) t.is(await bp.fullyOpened(), true) bp.close() await t.execution(bp.fullyClosed()) }) test('watch - multiple observers see the same channel open', function (t) { t.plan(3) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32, 1) // the "owning" library pairs and creates the channel as usual b.pair({ protocol, id }, () => { b.createChannel({ protocol, id }).open() }) // two independent, non-owning observers watch the same protocol/id b.watch({ protocol, id }, (channel) => { t.is(channel.protocol, protocol, 'observer 1 saw the channel open') }) b.watch({ protocol, id }, (channel) => { t.is(channel.protocol, protocol, 'observer 2 saw the channel open') }) a.createChannel({ protocol, id, onopen() { t.pass('a saw its own channel open') } }).open() }) test('watch - id: null matches all id for the protocol', function (t) { t.plan(1) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const idOne = b4a.alloc(32, 1) const idTwo = b4a.alloc(32, 2) const seen = [] b.pair({ protocol }, (id) => { b.createChannel({ protocol, id }).open() }) // Defaults `id` to `null` but set explicitly for clarity b.watch({ protocol, id: null }, (channel) => { seen.push(b4a.toString(channel.id)) if (seen.length === 2) { t.alike(seen.sort(), [b4a.toString(idOne), b4a.toString(idTwo)].sort()) } }) a.createChannel({ protocol, id: idOne }).open() a.createChannel({ protocol, id: idTwo }).open() }) test('watch - undefined callbacks are skipped', (t) => { t.plan(1) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32, 1) b.pair({ protocol, id }, () => { b.createChannel({ protocol, id }).open() }) b.stream.on('error', (err) => t.fail('stream errored: ' + err.message)) b.watch({ protocol, id }) b.watch({ protocol }) b.watch({ protocol, id }, () => t.pass('defined watcher still fires')) a.createChannel({ protocol, id }).open() }) test('unwatch - removes the given callback', (t) => { t.plan(1) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32, 1) b.pair({ protocol, id }, () => { b.createChannel({ protocol, id }).open() }) const shouldNotFire = () => t.fail('unwatched callback should not fire') const shouldFire = () => t.pass('remaining watcher still fires') b.watch({ protocol, id }, shouldNotFire) b.watch({ protocol, id }, shouldFire) b.unwatch({ protocol, id }, shouldNotFire) a.createChannel({ protocol, id }).open() }) test('unwatchAll - removes all callbacks for the protocol/id', async (t) => { t.plan(2) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32, 1) b.pair({ protocol, id }, () => { b.createChannel({ protocol, id }).open() }) const shouldNotFire = () => t.fail('unwatched callback should not fire') b.watch({ protocol, id }, shouldNotFire) b.watch({ protocol, id }, shouldNotFire) b.watch({ protocol }, () => t.pass('watcher for another protocol/id still fires')) b.unwatchAll({ protocol, id }) const channel = a.createChannel({ protocol, id }) channel.open() await channel.fullyOpened() t.pass('channel created') }) test('watch - fires when the local side opened first', async (t) => { t.plan(2) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32, 1) // a accepts via pair, so b's channel is fully opened directly by a's open message a.pair({ protocol, id }, () => { a.createChannel({ protocol, id }).open() }) const bc = b.createChannel({ protocol, id }) b.watch({ protocol, id }, (channel) => { t.is(channel, bc, 'watcher got the locally opened channel') t.ok(channel.opened, 'channel is marked opened') }) bc.open() }) test('watch - throwing emits a warning when the local side opened first', async (t) => { t.plan(3) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const error = new Error('boom') a.pair({ protocol }, () => { a.createChannel({ protocol }).open() }) b.watch({ protocol }, () => { throw error }) b.stream.on('error', (err) => t.fail('stream errored: ' + err.message)) const warned = new Promise((resolve) => b.stream.once('warning', resolve)) const channel = b.createChannel({ protocol }) channel.open() t.is(await warned, error, 'watcher error emitted as a warning') t.ok(await channel.fullyOpened(), 'channel still opened') t.absent(b.stream.destroyed, 'stream was not destroyed') }) test('watch - throwing emits a warning when paired', async (t) => { t.plan(3) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const error = new Error('boom') let channel = null b.pair({ protocol }, () => { channel = b.createChannel({ protocol }) channel.open() }) b.watch({ protocol }, () => { throw error }) b.stream.on('error', (err) => t.fail('stream errored: ' + err.message)) const warned = new Promise((resolve) => b.stream.once('warning', resolve)) a.createChannel({ protocol }).open() t.is(await warned, error, 'watcher error emitted as a warning') t.ok(await channel.fullyOpened(), 'channel still opened') t.absent(b.stream.destroyed, 'stream was not destroyed') }) test('watch - throwing does not stop other watchers from firing', async (t) => { t.plan(2) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32, 1) const error = new Error('boom') b.pair({ protocol, id }, () => { b.createChannel({ protocol, id }).open() }) b.watch({ protocol, id }, () => { throw error }) b.watch({ protocol, id }, () => t.pass('watcher for the same protocol/id fires')) b.watch({ protocol }, () => t.pass('wildcard watcher fires')) b.stream.on('warning', () => {}) b.stream.on('error', (err) => t.fail('stream errored: ' + err.message)) a.createChannel({ protocol, id }).open() }) test('watch - async rejection while open emits a warning', async (t) => { t.plan(3) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const error = new Error('boom') let channel = null b.pair({ protocol }, () => { channel = b.createChannel({ protocol }) channel.open() }) b.watch({ protocol }, async () => { await new Promise((resolve) => setImmediate(resolve)) throw error }) b.stream.on('error', (err) => t.fail('stream errored: ' + err.message)) const warned = new Promise((resolve) => b.stream.once('warning', resolve)) a.createChannel({ protocol }).open() t.is(await warned, error, 'watcher error emitted as a warning') t.absent(channel.closed, 'channel still open') t.absent(b.stream.destroyed, 'stream was not destroyed') }) test('watch - pending watcher does not delay channel destroy', async (t) => { t.plan(2) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' let watched = null const fired = new Promise((resolve) => { watched = resolve }) b.pair({ protocol }, () => { b.createChannel({ protocol }).open() }) b.watch({ protocol }, (channel) => { watched(channel) return new Promise(() => {}) }) a.createChannel({ protocol }).open() const channel = await fired channel.close() await channel.fullyClosed() t.ok(channel.destroyed, 'channel destroyed without waiting for the watcher') t.absent(b.stream.destroyed, 'stream was not destroyed') }) test('multi message', function (t) { const a = new Protomux(new SecretStream(true)) a.createChannel({ protocol: 'other' }).open() const ap = a.createChannel({ protocol: 'multi' }) ap.open() const a1 = ap.addMessage({ encoding: c.int }) const a2 = ap.addMessage({ encoding: c.string }) const a3 = ap.addMessage({ encoding: c.string }) const b = new Protomux(new SecretStream(false)) const bp = b.createChannel({ protocol: 'multi' }) bp.open() const b1 = bp.addMessage({ encoding: c.int }) const b2 = bp.addMessage({ encoding: c.string }) replicate(a, b) t.plan(2) a1.send(42) a2.send('a string with 42') a3.send('should be ignored') const expected = [42, 'a string with 42'] b1.onmessage = function (message) { t.is(message, expected.shift()) } b2.onmessage = function (message) { t.is(message, expected.shift()) } }) test('corks', function (t) { const a = new Protomux(new SecretStream(true)) a.cork() a.createChannel({ protocol: 'other' }).open() const ap = a.createChannel({ protocol: 'multi' }) ap.open() const a1 = ap.addMessage({ encoding: c.int }) const a2 = ap.addMessage({ encoding: c.string }) const b = new Protomux(new SecretStream(false)) const bp = b.createChannel({ protocol: 'multi' }) bp.open() const b1 = bp.addMessage({ encoding: c.int }) const b2 = bp.addMessage({ encoding: c.string }) replicate(a, b) t.plan(4 + 1) const expected = [1, 2, 3, 'a string'] a1.send(1) a1.send(2) a1.send(3) a2.send('a string') a.uncork() b.stream.once('data', function (data) { t.ok(expected.length === 0, 'received all messages in one data packet') }) b1.onmessage = function (message) { t.is(message, expected.shift()) } b2.onmessage = function (message) { t.is(message, expected.shift()) } }) test('mega cork', function (t) { const a = new Protomux(new SecretStream(true)) a.cork() const ap = a.createChannel({ protocol: 'mega' }) ap.open() const a1 = ap.addMessage({ encoding: c.buffer }) const b = new Protomux(new SecretStream(false)) const bp = b.createChannel({ protocol: 'mega' }) bp.open() const b1 = bp.addMessage({ encoding: c.buffer }) replicate(a, b) t.plan(32 + 4) const buf = b4a.alloc(1024 * 1024) const expected = [] for (let i = 0; i < 32; i++) { a1.send(buf) expected.push(buf) } a.uncork() b.stream.on('data', function (data) { t.ok(data.byteLength > 8000000, 'got big message') }) b1.onmessage = function (message) { t.alike(message, expected.shift()) } }) test('batch msg - wrong length', async function (t) { t.plan(1) const a = new Protomux(new SecretStream(true)) const ap = a.createChannel({ protocol: 'test' }) ap.open() const b = new Protomux(new SecretStream(false)) const bp = b.createChannel({ protocol: 'test' }) bp.open() const b1 = bp.addMessage({ encoding: c.int }) replicate(a, b) b.stream.on('error', (err) => { // IF it gets 'uint must be between 0 and 9007199254740991, use biguint', // then it tried to parse an undefined value. Instead it should detect the // OOB. t.is(err.message, 'Out of bounds', 'expect OOB') }) const buf = c.encode(c.uint, 1024 ** 2) // big length const badBuf = b4a.from([ 0x00, // Control session 0x00, // type 0 = batch 0x01, // remote id ...buf ]) a.stream.write(badBuf) b1.onmessage = function (message) { t.comment('Unexpected Message', message) t.fail('should not receive a message from ill formed message.') } }) test('batch msg - nested batch', function (t) { t.plan(1) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) b.stream.on('error', (err) => { t.is(err.message, 'Invalid nested batch') }) const batch = b4a.from([ 0x00, // type 0 = batch 0x00, // remote id 0 = control session ...c.encode(c.uint, 0xffffffff) // length, capped at the end of the outer batch ]) // control session, then batches that each contain the next one a.stream.write(b4a.concat([b4a.from([0x00]), ...new Array(2).fill(batch)])) }) test('handshake', function (t) { const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const p = a.createChannel({ protocol: 'foo', handshake: c.string, onopen(handshake) { t.is(handshake, 'b handshake') } }) p.open('a handshake') const bp = b.createChannel({ protocol: 'foo', handshake: c.string, onopen(handshake) { t.is(handshake, 'a handshake') } }) t.plan(2) bp.open('b handshake') }) test('rejections', function (t) { t.plan(1) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) let closed = 0 for (let i = 0; i < 10; i++) { const p = a.createChannel({ protocol: 'foo#' + i, onclose() { closed++ if (closed === 10) t.pass('all closed') } }) p.open() } }) test('pipeline close and rejections', function (t) { t.plan(1) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) let closed = 0 for (let i = 0; i < 10; i++) { const p = a.createChannel({ protocol: 'foo#' + i, onclose() { closed++ if (closed === 10) { t.pass('all closed') } } }) p.open() p.close() } }) test('alias', function (t) { const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const p = a.createChannel({ protocol: 'foo', aliases: ['bar'], onopen() { t.pass('a remote opened') } }) p.open() p.addMessage({ encoding: c.string, onmessage(message) { t.is(message, 'hello world') } }) const bp = b.createChannel({ protocol: 'bar' }) t.plan(2) bp.open() bp.addMessage({ encoding: c.string }).send('hello world') }) test('deduplicate muxers', function (t) { const sa = new SecretStream(true) const sb = new SecretStream(false) replicate({ stream: sa }, { stream: sb }) const a = Protomux.from(sa) const foo = a.createChannel({ protocol: 'foo', onopen() { t.pass('a remote opened') } }) foo.open() foo.addMessage({ encoding: c.string, onmessage(message) { t.is(message, 'hello foo') } }) const bfoo = Protomux.from(sb).createChannel({ protocol: 'foo' }) // Another Protomux instance for another protocol const a2 = Protomux.from(sa) const bar = a2.createChannel({ protocol: 'bar', onopen() { t.pass('a remote opened') } }) bar.open() bar.addMessage({ encoding: c.string, onmessage(message) { t.is(message, 'hello bar') } }) const bbar = Protomux.from(sb).createChannel({ protocol: 'bar' }) t.plan(4) bfoo.open() bfoo.addMessage({ encoding: c.string }).send('hello foo') bbar.open() bbar.addMessage({ encoding: c.string }).send('hello bar') }) test('open + send + close on same tick', async function (t) { t.plan(4) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const ac = a.createChannel({ protocol: 'foo', onopen() { t.pass('a opened') }, onclose() { t.pass('a closed') } }) ac.open() ac.addMessage({ encoding: c.string, onmessage(message) { t.is(message, 'hello') } }) const bc = b.createChannel({ protocol: 'foo', onopen() { t.fail('b opened') }, onclose() { t.pass('b closed') } }) bc.open() bc.addMessage({ encoding: c.string }).send('hello') bc.close() }) test('drain', function (t) { t.plan(7) const mux1 = new Protomux(new SecretStream(true)) const mux2 = new Protomux(new SecretStream(false)) t.ok(mux1.drained) t.ok(mux2.drained) replicate(mux1, mux2) const a = mux1.createChannel({ protocol: 'foo', messages: [{ encoding: c.string }] }) t.ok(a.drained) a.open() const b = mux2.createChannel({ protocol: 'foo', messages: [{ encoding: c.string }], ondrain(c) { t.is(c, b) t.ok(mux1.drained) } }) t.ok(b.drained) b.open() while (true) { const drained = b.messages[0].send('hello world') if (b.drained !== drained) t.fail('Drained property should be equal as in channel') if (mux2.drained !== drained) t.fail('Drained property should be equal as in mux') if (!drained) { t.pass() break } } }) test('keep alive - one side only', function (t) { t.plan(1) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) a.stream.on('error', (err) => t.fail(err.message)) b.stream.on('error', (err) => t.fail(err.message)) a.stream.setKeepAlive(1) replicate(a, b) setTimeout(() => t.pass(), 500) }) test('isIdle - basic functionality', async function (t) { t.plan(8) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const p = a.createChannel({ protocol: 'foo', onopen() { t.pass('a remote opened') }, onclose() { t.is(a.isIdle(), true, 'a is idle') t.is(b.isIdle(), true, 'b is idle') } }) t.is(a.isIdle(), true, 'a is idle') t.is(b.isIdle(), true, 'b is idle') p.open() p.addMessage({ encoding: c.string, onmessage(message) { t.is(message, 'hello world') } }) const bp = b.createChannel({ protocol: 'foo' }) bp.open() bp.addMessage({ encoding: c.string }).send('hello world') t.is(a.isIdle(), false, 'a is not idle') t.is(b.isIdle(), false, 'b is not idle') bp.close() }) test('id unslabbed when receiving', async function (t) { const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32) const p = a.createChannel({ protocol, id }) p.open() // Hack to make it not insta gc the info b.pair({ protocol, id }, async () => await new Promise((resolve) => setTimeout(resolve, 100))) await new Promise((resolve) => setImmediate(resolve)) t.is([...b._infos.values()][0].id.buffer.byteLength, 32, 'unslabbed id when received from remote') t.is([...a._infos.values()][0].id.buffer.byteLength, 32, 'unslabbed id when set by yourself') }) test('supports setting userData after `.createChannel()` but before `.open()`', async function (t) { // This test showcases why `_fullyOpenSoon()` queues `_fullyOpen()` to defer it running t.plan(1) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32) const p = a.createChannel({ protocol, id }) p.userData = true b.pair({ protocol, id }, async () => { const p2 = b.createChannel({ protocol, id, onopen(_, c) { t.ok(c.userData, 'userData is set on channel onopen') } }) p2.userData = true p2.open() }) p.open() }) test('incoming onopen runs after the pair callback finishes', function (t) { // Strict scheduling test that asserts incoming `onopen` is deferred until after `pair()` returns t.plan(3) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) const protocol = 'foo' const id = b4a.alloc(32) let setupDone = false let opened = false b.pair({ protocol, id }, () => { b.createChannel({ protocol, id, onopen() { opened = true t.ok(setupDone, 'onopen runs after pair callback setup finishes') } }).open() t.absent(opened, 'onopen did not fire before the pair callback returned') setupDone = true }) a.createChannel({ protocol, id }).open() // Wait one turn so the deferred `_fullyOpen()` callback has a chance to run. setImmediate(() => { t.ok(opened, 'onopen fired after the pair callback finished') }) }) test('async onmessage rejecting after channel close does not destroy the stream', async function (t) { t.plan(2) let error = null const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) let streamDestroyed = false a.stream.on('error', function () { t.fail('a stream errored') }) a.stream.on('warning', function (err) { t.is(err, error, 'the stream warned the error') }) a.stream.on('close', function () { streamDestroyed = true }) let rejectMessage = null let handlerReady = null const received = new Promise((resolve) => { handlerReady = resolve }) const aChannel = a.createChannel({ protocol: 'foo' }) aChannel.open() aChannel.addMessage({ encoding: c.string, onmessage() { return new Promise((_, reject) => { rejectMessage = reject handlerReady() }) } }) const bc = b.createChannel({ protocol: 'foo' }) bc.open() bc.addMessage({ encoding: c.string }).send('hello') await received aChannel.close() error = new Error('boom') rejectMessage(error) await new Promise((resolve) => setTimeout(resolve, 50)) t.absent(streamDestroyed, 'stream was not destroyed after the channel closed') }) test('async onmessage rejecting while channel is open still destroys the stream', async function (t) { t.plan(2) const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) let error = null let streamDestroyed = false a.stream.on('error', function (err) { t.is(err, error, 'stream got error') }) a.stream.on('close', function () { streamDestroyed = true }) let rejectMessage = null let handlerReady = null const received = new Promise((resolve) => { handlerReady = resolve }) const ac = a.createChannel({ protocol: 'foo' }) ac.open() ac.addMessage({ encoding: c.string, onmessage() { return new Promise((_, reject) => { rejectMessage = reject handlerReady() }) } }) const bc = b.createChannel({ protocol: 'foo' }) bc.open() bc.addMessage({ encoding: c.string }).send('hello') await received error = new Error('boom') rejectMessage(error) await new Promise((resolve) => setTimeout(resolve, 50)) t.ok(streamDestroyed, 'stream was destroyed while the channel was still open') }) test('async onopen rejecting after channel close does not destroy the stream', async function (t) { t.plan(2) let error = null const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) let streamDestroyed = false b.stream.on('error', function () { t.fail('b stream errored') }) b.stream.on('warning', function (err) { t.is(err, error, 'the stream warned the error') }) b.stream.on('close', function () { streamDestroyed = true }) const protocol = 'foo' let rejectOpen = null let handlerReady = null const opened = new Promise((resolve) => { handlerReady = resolve }) let bc = null b.pair({ protocol }, () => { bc = b.createChannel({ protocol, onopen() { return new Promise((_, reject) => { rejectOpen = reject handlerReady() }) } }) bc.open() }) a.createChannel({ protocol }).open() await opened bc.close() error = new Error('boom') rejectOpen(error) await new Promise(setImmediate) t.absent(streamDestroyed, 'stream was not destroyed after the channel closed') }) test('async onclose rejecting emits a warning instead of destroying the stream', async function (t) { t.plan(3) let error = null const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) let streamDestroyed = false a.stream.on('error', function () { t.fail('a stream errored') }) a.stream.on('warning', function (err) { t.is(err, error, 'the stream warned the error') }) a.stream.on('close', function () { streamDestroyed = true }) let rejectClose = null const ac = a.createChannel({ protocol: 'foo', onclose() { return new Promise((_, reject) => { rejectClose = reject }) } }) ac.open() const bc = b.createChannel({ protocol: 'foo' }) bc.open() // give the channel a chance to fully open before closing it await new Promise(setImmediate) t.ok(ac.opened, 'a is opened') ac.close() error = new Error('boom') rejectClose(error) // Allow warning to propagate await new Promise(setImmediate) t.absent(streamDestroyed, 'stream was not destroyed after the channel closed') }) test('async ondestroy rejecting emits a warning instead of destroying the stream', async function (t) { t.plan(3) let error = null const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) let streamDestroyed = false a.stream.on('error', function () { t.fail('a stream errored') }) a.stream.on('warning', function (err) { t.is(err, error, 'the stream warned the error') }) a.stream.on('close', function () { streamDestroyed = true }) let rejectDestroy = null const ac = a.createChannel({ protocol: 'foo', ondestroy() { return new Promise((_, reject) => { rejectDestroy = reject }) } }) ac.open() const bc = b.createChannel({ protocol: 'foo' }) bc.open() // give the channel a chance to fully open before closing it await new Promise(setImmediate) t.ok(ac.opened, 'a is opened') // onclose is a noop here, so `_active` hits 0 synchronously inside `close()`, // which means `_destroy()` (and therefore `ondestroy`) runs immediately ac.close() error = new Error('boom') rejectDestroy(error) await new Promise(setImmediate) t.absent(streamDestroyed, 'stream was not destroyed after the channel closed') }) test('async ondrain rejecting after channel close does not destroy the stream', async function (t) { t.plan(2) let error = null const a = new Protomux(new SecretStream(true)) const b = new Protomux(new SecretStream(false)) replicate(a, b) let streamDestroyed = false b.stream.on('error', function () { t.fail('b stream errored') }) b.stream.on('warning', function (err) { t.is(err, error, 'the stream warned the error') }) b.stream.on('close', function () { streamDestroyed = true }) a.createChannel({ protocol: 'foo', messages: [{ encoding: c.string }] }).open() let rejectDrain = null let handlerReady = null const drained = new Promise((resolve) => { handlerReady = resolve }) const bc = b.createChannel({ protocol: 'foo', messages: [{ encoding: c.string }], ondrain() { return new Promise((_, reject) => { rejectDrain = reject handlerReady() }) } }) bc.open() // force `bc.messages[0].send()` to return false so a future 'drain' event fires while (bc.messages[0].send('hello world')) { // keep sending until backpressure kicks in } await drained bc.close() error = new Error('boom') rejectDrain(error) await new Promise(setImmediate) t.absent(streamDestroyed, 'stream was not destroyed after the channel closed') }) function replicate(a, b) { a.stream.rawStream.pipe(b.stream.rawStream).pipe(a.stream.rawStream) }