Skip to content

Instantly share code, notes, and snippets.

@mrvisser
Created February 10, 2014 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrvisser/8915100 to your computer and use it in GitHub Desktop.
Save mrvisser/8915100 to your computer and use it in GitHub Desktop.
Reproduces the issue fixed by https://github.com/postwait/node-amqp/pull/293
require('./harness').run();
var _ = require('lodash');
var NUM_SENDBATCH = 100;
var NUM_DEVNULL = 100;
var enoughIO = false;
connection.addListener('ready', function () {
puts('connected to ' + connection.serverProperties.product);
var exchange = connection.exchange('test-buffer-overwrite', {
'type': 'direct',
'autoDelete': true
});
var qSendBatch = connection.queue('test-buffer-overwrite-sendbatch', {'autoDelete': true}, function() {
qSendBatch.bind(exchange, 'test-buffer-overwrite-sendbatch');
var sendBatchRecvCount = 0;
qSendBatch.subscribe({'ack': true, 'prefetchCount': 15}, function(data, headers, deliveryInfo, message) {
sendBatchRecvCount++;
message.acknowledge();
// Perform a secondary check to ensure that this test
// is being effectively executed with enough IO
if (connection.socket.bufferSize > 0) {
enoughIO = true;
}
// Branch out a batch of new messages to generate a bunch of I/O on the
// _sendMethod path
_sendDevNullBatch(exchange);
}).addCallback(function() {
_triggerNewBatch(exchange);
});
});
var qDevNull = connection.queue('test-buffer-overwrite-devnull', {'autoDelete': true}, function() {
qDevNull.bind(exchange, 'test-buffer-overwrite-devnull');
var devNullRecvCount = 0;
qDevNull.subscribe({'ack': true, 'preferchCount': 15}, function(data, headers, deliveryInfo, message) {
devNullRecvCount++;
message.acknowledge();
// Once we've received each devnull message from each batch, the test has succeeded
if (devNullRecvCount === NUM_SENDBATCH * NUM_DEVNULL) {
assert.ok(enoughIO, 'Not enough data was generated from this test to detect an issue');
setTimeout(connection.end, 1000);
}
});
});
});
var _triggerNewBatch = function(exchange) {
for (var i = 0; i < NUM_SENDBATCH; i++) {
exchange.publish('test-buffer-overwrite-sendbatch', {});
}
};
var _sendDevNullBatch = function(exchange) {
for (var i = 0; i < NUM_DEVNULL; i++) {
exchange.publish('test-buffer-overwrite-devnull', _generateMessage());
}
};
var _generateMessage = function() {
var result = '';
// The random size generated here needs to be variable enough to
// make buffer overwrites matter, and big enough to generate enough
// I/O to fill up the socket write buffer
var n = _.random(30000, 31000);
for (var i = 0; i < n; i++) {
result += 'a';
}
return {'value': result};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment