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

162 lines
4.0 KiB
JavaScript
Raw Normal View History

2015-07-10 22:02:57 -07:00
var streamPair = require('stream-pair')
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)
})
})
test('1 stream with 100 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, 100)
})
})
test('10 stream with 1 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, 10, 1)
})
})
test('10 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, 10, 10)
})
})
test('10 stream with 100 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, 10, 10)
})
})
test('100 stream with 1 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, 100, 1)
})
})
test('100 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, 100, 10)
})
})
test('100 stream with 100 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, 100, 10)
})
})
test('1000 stream with 1 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, 1000, 1)
})
})
test('1000 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, 1000, 10)
})
})
test('1000 stream with 100 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, 1000, 100)
})
})
2015-07-14 19:04:18 -07:00
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(1 + (5 * nStreams) + (nStreams * nMsg))
2015-07-13 10:36:28 -07:00
var msg = !size ? 'simple msg' : 'make the msg bigger'
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
}