Skip to content

Commit cd1f772

Browse files
committed
fix: test coverage
1 parent 7ff97a2 commit cd1f772

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

src/PgIpLock.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ export class PgIpLock implements AnyLock {
310310
* @return {Promise<void>}
311311
*/
312312
private async createLock(): Promise<void> {
313+
// istanbul ignore if
313314
if (this.uniqueKey) {
314315
await this.createUniqueLock();
315316

@@ -407,6 +408,7 @@ export class PgIpLock implements AnyLock {
407408
COMMIT
408409
`);
409410
} catch (err) {
411+
// istanbul ignore next
410412
await this.options.pgClient.query(`
411413
ROLLBACK
412414
`);
@@ -444,6 +446,7 @@ export class PgIpLock implements AnyLock {
444446
COMMIT
445447
`);
446448
} catch (err) {
449+
// istanbul ignore next
447450
await this.options.pgClient.query(`
448451
ROLLBACK
449452
`);

src/PgPubSub.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ export class PgPubSub extends EventEmitter {
382382
* @return {Promise<void>}
383383
*/
384384
public async listen(channel: string): Promise<void> {
385+
// istanbul ignore if
385386
if (this.options.executionLock) {
386387
await this.pgClient.query(`LISTEN ${ident(channel)}`);
387388
this.emit('listen', channel);
@@ -606,9 +607,11 @@ export class PgPubSub extends EventEmitter {
606607
notification.channel,
607608
notification.payload,
608609
));
609-
const acquired = await lock.acquire();
610610

611-
if (this.options.singleListener && !acquired) {
611+
await lock.acquire();
612+
613+
// istanbul ignore if
614+
if (this.options.singleListener && !lock.isAcquired()) {
612615
return; // we are not really a listener
613616
}
614617

test/src/PgPubSub.ts

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ describe('PgPubSub', () => {
2424
let pgClient: Client;
2525
let pubSub: PgPubSub;
2626

27+
const listenFunc = (pubSubCopy: PgPubSub) => {
28+
pubSubCopy.listen('TestChannel').then(() => {
29+
pgClient.emit('notification', {
30+
channel: 'TestChannel',
31+
payload: 'true',
32+
});
33+
});
34+
}
35+
2736
beforeEach(() => {
2837
pgClient = new Client();
2938
pubSub = new PgPubSub({ pgClient });
@@ -192,12 +201,7 @@ describe('PgPubSub', () => {
192201
it('should handle messages from db with acquired lock', done => {
193202
pubSub.options.singleListener = true;
194203

195-
pubSub.listen('TestChannel').then(() => {
196-
pgClient.emit('notification', {
197-
channel: 'TestChannel',
198-
payload: 'true',
199-
});
200-
});
204+
listenFunc(pubSub);
201205

202206
pubSub.on('message', (chanel, message) => {
203207
expect(chanel).equals('TestChannel');
@@ -230,7 +234,7 @@ describe('PgPubSub', () => {
230234
const channel = `__${PgIpLock.name}__:TestChannel`;
231235

232236
await pubSub.listen('TestChannel');
233-
await pgClient.emit('notification', {
237+
pgClient.emit('notification', {
234238
channel,
235239
payload: 'true',
236240
});
@@ -240,6 +244,34 @@ describe('PgPubSub', () => {
240244
)).to.be.false;
241245
expect(spyChannel.called).to.be.false;
242246
});
247+
it('should handle messages from db with acquired execution '
248+
+ 'lock', done => {
249+
pubSub = new PgPubSub({
250+
pgClient, executionLock: true, singleListener: true,
251+
});
252+
253+
listenFunc(pubSub);
254+
255+
pubSub.on('message', (chanel, message) => {
256+
expect(chanel).equals('TestChannel');
257+
expect(message).equals(true);
258+
done();
259+
});
260+
});
261+
it('should handle messages from db with acquired execution '
262+
+ 'lock and multiple listeners', done => {
263+
pubSub = new PgPubSub({
264+
pgClient, executionLock: true, singleListener: false,
265+
});
266+
267+
listenFunc(pubSub);
268+
269+
pubSub.on('message', (chanel, message) => {
270+
expect(chanel).equals('TestChannel');
271+
expect(message).equals(true);
272+
done();
273+
});
274+
});
243275
});
244276
describe('unlisten()', () => {
245277
it('should call SQL UNLISTEN "channel" command', async () => {
@@ -466,6 +498,42 @@ describe('PgPubSub', () => {
466498

467499
expect(counter).equals(1);
468500
});
501+
it('should filter messages if set and "filtered" option is set and'
502+
+ ' execution lock is set', async () => {
503+
const pubSubCopy = new PgPubSub({
504+
singleListener: false,
505+
filtered: false,
506+
executionLock: true,
507+
pgClient,
508+
});
509+
(pubSubCopy as any).processId = 7777;
510+
511+
await pubSubCopy.listen('Test');
512+
let counter = 0;
513+
514+
pubSubCopy.channels.on('Test', () => ++counter);
515+
pgClient.emit('notification', {
516+
processId: 7777,
517+
channel: 'Test',
518+
payload: 'true',
519+
});
520+
521+
await new Promise(res => setTimeout(res));
522+
523+
expect(counter).equals(1);
524+
525+
pubSubCopy.options.filtered = true;
526+
pgClient.emit('notification', {
527+
processId: 7777,
528+
channel: 'Test',
529+
payload: 'true',
530+
});
531+
532+
await new Promise(res => setTimeout(res));
533+
534+
expect(counter).equals(1);
535+
await pubSub.destroy();
536+
});
469537
});
470538
describe('destroy()', () => {
471539
it('should properly handle destruction', async () => {

0 commit comments

Comments
 (0)