Skip to content

Commit

Permalink
Add some more topology tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jessehansen committed Dec 22, 2017
1 parent 2d02e37 commit a87c03f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
3 changes: 1 addition & 2 deletions lib/topology.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,7 @@ class Topology {
this.definitions = {
bindings: {},
exchanges: {},
queues: {},
subscriptions: {}
queues: {}
};
}
}
Expand Down
96 changes: 95 additions & 1 deletion test/topology.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ describe('Topology', function() {
mockConnection = {
createChannel: () => mockChannel,
addQueue: () => Promise.resolve(),
on: (event, handler) => emitter.on(event, handler)
on: (event, handler) => emitter.on(event, handler),
reset: () => 0
};
mockChannel = {
bindQueue: function(/* target, source, key */) {
Expand All @@ -34,6 +35,8 @@ describe('Topology', function() {
bindExchange: function(/* target, source, key */) {
return Promise.resolve();
},
check: () => Promise.resolve(),
destroy: () => Promise.resolve(),
assertQueue: () => Promise.resolve(),
assertExchange: () => Promise.resolve(),
once: () => 0,
Expand All @@ -53,6 +56,39 @@ describe('Topology', function() {

expect(topology.onReconnect).to.have.been.calledWith();
});
it('should call check supporting channels', async function() {
let topology = constructTopology();
topology.getChannel('control');
sinon.spy(mockChannel, 'check');

emitter.emit('reconnected');
await Promise.delay(1);

expect(mockChannel.check).to.have.been.calledWith();
});
it('should be ok when channel does not support check', function() {
let topology = constructTopology();
delete mockChannel.check;
topology.getChannel('control');

emitter.emit('reconnected');
});
it('should configure bindings upon reconnect', async function(){
let topology = constructTopology();
sinon.spy(topology, 'configureBindings');

await topology.createBinding({
queue: true,
source: 'some-exchange',
target: 'some-queue',
keys: 'some-key'
});

emitter.emit('reconnected');
await Promise.delay(1);

expect(topology.configureBindings).to.have.been.calledWith(topology.definitions.bindings);
});
});

describe('configureBindings', function() {
Expand Down Expand Up @@ -184,4 +220,62 @@ describe('Topology', function() {
expect(mockChannel.bindQueue).to.have.been.calledWith('some-queue', 'some-exchange', 'some-other-key');
});
});

describe('#getChannel', function() {
let topology;

beforeEach(function() {
topology = constructTopology();
});

it('should create a channel', async function() {
let channel = await topology.getChannel('control');

expect(channel).to.equal(mockChannel);
});

it('should not create a second channel for the same name', async function() {
sinon.spy(mockConnection, 'createChannel');

await topology.getChannel('control');
await topology.getChannel('control');

expect(mockConnection.createChannel).to.have.been.calledOnce;
});
});

describe('#reset', function() {
let topology;

beforeEach(function() {
topology = constructTopology();
});

it('should destroy all channels', async function() {
await topology.getChannel('control');
sinon.spy(mockChannel, 'destroy');

topology.reset();

expect(mockChannel.destroy).to.have.been.calledWith();
expect(topology.channels).to.be.empty;
});

it('should forget all existing definitions', async function() {
await topology.createQueue({ name:'some-queue' });
await topology.createExchange({ name:'some-exchange' });

await topology.createBinding({
queue: true,
source: 'some-exchange',
target: 'some-queue'
});

topology.reset();

expect(topology.definitions.queues).to.be.empty;
expect(topology.definitions.exchanges).to.be.empty;
expect(topology.definitions.bindings).to.be.empty;
});
});
});

0 comments on commit a87c03f

Please sign in to comment.