This repository has been archived on 2023-04-09. You can view files and clone it, but cannot push or open issues or pull requests.
kernel-protomux/test.js

318 lines
5.5 KiB
JavaScript
Raw Permalink Normal View History

2021-11-10 14:15:09 +00:00
const Protomux = require('./')
const SecretStream = require('@hyperswarm/secret-stream')
const test = require('brittle')
const c = require('compact-encoding')
2022-07-06 12:39:04 +00:00
const b4a = require('b4a')
2021-11-10 14:15:09 +00:00
test('basic', function (t) {
2021-12-30 20:13:47 +00:00
const a = new Protomux(new SecretStream(true))
const b = new Protomux(new SecretStream(false))
replicate(a, b)
const p = a.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'foo',
onopen () {
2021-12-30 20:13:47 +00:00
t.pass('a remote opened')
2022-02-01 18:09:56 +00:00
}
})
p.open()
2022-02-01 18:09:56 +00:00
p.addMessage({
encoding: c.string,
onmessage (message) {
2021-12-30 20:13:47 +00:00
t.is(message, 'hello world')
}
})
2021-11-10 14:15:09 +00:00
const bp = b.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'foo'
2021-12-30 20:13:47 +00:00
})
2021-11-10 14:15:09 +00:00
2022-02-01 18:09:56 +00:00
t.plan(2)
2021-11-10 14:15:09 +00:00
bp.open()
2022-02-01 18:09:56 +00:00
bp.addMessage({ encoding: c.string }).send('hello world')
2021-11-10 14:15:09 +00:00
})
test('echo message', function (t) {
2021-12-30 20:13:47 +00:00
const a = new Protomux(new SecretStream(true))
2022-02-01 18:09:56 +00:00
const b = new Protomux(new SecretStream(false))
2021-11-10 14:15:09 +00:00
replicate(a, b)
const ap = a.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'foo'
2022-02-01 18:09:56 +00:00
})
ap.open()
2022-02-01 18:09:56 +00:00
const aEcho = ap.addMessage({
encoding: c.string,
onmessage (message) {
aEcho.send('echo: ' + message)
2021-12-30 20:13:47 +00:00
}
})
2021-11-10 14:15:09 +00:00
b.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'other'
}).open()
2021-11-10 14:15:09 +00:00
const bp = b.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'foo',
onopen () {
2021-12-30 20:13:47 +00:00
t.pass('b remote opened')
2022-02-01 18:09:56 +00:00
}
})
bp.open()
2022-02-01 18:09:56 +00:00
const bEcho = bp.addMessage({
encoding: c.string,
onmessage (message) {
2021-12-30 20:13:47 +00:00
t.is(message, 'echo: hello world')
}
})
2021-11-10 14:15:09 +00:00
2022-02-01 18:09:56 +00:00
t.plan(2)
2021-11-10 14:15:09 +00:00
2022-02-01 18:09:56 +00:00
bEcho.send('hello world')
2021-11-10 14:15:09 +00:00
})
test('multi message', function (t) {
2021-12-30 20:13:47 +00:00
const a = new Protomux(new SecretStream(true))
a.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'other'
}).open()
2021-12-30 20:13:47 +00:00
const ap = a.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'multi'
2021-12-30 20:13:47 +00:00
})
2021-11-10 14:15:09 +00:00
ap.open()
2022-02-01 18:09:56 +00:00
const a1 = ap.addMessage({ encoding: c.int })
const a2 = ap.addMessage({ encoding: c.string })
const a3 = ap.addMessage({ encoding: c.string })
2021-12-30 20:13:47 +00:00
const b = new Protomux(new SecretStream(false))
const bp = b.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'multi'
2021-12-30 20:13:47 +00:00
})
2021-11-10 14:15:09 +00:00
bp.open()
2022-02-01 18:09:56 +00:00
const b1 = bp.addMessage({ encoding: c.int })
const b2 = bp.addMessage({ encoding: c.string })
2021-11-10 14:15:09 +00:00
replicate(a, b)
2022-02-01 18:09:56 +00:00
t.plan(2)
2021-11-10 14:15:09 +00:00
2022-02-01 18:09:56 +00:00
a1.send(42)
a2.send('a string with 42')
a3.send('should be ignored')
2021-11-10 14:15:09 +00:00
const expected = [
2022-02-01 18:09:56 +00:00
42,
'a string with 42'
2021-11-10 14:15:09 +00:00
]
2022-02-01 18:09:56 +00:00
b1.onmessage = function (message) {
t.is(message, expected.shift())
}
b2.onmessage = function (message) {
t.is(message, expected.shift())
2021-11-10 14:15:09 +00:00
}
})
2021-12-27 15:09:34 +00:00
test('corks', function (t) {
2021-12-30 20:13:47 +00:00
const a = new Protomux(new SecretStream(true))
a.cork()
a.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'other'
}).open()
2021-12-30 20:13:47 +00:00
const ap = a.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'multi'
2021-12-30 20:13:47 +00:00
})
2021-12-27 15:09:34 +00:00
ap.open()
2022-02-01 18:09:56 +00:00
const a1 = ap.addMessage({ encoding: c.int })
const a2 = ap.addMessage({ encoding: c.string })
2021-12-30 20:13:47 +00:00
const b = new Protomux(new SecretStream(false))
const bp = b.createChannel({
2022-03-10 22:08:37 +00:00
protocol: 'multi'
2021-12-30 20:13:47 +00:00
})
2021-12-27 15:09:34 +00:00
bp.open()
2022-02-01 18:09:56 +00:00
const b1 = bp.addMessage({ encoding: c.int })
const b2 = bp.addMessage({ encoding: c.string })
2021-12-27 15:09:34 +00:00
replicate(a, b)
2022-02-01 18:09:56 +00:00
t.plan(4 + 1)
2021-12-27 15:09:34 +00:00
const expected = [
2022-02-01 18:09:56 +00:00
1,
2,
3,
'a string'
2021-12-27 15:09:34 +00:00
]
2022-02-01 18:09:56 +00:00
a1.send(1)
a1.send(2)
a1.send(3)
a2.send('a string')
2021-12-30 20:13:47 +00:00
a.uncork()
2021-12-27 15:09:34 +00:00
b.stream.once('data', function (data) {
t.ok(expected.length === 0, 'received all messages in one data packet')
})
2022-02-01 18:09:56 +00:00
b1.onmessage = function (message) {
t.is(message, expected.shift())
}
b2.onmessage = function (message) {
t.is(message, expected.shift())
2021-12-27 15:09:34 +00:00
}
})
2021-11-10 14:15:09 +00:00
2022-07-06 12:39:04 +00:00
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('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()
}
})
2021-11-10 14:15:09 +00:00
function replicate (a, b) {
a.stream.rawStream.pipe(b.stream.rawStream).pipe(a.stream.rawStream)
}