mirror of
https://github.com/fluencelabs/js-libp2p-websockets
synced 2025-07-31 05:11:57 +00:00
Merge pull request #7 from xicombd/stress-tests
Add browser stress tests
This commit is contained in:
@@ -4,11 +4,12 @@ module.exports = function (config) {
|
||||
frameworks: ['mocha'],
|
||||
|
||||
files: [
|
||||
'tests/browser.js'
|
||||
'tests/browser-nodejs/browser.js'
|
||||
],
|
||||
|
||||
preprocessors: {
|
||||
'tests/*': ['webpack']
|
||||
'tests/*': ['webpack'],
|
||||
'tests/browser-nodejs/*': ['webpack']
|
||||
},
|
||||
|
||||
webpack: {
|
||||
|
@@ -8,7 +8,7 @@
|
||||
"test:compliance:transport": "node tests/transport.js",
|
||||
"test:specific": "mocha tests/*-test.js",
|
||||
"test:node": "npm run test:specific",
|
||||
"test:browser": "node tests/karma",
|
||||
"test:browser": "node tests/browser-nodejs/test.js",
|
||||
"test": "npm run test:node && npm run test:browser",
|
||||
"test-2": "npm run test:specific && npm run test:compliance:transport && npm run test:compliance:connection",
|
||||
"lint": "standard"
|
||||
|
77
tests/browser-nodejs/browser.js
Normal file
77
tests/browser-nodejs/browser.js
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict'
|
||||
/* eslint-env mocha */
|
||||
|
||||
const expect = require('chai').expect
|
||||
const WSlibp2p = require('../../src')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
describe('libp2p-websockets', function () {
|
||||
this.timeout(10000)
|
||||
var ws
|
||||
|
||||
it('create', (done) => {
|
||||
ws = new WSlibp2p()
|
||||
expect(ws).to.exist
|
||||
done()
|
||||
})
|
||||
|
||||
it('echo', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
|
||||
const conn = ws.dial(mh)
|
||||
const message = 'Hello World!'
|
||||
conn.write(message)
|
||||
conn.on('data', (data) => {
|
||||
expect(data.toString()).to.equal(message)
|
||||
conn.end()
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('stress', () => {
|
||||
it('one big write', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
|
||||
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()
|
||||
})
|
||||
})
|
||||
|
||||
it('many writes in 2 batches', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
|
||||
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()
|
||||
})
|
||||
|
||||
conn.on('end', () => {
|
||||
expect(result).to.equal(expected)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
@@ -1,12 +1,12 @@
|
||||
const Server = require('karma').Server
|
||||
const path = require('path')
|
||||
|
||||
const WSlibp2p = require('../src')
|
||||
const WSlibp2p = require('../../src')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
var ws
|
||||
|
||||
function createServer (done) {
|
||||
function createListener (done) {
|
||||
ws = new WSlibp2p()
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
|
||||
ws.createListener(mh, (socket) => {
|
||||
@@ -18,11 +18,11 @@ function stopServer (done) {
|
||||
ws.close(done)
|
||||
}
|
||||
|
||||
function runTests (done) {
|
||||
function run (done) {
|
||||
new Server({
|
||||
configFile: path.join(__dirname, '/../karma.conf.js'),
|
||||
configFile: path.join(__dirname, '/../../karma.conf.js'),
|
||||
singleRun: true
|
||||
}, done).start()
|
||||
}
|
||||
|
||||
createServer(() => runTests(() => stopServer(() => null)))
|
||||
createListener(() => run((exitCode) => stopServer(() => process.exit(exitCode))))
|
@@ -1,28 +0,0 @@
|
||||
/* eslint-env mocha */
|
||||
|
||||
const expect = require('chai').expect
|
||||
const WSlibp2p = require('../src')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
describe('libp2p-websockets', function () {
|
||||
this.timeout(10000)
|
||||
var ws
|
||||
|
||||
it('create', (done) => {
|
||||
ws = new WSlibp2p()
|
||||
expect(ws).to.exist
|
||||
done()
|
||||
})
|
||||
|
||||
it('echo', (done) => {
|
||||
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090/websockets')
|
||||
const conn = ws.dial(mh)
|
||||
const message = 'Hello World!'
|
||||
conn.write(message)
|
||||
conn.on('data', (data) => {
|
||||
expect(data.toString()).to.equal(message)
|
||||
conn.end()
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user