js-libp2p-interfaces/tests/stress-test.js

86 lines
2.0 KiB
JavaScript
Raw Normal View History

2015-07-10 22:02:57 -07:00
var streamPair = require('stream-pair')
2015-07-13 10:36:28 -07:00
// var devNull = require('dev-null')
// var bytesStream = require('random-bytes-stream')
2015-07-10 22:02:57 -07:00
module.exports.all = function (test, common) {
2015-07-13 10:36:28 -07:00
test('1 stream with 1 msg', function (t) {
2015-07-10 22:02:57 -07:00
common.setup(test, function (err, Muxer) {
2015-07-11 19:17:33 -07:00
t.ifError(err, 'should not throw')
2015-07-10 22:02:57 -07:00
var pair = streamPair.create()
2015-07-13 10:36:28 -07:00
spawnGeneration(t, Muxer, pair, pair.other, 1, 1)
2015-07-10 22:02:57 -07:00
})
})
2015-07-13 14:42:27 -07:00
test('1 stream with 10 msg', function (t) {
common.setup(test, function (err, Muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
spawnGeneration(t, Muxer, pair, pair.other, 1, 10)
})
})
2015-07-11 19:17:33 -07:00
}
2015-07-13 14:42:27 -07:00
function spawnGeneration (t, Muxer, dialerSocket, listenerSocket, nStreams, nMsg, size) {
t.plan(6 + (nStreams * nMsg))
2015-07-13 10:36:28 -07:00
2015-07-13 14:42:27 -07:00
var msg = !size ? 'simple msg' : 'aaa'
2015-07-13 10:36:28 -07:00
var listenerMuxer = new Muxer()
var dialerMuxer = new Muxer()
2015-07-13 14:42:27 -07:00
var listenerConn = listenerMuxer.attach(listenerSocket, true)
var dialerConn = dialerMuxer.attach(dialerSocket, false)
2015-07-13 10:36:28 -07:00
listenerConn.on('stream', function (stream) {
t.pass('Incoming stream')
stream.on('data', function (chunk) {
2015-07-13 14:42:27 -07:00
t.pass('Received message')
2015-07-13 10:36:28 -07:00
})
stream.on('end', function () {
t.pass('Stream ended on Listener')
2015-07-13 14:42:27 -07:00
stream.end()
2015-07-13 10:36:28 -07:00
})
})
for (var i = 0; i < nStreams; i++) {
dialerConn.dialStream(function (err, stream) {
t.ifError(err, 'Should not throw')
t.pass('Dialed stream')
for (var j = 0; j < nMsg; j++) {
stream.write(msg)
}
stream.on('data', function (chunk) {
t.fail('Should not happen')
})
stream.on('end', function () {
t.pass('Stream ended on Dialer')
})
stream.end()
})
}
2015-07-11 19:17:33 -07:00
}
2015-07-10 22:02:57 -07:00
2015-07-11 21:10:09 -07:00
// function randSizeMsg (sizeWindow) {
// return Math.floor(Math.random() * (sizeWindow[1] - sizeWindow[0] + 1)) + sizeWindow[0]
// }
// tests list:
// SubtestStress1Conn1Stream1Msg
// SubtestStress1Conn1Stream100Msg
// SubtestStress1Conn100Stream100Msg
// SubtestStress1Conn1000Stream10Msg
// SubtestStress1Conn1000Stream100Msg10MB