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: return singleton success future for exactly-once methods in Message #608

Merged
merged 3 commits into from
Mar 9, 2022
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
24 changes: 15 additions & 9 deletions google/cloud/pubsub_v1/subscriber/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
attributes: {}
}}"""

_SUCCESS_FUTURE = futures.Future()
_SUCCESS_FUTURE.set_result(AcknowledgeStatus.SUCCESS)


def _indent(lines: str, prefix: str = " ") -> str:
"""Indent some text.
Expand Down Expand Up @@ -291,12 +294,13 @@ def ack_with_response(self) -> "futures.Future":
pubsub_v1.subscriber.exceptions.AcknowledgeError exception
will be thrown.
"""
future = futures.Future()
req_future = None
req_future: Optional[futures.Future]
if self._exactly_once_delivery_enabled_func():
future = futures.Future()
req_future = future
else:
future.set_result(AcknowledgeStatus.SUCCESS)
future = _SUCCESS_FUTURE
req_future = None
time_to_ack = math.ceil(time.time() - self._received_timestamp)
self._request_queue.put(
requests.AckRequest(
Expand Down Expand Up @@ -390,12 +394,13 @@ def modify_ack_deadline_with_response(self, seconds: int) -> "futures.Future":
will be thrown.

"""
future = futures.Future()
req_future = None
req_future: Optional[futures.Future]
if self._exactly_once_delivery_enabled_func():
future = futures.Future()
req_future = future
else:
future.set_result(AcknowledgeStatus.SUCCESS)
future = _SUCCESS_FUTURE
req_future = None

self._request_queue.put(
requests.ModAckRequest(
Expand Down Expand Up @@ -451,12 +456,13 @@ def nack_with_response(self) -> "futures.Future":
will be thrown.

"""
future = futures.Future()
req_future = None
req_future: Optional[futures.Future]
if self._exactly_once_delivery_enabled_func():
future = futures.Future()
req_future = future
else:
future.set_result(AcknowledgeStatus.SUCCESS)
future = _SUCCESS_FUTURE
req_future = None

self._request_queue.put(
requests.NackRequest(
Expand Down
3 changes: 3 additions & 0 deletions tests/unit/pubsub_v1/subscriber/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ def test_ack_with_response_exactly_once_delivery_disabled():
)
)
assert future.result() == AcknowledgeStatus.SUCCESS
assert future == message._SUCCESS_FUTURE
check_call_types(put, requests.AckRequest)


Expand Down Expand Up @@ -205,6 +206,7 @@ def test_modify_ack_deadline_with_response_exactly_once_delivery_disabled():
requests.ModAckRequest(ack_id="bogus_ack_id", seconds=60, future=None)
)
assert future.result() == AcknowledgeStatus.SUCCESS
assert future == message._SUCCESS_FUTURE
check_call_types(put, requests.ModAckRequest)


Expand Down Expand Up @@ -242,6 +244,7 @@ def test_nack_with_response_exactly_once_delivery_disabled():
)
)
assert future.result() == AcknowledgeStatus.SUCCESS
assert future == message._SUCCESS_FUTURE
check_call_types(put, requests.NackRequest)


Expand Down