2016-03-15 18:59:32 +00:00
|
|
|
/* eslint-env mocha */
|
2016-03-23 16:23:10 +01:00
|
|
|
'use strict'
|
2016-03-15 18:59:32 +00:00
|
|
|
|
2020-08-11 14:54:49 +01:00
|
|
|
const { expect } = require('aegir/utils/chai')
|
2017-03-21 14:32:59 +00:00
|
|
|
|
2016-03-15 18:59:32 +00:00
|
|
|
const multiaddr = require('multiaddr')
|
2019-09-30 12:14:28 +02:00
|
|
|
const pipe = require('it-pipe')
|
|
|
|
const goodbye = require('it-goodbye')
|
|
|
|
const { collect, take } = require('streaming-iterables')
|
2020-08-11 14:54:49 +01:00
|
|
|
const uint8ArrayFromString = require('uint8arrays/from-string')
|
2016-08-11 14:50:44 +02:00
|
|
|
|
2016-06-19 06:35:31 +01:00
|
|
|
const WS = require('../src')
|
2016-03-15 18:59:32 +00:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
const mockUpgrader = {
|
|
|
|
upgradeInbound: maConn => maConn,
|
|
|
|
upgradeOutbound: maConn => maConn
|
|
|
|
}
|
|
|
|
|
2016-06-19 06:35:31 +01:00
|
|
|
describe('libp2p-websockets', () => {
|
2017-10-20 04:12:35 -07:00
|
|
|
const ma = multiaddr('/ip4/127.0.0.1/tcp/9095/ws')
|
2016-06-19 06:35:31 +01:00
|
|
|
let ws
|
2016-08-11 14:50:44 +02:00
|
|
|
let conn
|
2016-03-15 18:59:32 +00:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
beforeEach(async () => {
|
|
|
|
ws = new WS({ upgrader: mockUpgrader })
|
|
|
|
conn = await ws.dial(ma)
|
2016-03-15 18:59:32 +00:00
|
|
|
})
|
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
it('echo', async () => {
|
2020-08-11 14:54:49 +01:00
|
|
|
const message = uint8ArrayFromString('Hello World!')
|
2019-09-30 12:14:28 +02:00
|
|
|
const s = goodbye({ source: [message], sink: collect })
|
2016-03-22 22:14:24 +00:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
const results = await pipe(s, conn, s)
|
|
|
|
expect(results).to.eql([message])
|
2016-08-11 14:50:44 +02:00
|
|
|
})
|
2016-03-22 22:14:24 +00:00
|
|
|
|
2016-08-11 14:50:44 +02:00
|
|
|
describe('stress', () => {
|
2019-09-30 12:14:28 +02:00
|
|
|
it('one big write', async () => {
|
2020-08-11 14:54:49 +01:00
|
|
|
const rawMessage = new Uint8Array(1000000).fill('a')
|
2016-03-22 22:14:24 +00:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
const s = goodbye({ source: [rawMessage], sink: collect })
|
|
|
|
|
|
|
|
const results = await pipe(s, conn, s)
|
|
|
|
expect(results).to.eql([rawMessage])
|
2016-08-11 14:50:44 +02:00
|
|
|
})
|
2016-03-22 22:14:24 +00:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
it('many writes', async function () {
|
2017-10-20 04:12:35 -07:00
|
|
|
this.timeout(10000)
|
2016-08-11 14:50:44 +02:00
|
|
|
const s = goodbye({
|
2019-09-30 12:14:28 +02:00
|
|
|
source: pipe(
|
|
|
|
{
|
|
|
|
[Symbol.iterator] () { return this },
|
2020-08-11 14:54:49 +01:00
|
|
|
next: () => ({ done: false, value: uint8ArrayFromString(Math.random().toString()) })
|
2019-09-30 12:14:28 +02:00
|
|
|
},
|
|
|
|
take(20000)
|
2016-08-11 14:50:44 +02:00
|
|
|
),
|
2019-09-30 12:14:28 +02:00
|
|
|
sink: collect
|
2016-03-22 22:14:24 +00:00
|
|
|
})
|
2016-08-11 14:50:44 +02:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
const result = await pipe(s, conn, s)
|
|
|
|
expect(result).to.have.length(20000)
|
2016-03-22 22:14:24 +00:00
|
|
|
})
|
|
|
|
})
|
2018-04-24 12:45:56 +02:00
|
|
|
|
2019-09-30 12:14:28 +02:00
|
|
|
it('.createServer throws in browser', () => {
|
|
|
|
expect(new WS({ upgrader: mockUpgrader }).createListener).to.throw()
|
|
|
|
})
|
2018-04-24 12:45:56 +02:00
|
|
|
})
|