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

Pubsub: Separate subscriber and publish future documentation. #8205

Merged
merged 3 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 10 additions & 13 deletions pubsub/google/cloud/pubsub_v1/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,12 @@ def done(self):
return self._exception != self._SENTINEL or self._result != self._SENTINEL

def result(self, timeout=None):
"""Return the message ID, or raise an exception.

This blocks until the message has successfully been published, and
returns the message ID.
"""Resolve the future and return a value where appropriate.

Args:
timeout (Union[int, float]): The number of seconds before this call
times out and raises TimeoutError.

Returns:
str: The message ID.

Raises:
~.pubsub_v1.TimeoutError: If the request times out.
Exception: For undefined exceptions in the underlying
Expand All @@ -115,9 +109,6 @@ def result(self, timeout=None):
def exception(self, timeout=None):
"""Return the exception raised by the call, if any.

This blocks until the message has successfully been published, and
returns the exception. If the call succeeded, return None.

Args:
timeout (Union[int, float]): The number of seconds before this call
times out and raises TimeoutError.
Expand All @@ -139,15 +130,21 @@ def exception(self, timeout=None):
# Okay, this batch had an error; this should return it.
return self._exception

def add_done_callback(self, fn):
def add_done_callback(self, callback):
"""Attach the provided callable to the future.

The provided function is called, with this future as its only argument,
when the future finishes running.

Args:
callback (Callable): The function to call.

Returns:
None
"""
if self.done():
return fn(self)
self._callbacks.append(fn)
return callback(self)
self._callbacks.append(callback)

def set_result(self, result):
"""Set the result of the future to the provided result.
Expand Down
35 changes: 25 additions & 10 deletions pubsub/google/cloud/pubsub_v1/publisher/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,28 @@ class Future(futures.Future):
:class:`threading.Event` will be created and used.
"""

# The publishing-side subclass does not need any special behavior
# at this time.
#
# However, there is still a subclass so that if someone attempts
# isinstance checks against a publisher-returned or subscriber-returned
# future, trying either one against the other returns False.
pass


__all__ = ("Future",)
def result(self, timeout=None):
"""Return the message ID or raise an exception.

This blocks until the message has been published successfully and
returns the message ID unless an exception is raised.

Args:
timeout (Union[int, float]): The number of seconds before this call
times out and raises TimeoutError.

Returns:
str: The message ID.

Raises:
~.pubsub_v1.TimeoutError: If the request times out.
Exception: For undefined exceptions in the underlying
call execution.
"""
# Attempt to get the exception if there is one.
# If there is not one, then we know everything worked, and we can
# return an appropriate value.
err = self.exception(timeout=timeout)
if err is None:
return self._result
raise err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new method needs unit tests (one for the error case, one for the normal one).

5 changes: 4 additions & 1 deletion pubsub/google/cloud/pubsub_v1/subscriber/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,8 @@ def cancel(self):
return self._manager.close()

def cancelled(self):
"""bool: True if the subscription has been cancelled."""
"""
returns:
bool: ``True`` if the subscription has been cancelled.
"""
return self._cancelled