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

171 lines
3.0 KiB
JavaScript
Raw 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')
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)
a.addProtocol({
2021-11-10 14:15:09 +00:00
name: 'foo',
2021-12-30 20:13:47 +00:00
messages: [c.string],
onremoteopen () {
t.pass('a remote opened')
},
onmessage (type, message) {
t.is(type, 0)
t.is(message, 'hello world')
}
})
2021-11-10 14:15:09 +00:00
2021-12-30 20:13:47 +00:00
const bp = b.addProtocol({
2021-11-10 14:15:09 +00:00
name: 'foo',
messages: [c.string]
2021-12-30 20:13:47 +00:00
})
2021-11-10 14:15:09 +00:00
t.plan(3)
bp.send(0, 'hello world')
})
test('echo message', function (t) {
2021-12-30 20:13:47 +00:00
const a = new Protomux(new SecretStream(true))
2021-11-10 14:15:09 +00:00
const b = new Protomux(new SecretStream(false), [{
name: 'other',
messages: [c.bool, c.bool]
}, {
name: 'foo',
messages: [c.string]
}])
replicate(a, b)
2021-12-30 20:13:47 +00:00
const ap = a.addProtocol({
name: 'foo',
messages: [c.string],
onmessage (type, message) {
ap.send(type, 'echo: ' + message)
}
})
2021-11-10 14:15:09 +00:00
2021-12-30 20:13:47 +00:00
b.addProtocol({
name: 'other',
messages: [c.bool, c.bool]
})
2021-11-10 14:15:09 +00:00
2021-12-30 20:13:47 +00:00
const bp = b.addProtocol({
name: 'foo',
messages: [c.string],
onremoteopen () {
t.pass('b remote opened')
},
onmessage (type, message) {
t.is(type, 0)
t.is(message, 'echo: hello world')
}
})
2021-11-10 14:15:09 +00:00
2021-12-30 20:13:47 +00:00
t.plan(3)
2021-11-10 14:15:09 +00:00
2021-12-30 20:13:47 +00:00
bp.send(0, '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.addProtocol({
2021-11-10 14:15:09 +00:00
name: 'other',
messages: [c.bool, c.bool]
2021-12-30 20:13:47 +00:00
})
const ap = a.addProtocol({
2021-11-10 14:15:09 +00:00
name: 'multi',
messages: [c.int, c.string, c.string]
2021-12-30 20:13:47 +00:00
})
2021-11-10 14:15:09 +00:00
2021-12-30 20:13:47 +00:00
const b = new Protomux(new SecretStream(false))
const bp = b.addProtocol({
2021-11-10 14:15:09 +00:00
name: 'multi',
messages: [c.int, c.string]
2021-12-30 20:13:47 +00:00
})
2021-11-10 14:15:09 +00:00
replicate(a, b)
t.plan(4)
ap.send(0, 42)
ap.send(1, 'a string with 42')
ap.send(2, 'should be ignored')
const expected = [
[0, 42],
[1, 'a string with 42']
]
bp.onmessage = function (type, message) {
const e = expected.shift()
t.is(type, e[0])
t.is(message, e[1])
}
})
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.addProtocol({
2021-12-27 15:09:34 +00:00
name: 'other',
messages: [c.bool, c.bool]
2021-12-30 20:13:47 +00:00
})
const ap = a.addProtocol({
2021-12-27 15:09:34 +00:00
name: 'multi',
messages: [c.int, c.string]
2021-12-30 20:13:47 +00:00
})
2021-12-27 15:09:34 +00:00
2021-12-30 20:13:47 +00:00
const b = new Protomux(new SecretStream(false))
const bp = b.addProtocol({
2021-12-27 15:09:34 +00:00
name: 'multi',
messages: [c.int, c.string]
2021-12-30 20:13:47 +00:00
})
2021-12-27 15:09:34 +00:00
replicate(a, b)
t.plan(8 + 1)
const expected = [
[0, 1],
[0, 2],
[0, 3],
[1, 'a string']
]
ap.send(0, 1)
ap.send(0, 2)
ap.send(0, 3)
ap.send(1, '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')
})
bp.onmessage = function (type, message) {
const e = expected.shift()
t.is(type, e[0])
t.is(message, e[1])
}
})
2021-11-10 14:15:09 +00:00
function replicate (a, b) {
a.stream.rawStream.pipe(b.stream.rawStream).pipe(a.stream.rawStream)
}