Skip to content

Commit

Permalink
test(AdapterConsumer): add cases for concurrent call ensureConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
CheerlessCloud committed Oct 12, 2018
1 parent 2cf6bdd commit dd3a0dc
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions src/AdapterConsumer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-disable no-param-reassign */
import test from 'ava';
import { stub } from 'sinon';
import AdapterConsumer from './AdapterConsumer';
import AMQPAdapter from './AMQPAdapter';

test.serial(
'call ensureConnection on already connected instance should reuse connection',
async t => {
const connectStub = stub(AMQPAdapter, 'connect').resolves({});
const adapterConsumer = new class extends AdapterConsumer {}();
await adapterConsumer.ensureConnection();

t.true(connectStub.calledOnce);

await adapterConsumer.ensureConnection();
await adapterConsumer.ensureConnection();

t.true(connectStub.calledOnce);
connectStub.restore();
},
);

test.serial(
'concurrent call ensureConnection on same instance should call once connect',
async t => {
let resolvePromise = null;
const promise = new Promise(resolve => {
resolvePromise = resolve;
});

const connectStub = stub(AMQPAdapter, 'connect').resolves(promise);

const adapterConsumer = new class extends AdapterConsumer {}();

const connectPromises = Array.from({ length: 10 }).map(() =>
adapterConsumer.ensureConnection(),
);
resolvePromise({});

await Promise.all(connectPromises);

t.true(connectStub.calledOnce);

connectStub.restore();
},
);

test.serial('exception flow in call concurrent ensureConnection', async t => {
const connectError = new Error('Some connection error');
const connectStub = stub(AMQPAdapter, 'connect')
.onFirstCall()
.rejects(connectError)
.onSecondCall()
.resolves({});

const adapterConsumer = new class extends AdapterConsumer {}();

const connectPromises = Array.from({ length: 10 })
.map(() => adapterConsumer.ensureConnection({ connectParams: { firstCall: true } }))
.map(promise => promise.catch(err => ({ err })));

for (const { err } of await Promise.all(connectPromises)) {
t.truthy(err);
t.is(err, connectError);
}

t.true(connectStub.calledOnce);
t.true(connectStub.getCall(0).args.pop().firstCall);

// try connect again
await adapterConsumer.ensureConnection({ connectParams: { secondCall: true } });

t.true(connectStub.calledTwice);
t.true(connectStub.getCall(1).args.pop().secondCall);

connectStub.restore();
});

0 comments on commit dd3a0dc

Please sign in to comment.