Skip to content

Instantly share code, notes, and snippets.

@vmolsa
Created March 23, 2015 20:58
Show Gist options
  • Save vmolsa/84aab2974132d189574e to your computer and use it in GitHub Desktop.
Save vmolsa/84aab2974132d189574e to your computer and use it in GitHub Desktop.
node-webrtc test for sending buffers
var rtc = require('rtc-stream');
function runTest(instances, channels, times, size, callback) {
var result = {
instances: 0,
channels: 0,
bytesReceived: 0,
bytesSended: 0,
};
var peers = [];
var superviser = setTimeout(function() {
peers.forEach(function(peer) {
if (peer.alice) {
peer.alice.end();
}
if (peer.bob) {
peer.bob.end();
}
});
}, 5000);
for (var instance = 0; instance < instances; instance++) {
var alice = new rtc();
var bob = new rtc(alice);
var peer = {
alice: alice,
bob: bob
};
peers.push(peer);
alice.on('end', function() {
result.instances++;
peer.alice = null;
if (result.instances >= instances) {
clearTimeout(superviser);
callback(result);
}
});
bob.on('end', function() {
peer.bob = null;
});
alice.on('channel', function(stream) {
stream.on('data', function(data) {
result.bytesReceived += data.length;
stream.write(data, function() {
result.bytesSended += data.length;
});
});
});
for (var channel = 0; channel < channels; channel++) {
var counter = 0;
bob.createChannel('test', function(stream) {
if (!channel) {
throw new Error('Unable to create test channel');
}
stream.on('end', function() {
result.channels++;
if (result.channels >= channels) {
bob.end();
}
});
stream.on('data', function(data) {
result.bytesReceived += data.length;
counter++;
if (counter >= times) {
stream.end();
}
});
stream.on('error', function(error) {
throw error;
});
function sendData(index) {
if (index < times) {
var buffer = new Buffer(size);
buffer.fill('A');
stream.write(buffer, function() {
result.bytesSended += buffer.length;
sendData(index + 1);
});
}
}
sendData(0);
});
}
}
}
function checkResult(result, expected) {
if (result.bytesReceived === expected && result.bytesSended === expected) {
console.log('TEST(bytes: ' + expected + '): SUCCESS!');
return true;
} else {
console.log('TEST(bytes: ' + expected + '): FAILED!');
console.log(result);
return false;
}
}
function runAllTests(tests, pause, index) {
if (!index && index !== 0) {
return runAllTests(tests, pause, 0);
}
if (index < tests.length) {
var option = tests[index];
runTest(option.instances, option.channels, option.times, option.size, function(result) {
if (checkResult(result, option.expected)) {
setTimeout(function() {
runAllTests(tests, pause, index + 1);
}, pause);
}
});
}
}
runAllTests([
{ instances: 1, channels: 1, times: 1, size: 1000, expected: 2000 }, // SUCCESS
{ instances: 1, channels: 1, times: 1, size: 10000, expected: 20000 }, // SUCCESS
//{ instances: 1, channels: 1, times: 1, size: 100000, expected: 200000 }, // FAIL
//{ instances: 1, channels: 1, times: 1, size: 1000000, expected: 2000000 }, // FAIL
//{ instances: 1, channels: 1, times: 10, size: 1000, expected: 20000 }, // ~ bytesReceived == 18000
//{ instances: 1, channels: 1, times: 100, size: 1000, expected: 200000 }, // FAIL
//{ instances: 1, channels: 1, times: 1000, size: 1000, expected: 2000000 }, // FAIL
//{ instances: 1, channels: 1, times: 10000, size: 100, expected: 2000000 }, // SEGFAULT
//{ instances: 1, channels: 1, times: 10000, size: 10, expected: 200000 }, // Error?
//{ instances: 1, channels: 10, times: 10, size: 100, expected: 20000 }, // FAIL
//{ instances: 5, channels: 1, times: 1, size: 100, expected: 2000 }, // FAIL
//{ instances: 2, channels: 1, times: 1, size: 10, expected: 40 }, // ~ bytesReceived == 30 || 20
//{ instances: 2, channels: 1, times: 10, size: 10, expected: 400 } // ~ bytesReceived == 300 || 320
], 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment