Skip to content

Commit

Permalink
Merge pull request #304 from ikhemissi/heartbeat-config
Browse files Browse the repository at this point in the history
Specify heartbeat with object configuration
  • Loading branch information
squaremo committed Dec 4, 2016
2 parents 24f2f27 + bb1e6c8 commit 355ef05
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
9 changes: 8 additions & 1 deletion lib/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ function connect(url, socketOptions, openCallback) {
pass = url.password;
}

fields = openFrames(url.vhost, null, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
var config = {
locale: url.locale,
channelMax: url.channelMax,
frameMax: url.frameMax,
heartbeat: url.heartbeat,
};

fields = openFrames(url.vhost, config, sockopts.credentials || credentials.plain(user, pass), extraClientProperties);
} else {
var parts = URL.parse(url, true); // yes, parse the query string
protocol = parts.protocol;
Expand Down
21 changes: 19 additions & 2 deletions test/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var assert = require('assert');
var util = require('./util');
var net = require('net');
var fail = util.fail, succeed = util.succeed,
kCallback = util.kCallback;
kCallback = util.kCallback,
succeedIfAttributeEquals = util.succeedIfAttributeEquals;

var URL = process.env.URL || 'amqp://localhost';

Expand All @@ -15,7 +16,7 @@ suite("Connect API", function() {
connect('amqp://localhost:23450', {},
kCallback(fail(done), succeed(done)));
});

// %% this ought to fail the promise, rather than throwing an error
test("bad URL", function() {
assert.throws(function() {
Expand All @@ -33,6 +34,22 @@ suite("Connect API", function() {
connect(u, {}, kCallback(fail(done), succeed(done)));
});

test("using custom heartbeat option", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
var config = parts.query || {};
config.heartbeat = 20;
connect(config, {}, kCallback(succeedIfAttributeEquals('heartbeat', 20, done), fail(done)));
});

test("wrongly typed heartbeat option", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
var config = parts.query || {};
config.heartbeat = 'NOT A NUMBER';
connect(config, {}, kCallback(fail(done), succeed(done)));
});

test("using plain credentials", function(done) {
var url = require('url');
var parts = url.parse(URL, true);
Expand Down
14 changes: 14 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ function succeed(done) {
return function() { done(); }
}

// Produce a callback that will complete the test successfully
// only if the value is an object, it has the specified
// attribute, and its value is equals to the expected value
function succeedIfAttributeEquals(attribute, value, done) {
return function(object) {
if (object && !(object instanceof Error) && value === object[attribute]) {
return done();
}

done(new Error(attribute + " is not equal to " + value));
};
}

// Produce a callback that will fail the test, given either an error
// (to be used as a failure continuation) or any other value (to be
// used as a success continuation when failure is expected)
Expand Down Expand Up @@ -196,6 +209,7 @@ module.exports = {
socketPair: socketPair,
runServer: runServer,
succeed: succeed,
succeedIfAttributeEquals: succeedIfAttributeEquals,
fail: fail,
latch: latch,
completes: completes,
Expand Down

0 comments on commit 355ef05

Please sign in to comment.