Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(subscription): auto close sub on non-recoverable errors #441

Merged
merged 1 commit into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ export class Subscriber extends EventEmitter {
this._inventory.clear();

await this._waitForFlush();

this.emit('close');
}
/**
* Gets the subscriber client instance.
Expand Down Expand Up @@ -309,7 +311,8 @@ export class Subscriber extends EventEmitter {
this._stream = new MessageStream(this, streamingOptions);

this._stream.on('error', err => this.emit('error', err))
.on('data', (data: PullResponse) => this._onData(data));
.on('data', (data: PullResponse) => this._onData(data))
.once('close', () => this.close());

this._inventory.on('full', () => this._stream.pause())
.on('free', () => this._stream.resume());
Expand Down
6 changes: 5 additions & 1 deletion src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ export interface SubscriptionMetadata extends TSubscriptionMetadata {
* // Register an error handler.
* subscription.on('error', (err) => {});
*
* // Register a close handler in case the subscriber closes unexpectedly
* subscription.on('close', () => {});
*
* // Register a listener for `message` events.
* function onMessage(message) {
* // Called every time a message is received.
Expand Down Expand Up @@ -251,7 +254,8 @@ export class Subscription extends EventEmitter {

this._subscriber = new Subscriber(this, options as SubscriberOptions);
this._subscriber.on('error', err => this.emit('error', err))
.on('message', message => this.emit('message', message));
.on('message', message => this.emit('message', message))
.on('close', () => this.emit('close'));

this._listen();
}
Expand Down
14 changes: 14 additions & 0 deletions test/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ describe('Subscriber', () => {
assert.strictEqual(stub.callCount, 1);
});

it('should emit a close event', done => {
subscriber.on('close', done);
subscriber.close();
});

describe('flushing the queues', () => {
it('should wait for any pending acks', async () => {
const ackQueue: FakeAckQueue = stubs.get('ackQueue');
Expand Down Expand Up @@ -476,6 +481,15 @@ describe('Subscriber', () => {
stream.emit('error', fakeError);
});

it('should close the subscriber if stream closes unexpectedly', () => {
const stub = sandbox.stub(subscriber, 'close');
const stream: FakeMessageStream = stubs.get('messageStream');

stream.emit('close');

assert.strictEqual(stub.callCount, 1);
});

it('should add messages to the inventory', done => {
subscriber.open();

Expand Down
5 changes: 5 additions & 0 deletions test/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ describe('Subscription', () => {

subscriber.emit('error', error);
});

it('should emit close events', done => {
subscription.on('close', done);
subscriber.emit('close');
});
});

describe('formatMetadata_', () => {
Expand Down