Skip to content

Commit

Permalink
Fix channelManager close method
Browse files Browse the repository at this point in the history
  • Loading branch information
galkin authored and sergiv83 committed Jan 27, 2017
1 parent 505c3de commit 411fd43
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/channelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ channelManager.create = function() {
channelManager.close = function() {
if (!adapter || !adapter.close) return;
adapter.close();

producersByTopic = {};
consumersByTopic = {};
consumerTopicsToCheck = [];
toCheckConsumers = false;
adapter = null;
adapterConfig = null;
};

channelManager.hasChannels = function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "msb",
"version": "1.0.0",
"version": "1.0.1",
"description": "A framework to simplify the implementation of an event-bus oriented microservices architecture",
"license": "MIT",
"main": "index.js",
Expand Down
61 changes: 61 additions & 0 deletions test/channelManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,65 @@ describe('channelManager', function() {
});
});
});

describe('After close', function() {
it('should recreate producer', function(done) {
var mockPublisher = {};

simple.mock(queue, 'Publish').returnWith(mockPublisher);

simple
.mock(mockPublisher, 'channel')
.returnWith({})
.returnWith({})
.returnWith(null);

var producer1a = channelManager.findOrCreateProducer('prod1:1');
var producer1b = channelManager.findOrCreateProducer('prod1:1');
expect(producer1a).equals(producer1b);

channelManager.close();

var producer2 = channelManager.findOrCreateProducer('prod1:1');

expect(producer2).to.not.equal(producer1a);
expect(producer2).to.not.equal(producer1b);
done();
});

it('should recreate consumer', function(done) {
var mockSubscriber = {};

simple.mock(mockSubscriber, 'setMaxListeners');
simple.mock(mockSubscriber, 'on');

simple
.mock(queue, 'Subscribe')
.returnWith(mockSubscriber)
.returnWith(mockSubscriber)
.returnWith(null);

var consumer1a = channelManager.findOrCreateConsumer('con1:1');

expect(queue.Subscribe.called).to.be.true;
expect(queue.Subscribe.lastCall.args[0]).deep.include({
channel: 'con1:1',
host: 'mock.host',
port: '99999'
});

var consumer1b = channelManager.findOrCreateConsumer('con1:1');

expect(consumer1a).equals(consumer1b);

channelManager.close();

var consumer2 = channelManager.findOrCreateConsumer('con1:1');

expect(consumer2).to.not.equal(consumer1a);
expect(consumer2).to.not.equal(consumer1a);

done();
});
});
});

0 comments on commit 411fd43

Please sign in to comment.