Skip to content

Commit c8ae9cd

Browse files
authored
Merge pull request #778 from amqp-node/configure-channel-highwatermark
Support channel options like highwatermark
2 parents d2af467 + 6add58c commit c8ae9cd

File tree

4 files changed

+82
-7
lines changed

4 files changed

+82
-7
lines changed

lib/callback_model.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ class CallbackModel extends EventEmitter {
2828
this.connection._updateSecret(newSecret, reason, cb);
2929
}
3030

31-
createChannel (cb) {
31+
createChannel (options, cb) {
32+
if (arguments.length === 1) {
33+
cb = options;
34+
options = undefined;
35+
}
3236
var ch = new Channel(this.connection);
37+
ch.setOptions(options);
3338
ch.open(function (err, ok) {
3439
if (err === null)
3540
cb && cb(null, ch);
@@ -39,8 +44,13 @@ class CallbackModel extends EventEmitter {
3944
return ch;
4045
}
4146

42-
createConfirmChannel (cb) {
47+
createConfirmChannel (options, cb) {
48+
if (arguments.length === 1) {
49+
cb = options;
50+
options = undefined;
51+
}
4352
var ch = new ConfirmChannel(this.connection);
53+
ch.setOptions(options);
4454
ch.open(function (err) {
4555
if (err !== null)
4656
return cb && cb(err);

lib/channel.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ class Channel extends EventEmitter {
4747
this.handleMessage = acceptDeliveryOrReturn;
4848
}
4949

50+
setOptions(options) {
51+
this.options = options;
52+
}
53+
5054
allocate () {
51-
this.ch = this.connection.freshChannel(this);
55+
this.ch = this.connection.freshChannel(this, this.options);
5256
return this;
5357
}
5458

lib/channel_model.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ class ChannelModel extends EventEmitter {
3030
return promisify(this.connection._updateSecret.bind(this.connection))(newSecret, reason);
3131
}
3232

33-
async createChannel() {
33+
async createChannel(options) {
3434
const channel = new Channel(this.connection);
35+
channel.setOptions(options);
3536
await channel.open();
3637
return channel;
3738
}
3839

39-
async createConfirmChannel() {
40+
async createConfirmChannel(options) {
4041
const channel = new ConfirmChannel(this.connection);
42+
channel.setOptions(options);
4143
await channel.open();
4244
await channel.rpc(defs.ConfirmSelect, {nowait: false}, defs.ConfirmSelectOk);
4345
return channel;

test/callback_api.js

+61-2
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,14 @@ suite('updateSecret', function() {
7272
});
7373

7474
function channel_test_fn(method) {
75-
return function(name, chfun) {
75+
return function(name, options, chfun) {
76+
if (arguments.length === 2) {
77+
chfun = options;
78+
options = {};
79+
}
7680
test(name, function(done) {
7781
connect(kCallback(function(c) {
78-
c[method](kCallback(function(ch) {
82+
c[method](options, kCallback(function(ch) {
7983
chfun(ch, done);
8084
}, done));
8185
}, done));
@@ -210,6 +214,33 @@ suite('sending messages', function() {
210214
});
211215
});
212216

217+
var channelOptions = {};
218+
219+
channel_test('find high watermark', function(ch, done) {
220+
var msg = randomString();
221+
var baseline = 0;
222+
ch.assertQueue('', {exclusive: true}, function(e, q) {
223+
if (e !== null) return done(e);
224+
while (ch.sendToQueue(q.queue, Buffer.from(msg))) {
225+
baseline++;
226+
};
227+
channelOptions.highWaterMark = baseline * 2;
228+
done();
229+
})
230+
});
231+
232+
channel_test('set high watermark', channelOptions, function(ch, done) {
233+
var msg = randomString();
234+
ch.assertQueue('', {exclusive: true}, function(e, q) {
235+
if (e !== null) return done(e);
236+
var ok;
237+
for (var i = 0; i < channelOptions.highWaterMark; i++) {
238+
ok = ch.sendToQueue(q.queue, Buffer.from(msg));
239+
assert.equal(ok, true);
240+
}
241+
done();
242+
});
243+
});
213244
});
214245

215246
suite('ConfirmChannel', function() {
@@ -228,6 +259,34 @@ suite('ConfirmChannel', function() {
228259
ch.waitForConfirms(done);
229260
});
230261

262+
var channelOptions = {};
263+
264+
confirm_channel_test('find high watermark', function(ch, done) {
265+
var msg = randomString();
266+
var baseline = 0;
267+
ch.assertQueue('', {exclusive: true}, function(e, q) {
268+
if (e !== null) return done(e);
269+
while (ch.sendToQueue(q.queue, Buffer.from(msg))) {
270+
baseline++;
271+
};
272+
channelOptions.highWaterMark = baseline * 2;
273+
done();
274+
})
275+
});
276+
277+
confirm_channel_test('set high watermark', channelOptions, function(ch, done) {
278+
var msg = randomString();
279+
ch.assertQueue('', {exclusive: true}, function(e, q) {
280+
if (e !== null) return done(e);
281+
var ok;
282+
for (var i = 0; i < channelOptions.highWaterMark; i++) {
283+
ok = ch.sendToQueue(q.queue, Buffer.from(msg));
284+
assert.equal(ok, true);
285+
}
286+
done();
287+
});
288+
});
289+
231290
});
232291

233292
suite("Error handling", function() {

0 commit comments

Comments
 (0)