mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-06-13 21:31:41 +00:00
feat(pull): migrate to pull streams
This commit is contained in:
committed by
David Dias
parent
3c3a7077f6
commit
3f58dca09a
@ -3,74 +3,67 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream')
|
||||
const goodbye = require('pull-goodbye')
|
||||
|
||||
const WS = require('../src')
|
||||
|
||||
describe('libp2p-websockets', () => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
let ws
|
||||
let conn
|
||||
|
||||
it('create', (done) => {
|
||||
beforeEach((done) => {
|
||||
ws = new WS()
|
||||
expect(ws).to.exist
|
||||
done()
|
||||
conn = ws.dial(ma, done)
|
||||
})
|
||||
|
||||
it('echo', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
const conn = ws.dial(ma)
|
||||
const message = 'Hello World!'
|
||||
conn.write(message)
|
||||
conn.on('data', (data) => {
|
||||
expect(data.toString()).to.equal(message)
|
||||
conn.end()
|
||||
done()
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values([message]),
|
||||
sink: pull.collect((err, results) => {
|
||||
expect(err).to.not.exist
|
||||
expect(results).to.be.eql([message])
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
pull(s, conn, s)
|
||||
})
|
||||
|
||||
describe('stress', () => {
|
||||
it('one big write', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
const conn = ws.dial(mh)
|
||||
const message = new Buffer(1000000).fill('a').toString('hex')
|
||||
conn.write(message)
|
||||
conn.on('data', (data) => {
|
||||
expect(data.toString()).to.equal(message)
|
||||
conn.end()
|
||||
done()
|
||||
const rawMessage = new Buffer(1000000).fill('a')
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values([rawMessage]),
|
||||
sink: pull.collect((err, results) => {
|
||||
expect(err).to.not.exist
|
||||
expect(results).to.be.eql([rawMessage])
|
||||
done()
|
||||
})
|
||||
})
|
||||
pull(s, conn, s)
|
||||
})
|
||||
|
||||
it('many writes in 2 batches', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/ws')
|
||||
const conn = ws.dial(mh)
|
||||
let expected = ''
|
||||
let counter = 0
|
||||
while (++counter < 10000) {
|
||||
conn.write(`${counter} `)
|
||||
expected += `${counter} `
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
while (++counter < 20000) {
|
||||
conn.write(`${counter} `)
|
||||
expected += `${counter} `
|
||||
}
|
||||
|
||||
conn.write('STOP')
|
||||
}, 1000)
|
||||
|
||||
let result = ''
|
||||
conn.on('data', (data) => {
|
||||
if (data.toString() === 'STOP') {
|
||||
conn.end()
|
||||
return
|
||||
}
|
||||
result += data.toString()
|
||||
it('many writes', (done) => {
|
||||
const s = goodbye({
|
||||
source: pull(
|
||||
pull.infinite(),
|
||||
pull.take(1000),
|
||||
pull.map((val) => Buffer(val.toString()))
|
||||
),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist
|
||||
expect(result).to.have.length(1000)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
conn.on('end', () => {
|
||||
expect(result).to.equal(expected)
|
||||
done()
|
||||
})
|
||||
pull(s, conn, s)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
23
test/compliance.node.js
Normal file
23
test/compliance.node.js
Normal file
@ -0,0 +1,23 @@
|
||||
/* eslint-env mocha */
|
||||
'use strict'
|
||||
|
||||
const tests = require('interface-transport')
|
||||
const multiaddr = require('multiaddr')
|
||||
const Ws = require('../src')
|
||||
|
||||
describe('compliance', () => {
|
||||
tests({
|
||||
setup (cb) {
|
||||
let ws = new Ws()
|
||||
const addrs = [
|
||||
multiaddr('/ip4/127.0.0.1/tcp/9091/ws'),
|
||||
multiaddr('/ip4/127.0.0.1/tcp/9092/ws'),
|
||||
multiaddr('/ip4/127.0.0.1/tcp/9093/ws')
|
||||
]
|
||||
cb(null, ws, addrs)
|
||||
},
|
||||
teardown (cb) {
|
||||
cb()
|
||||
}
|
||||
})
|
||||
})
|
83
test/node.js
83
test/node.js
@ -3,19 +3,17 @@
|
||||
|
||||
const expect = require('chai').expect
|
||||
const multiaddr = require('multiaddr')
|
||||
const pull = require('pull-stream')
|
||||
const goodbye = require('pull-goodbye')
|
||||
|
||||
const WS = require('../src')
|
||||
|
||||
require('./compliance.node')
|
||||
|
||||
describe('instantiate the transport', () => {
|
||||
it('create', (done) => {
|
||||
it('create', () => {
|
||||
const ws = new WS()
|
||||
expect(ws).to.exist
|
||||
done()
|
||||
})
|
||||
|
||||
it('create without new', (done) => {
|
||||
const ws = WS()
|
||||
expect(ws).to.exist
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
@ -122,7 +120,7 @@ describe('dial', () => {
|
||||
beforeEach((done) => {
|
||||
ws = new WS()
|
||||
listener = ws.createListener((conn) => {
|
||||
conn.pipe(conn)
|
||||
pull(conn, conn)
|
||||
})
|
||||
listener.listen(ma, done)
|
||||
})
|
||||
@ -133,12 +131,18 @@ describe('dial', () => {
|
||||
|
||||
it('dial on IPv4', (done) => {
|
||||
const conn = ws.dial(ma)
|
||||
conn.write('hey')
|
||||
conn.end()
|
||||
conn.on('data', (chunk) => {
|
||||
expect(chunk.toString()).to.equal('hey')
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values(['hey']),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist
|
||||
|
||||
expect(result).to.be.eql(['hey'])
|
||||
done()
|
||||
})
|
||||
})
|
||||
conn.on('end', done)
|
||||
|
||||
pull(s, conn, s)
|
||||
})
|
||||
|
||||
it.skip('dial on IPv6', (done) => {
|
||||
@ -148,12 +152,18 @@ describe('dial', () => {
|
||||
it('dial on IPv4 with IPFS Id', (done) => {
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/9090/ws/ipfs/Qmb6owHp6eaWArVbcJJbQSyifyJBttMMjYV76N2hMbf5Vw')
|
||||
const conn = ws.dial(ma)
|
||||
conn.write('hey')
|
||||
conn.end()
|
||||
conn.on('data', (chunk) => {
|
||||
expect(chunk.toString()).to.equal('hey')
|
||||
|
||||
const s = goodbye({
|
||||
source: pull.values(['hey']),
|
||||
sink: pull.collect((err, result) => {
|
||||
expect(err).to.not.exist
|
||||
|
||||
expect(result).to.be.eql(['hey'])
|
||||
done()
|
||||
})
|
||||
})
|
||||
conn.on('end', done)
|
||||
|
||||
pull(s, conn, s)
|
||||
})
|
||||
})
|
||||
|
||||
@ -204,13 +214,17 @@ describe('valid Connection', () => {
|
||||
dialerObsAddrs = addrs
|
||||
})
|
||||
|
||||
conn.pipe(conn)
|
||||
pull(conn, conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
conn.on('end', onEnd)
|
||||
pull(
|
||||
pull.empty(),
|
||||
conn,
|
||||
pull.onEnd(onEnd)
|
||||
)
|
||||
|
||||
function onEnd () {
|
||||
conn.getObservedAddrs((err, addrs) => {
|
||||
@ -218,7 +232,6 @@ describe('valid Connection', () => {
|
||||
listenerObsAddrs = addrs
|
||||
|
||||
listener.close(onClose)
|
||||
|
||||
function onClose () {
|
||||
expect(listenerObsAddrs[0]).to.deep.equal(ma)
|
||||
expect(dialerObsAddrs.length).to.equal(0)
|
||||
@ -226,8 +239,6 @@ describe('valid Connection', () => {
|
||||
}
|
||||
})
|
||||
}
|
||||
conn.resume()
|
||||
conn.end()
|
||||
})
|
||||
})
|
||||
|
||||
@ -241,13 +252,17 @@ describe('valid Connection', () => {
|
||||
expect(err).to.exist
|
||||
})
|
||||
|
||||
conn.pipe(conn)
|
||||
pull(conn, conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, () => {
|
||||
const conn = ws.dial(ma)
|
||||
|
||||
conn.on('end', onEnd)
|
||||
pull(
|
||||
pull.empty(),
|
||||
conn,
|
||||
pull.onEnd(onEnd)
|
||||
)
|
||||
|
||||
function onEnd () {
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
@ -255,8 +270,6 @@ describe('valid Connection', () => {
|
||||
listener.close(done)
|
||||
})
|
||||
}
|
||||
conn.resume()
|
||||
conn.end()
|
||||
})
|
||||
})
|
||||
|
||||
@ -272,7 +285,7 @@ describe('valid Connection', () => {
|
||||
expect(peerInfo).to.equal('a')
|
||||
})
|
||||
|
||||
conn.pipe(conn)
|
||||
pull(conn, conn)
|
||||
})
|
||||
|
||||
listener.listen(ma, onListen)
|
||||
@ -280,15 +293,19 @@ describe('valid Connection', () => {
|
||||
const conn = ws.dial(ma)
|
||||
conn.setPeerInfo('b')
|
||||
|
||||
conn.on('end', () => {
|
||||
pull(
|
||||
pull.empty(),
|
||||
conn,
|
||||
pull.onEnd(onEnd)
|
||||
)
|
||||
|
||||
function onEnd () {
|
||||
conn.getPeerInfo((err, peerInfo) => {
|
||||
expect(err).to.not.exist
|
||||
expect(peerInfo).to.equal('b')
|
||||
listener.close(done)
|
||||
})
|
||||
})
|
||||
conn.resume()
|
||||
conn.end()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user