1
0
mirror of https://github.com/fluencelabs/js-libp2p-interfaces synced 2025-04-01 17:51:08 +00:00

154 lines
3.9 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) {
2016-03-06 22:42:53 +00: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()
2016-03-06 22:42:53 +00: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) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
2015-07-13 14:42:27 -07:00
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 1, 10)
2015-07-13 14:42:27 -07:00
})
})
test('1 stream with 100 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 1, 100)
})
})
test('10 stream with 1 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 10, 1)
})
})
test('10 stream with 10 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 10, 10)
})
})
test('10 stream with 100 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 10, 10)
})
})
test('100 stream with 1 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 100, 1)
})
})
test('100 stream with 10 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 100, 10)
})
})
test('100 stream with 100 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 100, 10)
})
})
test('1000 stream with 1 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 1000, 1)
})
})
test('1000 stream with 10 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 1000, 10)
})
})
test('1000 stream with 100 msg', function (t) {
2016-03-06 22:42:53 +00:00
common.setup(test, function (err, muxer) {
t.ifError(err, 'should not throw')
var pair = streamPair.create()
2016-03-06 22:42:53 +00:00
spawnGeneration(t, muxer, pair, pair.other, 1000, 100)
})
})
2015-07-11 19:17:33 -07:00
}
2016-03-06 22:42:53 +00: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
2016-03-06 22:42:53 +00:00
var listener = muxer(listenerSocket, true)
var dialer = muxer(dialerSocket, false)
2015-07-13 10:36:28 -07:00
2016-03-06 22:42:53 +00:00
listener.on('stream', function (stream) {
2015-07-13 10:36:28 -07:00
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++) {
2016-03-06 22:42:53 +00:00
dialer.newStream(function (err, stream) {
2015-07-13 10:36:28 -07:00
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
}