test: add interface tests

License: MIT
Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
This commit is contained in:
Alan Shaw
2019-04-03 10:38:35 +01:00
parent 1b9c591da6
commit f674122b6f
8 changed files with 126 additions and 56 deletions

View File

@ -7,71 +7,57 @@ const expect = chai.expect
chai.use(dirtyChai)
const multiaddr = require('multiaddr')
const pull = require('pull-stream')
const goodbye = require('pull-goodbye')
const pipe = require('it-pipe')
const goodbye = require('it-goodbye')
const { collect, take } = require('streaming-iterables')
const WS = require('../src')
// require('./adapter/browser')
describe('libp2p-websockets', () => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9095/ws')
let ws
let conn
beforeEach((done) => {
beforeEach(async () => {
ws = new WS()
expect(ws).to.exist()
conn = ws.dial(ma, (err, res) => {
expect(err).to.not.exist()
done()
})
conn = await ws.dial(ma)
})
it('echo', (done) => {
it('echo', async () => {
const message = 'Hello World!'
const s = goodbye({ source: [message], sink: collect })
const s = goodbye({
source: pull.values([message]),
sink: pull.collect((err, results) => {
expect(err).to.not.exist()
expect(results).to.eql([message])
done()
})
})
pull(s, conn, s)
const results = await pipe(s, conn, s)
expect(results).to.eql([message])
})
describe('stress', () => {
it('one big write', (done) => {
it('one big write', async () => {
const rawMessage = Buffer.allocUnsafe(1000000).fill('a')
const s = goodbye({
source: pull.values([rawMessage]),
sink: pull.collect((err, results) => {
expect(err).to.not.exist()
expect(results).to.eql([rawMessage])
done()
})
})
pull(s, conn, s)
const s = goodbye({ source: [rawMessage], sink: collect })
const results = await pipe(s, conn, s)
expect(results).to.eql([rawMessage])
})
it('many writes', function (done) {
this.timeout(10000)
it('many writes', async function () {
this.timeout(100000)
const s = goodbye({
source: pull(
pull.infinite(),
pull.take(1000),
pull.map((val) => Buffer.from(val.toString()))
source: pipe(
{
[Symbol.iterator] () { return this },
next: () => ({ done: false, value: Buffer.from(Math.random().toString()) })
},
take(20000)
),
sink: pull.collect((err, result) => {
expect(err).to.not.exist()
expect(result).to.have.length(1000)
done()
})
sink: collect
})
pull(s, conn, s)
const result = await pipe(s, conn, s)
expect(result).to.have.length(20000)
})
})
})