-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(AdapterConsumer): add cases for concurrent call ensureConnection
- Loading branch information
1 parent
2cf6bdd
commit dd3a0dc
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |