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

Resolve subscription.close() once streams have been flushed. #50

Closed
STeveShary opened this issue Jan 25, 2018 · 11 comments · Fixed by #92
Closed

Resolve subscription.close() once streams have been flushed. #50

STeveShary opened this issue Jan 25, 2018 · 11 comments · Fixed by #92
Assignees
Labels
api: pubsub Issues related to the googleapis/nodejs-pubsub API. priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. triaged for GA type: question Request for information or clarification. Not an issue.

Comments

@STeveShary
Copy link

After using the pub/sub client for test, I have found that I wanted to know when the queue was flushed before exiting. The comment in the code states that a promise is returned when no callback is provided: https://github.com/googleapis/nodejs-pubsub/blob/master/src/subscription.js#L377 but the code does not return a promise.

Environment details

  • OS: OSX
  • Node.js version: 8.6.0
  • npm version: 5.3.0
  • @google-cloud/pubsub version: 0.16.2

Steps to reproduce

  1. Open a subscription
  2. Close a subscription with no callback.
  3. Execute .then() on the promise from the subscription close method.

Expected: the callback in the .then() is executed.
Actual: Error calling .then on undefined.

I have completed a fix with a test here: STeveShary@2bf9c33 . Your contribution guide doesn't recommend a PR yet, but I can create one quickly.

@callmehiphop
Copy link
Contributor

@STeveShary thanks for reporting! I'm sorry you're running into this issue. Generally we do not return promises directly in any of our methods. Instead we use a decorator to wrap them.

I attempted to reproduce the error you were seeing, however in my local testing close() returned a promise for me. If you wouldn't mind putting together a minimal example of how to reproduce this error, I think that would help try and resolve this.

@callmehiphop callmehiphop added priority: p2 Moderately-important priority. Fix may not be included in next release. type: question Request for information or clarification. Not an issue. labels Jan 29, 2018
@STeveShary
Copy link
Author

@callmehiphop Thanks for responding. My commit from above: STeveShary@2bf9c33 has a really simple test as part of it. I found that I had to add the code fix to get the test to pass.

@STeveShary
Copy link
Author

I threw a PR over to help with that.

@callmehiphop
Copy link
Contributor

@STeveShary we stub the decorators in our unit tests, so promises will not work there unless we return the promise directly, which I think we want to avoid so we can stay uniform with the rest of the code.

@callmehiphop
Copy link
Contributor

I created the following e2e test in /system-test/pubsub.js

it.only('should return a promise', function() {
  var subscription = topic.subscription(generateSubName(), {
    maxConnections: 1,
  });

  subscription
    .on('error', err => console.error(err))
    .on('message', message => {});

  return subscription.close().then(function() {
    console.log('it works!');
  });
});

And it passes/logs as expected. By any chance have you been exclusively testing in the unit test files? That would have the potential for a false positive.

@STeveShary
Copy link
Author

Hmmm... I know we ran into this problem initially when we were trying to close and make sure the queue was flushed before exiting. Let me look...

@STeveShary
Copy link
Author

Ok. Jogged the memory. The issue that we ran into is that we found that our acks were not being sent back. This we found was because there is no command that will return a promise that tells us when the queue for acks has been drained. We did see in the close() method:

  this.flushQueues_().then(function() {
    self.closeConnection_(callback);
  });

This would be GREAT! But, alas it does not return that promise and therefore we can't know when the queue is clear other than setTimeout(..., 1010); (which is not optimal)

For our tests, we want to send acks for all subscriptions but our tests almost everytime exit before the acks are flushed from the queue. Making close() return a promise allows us to know that exactly from the its fulfillment.

@callmehiphop
Copy link
Contributor

callmehiphop commented Jan 29, 2018

Ah, I see. In most cases that queue isn't actually used. Its primary function is to act as a backup in case we're unable to make a streaming connection. I'd need to investigate a bit more, but it might be possible to implement a flush (or similar) event to listen to.

@stephenplusplus
Copy link
Contributor

@callmehiphop do we need these changes: #52?

@callmehiphop
Copy link
Contributor

callmehiphop commented Feb 10, 2018 via email

@callmehiphop callmehiphop changed the title Subscription.close() does not return a promise Resolve subscription.close() once streams have been flushed. Feb 22, 2018
@callmehiphop
Copy link
Contributor

@STeveShary we've got a fix prepared for this (#92) if you have the bandwidth to test it out that would be fantastic! Otherwise we should have it released within the next day or two.

@callmehiphop callmehiphop mentioned this issue Mar 19, 2018
stephenplusplus pushed a commit to stephenplusplus/nodejs-pubsub that referenced this issue Aug 31, 2018
* Fixed the failing unit test.

* Removed the unreliable assertion
@google-cloud-label-sync google-cloud-label-sync bot added the api: pubsub Issues related to the googleapis/nodejs-pubsub API. label Jan 31, 2020
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the googleapis/nodejs-pubsub API. priority: p2 Moderately-important priority. Fix may not be included in next release. 🚨 This issue needs some love. triaged for GA type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants