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

refactor: incorporate will_accept() checks into publish() #108

Merged
merged 6 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
refactor: incorporate will_accept() checks into publish()
  • Loading branch information
IlyaFaer committed May 28, 2020
commit 60b8912c9d51e71f1eaef1856dfd012f05743e4a
4 changes: 2 additions & 2 deletions google/cloud/pubsub_v1/publisher/_batch/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ def publish(self, message):
self._status != base.BatchStatus.ERROR
), "Publish after stop() or publish error."

if not self.will_accept(message):
return future
if self.status != base.BatchStatus.ACCEPTING_MESSAGES:
return

size_increase = types.PublishRequest(messages=[message]).ByteSize()

Expand Down
30 changes: 0 additions & 30 deletions tests/unit/pubsub_v1/publisher/batch/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,3 @@ def test_len():
assert len(batch) == 0
batch.publish(types.PubsubMessage(data=b"foo"))
assert len(batch) == 1


def test_will_accept():
batch = create_batch(status=BatchStatus.ACCEPTING_MESSAGES)
message = types.PubsubMessage()
assert batch.will_accept(message) is True


def test_will_accept_oversize():
batch = create_batch(
settings=types.BatchSettings(max_bytes=10),
status=BatchStatus.ACCEPTING_MESSAGES,
)
message = types.PubsubMessage(data=b"abcdefghijklmnopqrstuvwxyz")
assert batch.will_accept(message) is True


def test_will_not_accept_status():
batch = create_batch(status="talk to the hand")
message = types.PubsubMessage()
assert batch.will_accept(message) is False


def test_will_not_accept_number():
batch = create_batch(
settings=types.BatchSettings(max_messages=-1),
status=BatchStatus.ACCEPTING_MESSAGES,
)
message = types.PubsubMessage(data=b"abc")
assert batch.will_accept(message) is False
14 changes: 0 additions & 14 deletions tests/unit/pubsub_v1/publisher/batch/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,6 @@ def test_publish_updating_batch_size():
assert batch.size > 0 # I do not always trust protobuf.


def test_publish_not_will_accept():
batch = create_batch(topic="topic_foo", max_messages=0)
base_request_size = types.PublishRequest(topic="topic_foo").ByteSize()

# Publish the message.
message = types.PubsubMessage(data=b"foobarbaz")
future = batch.publish(message)

assert future is None
assert batch.size == base_request_size
assert batch.messages == []
assert batch._futures == []


def test_publish_exceed_max_messages():
max_messages = 4
batch = create_batch(max_messages=max_messages)
Expand Down
3 changes: 0 additions & 3 deletions tests/unit/pubsub_v1/publisher/test_publisher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ def test_publish():
# Use a mock in lieu of the actual batch class.
batch = mock.Mock(spec=client._batch_class)
# Set the mock up to claim indiscriminately that it accepts all messages.
batch.will_accept.return_value = True
batch.publish.side_effect = (mock.sentinel.future1, mock.sentinel.future2)

topic = "topic/path"
Expand Down Expand Up @@ -185,7 +184,6 @@ def test_publish_attrs_bytestring():
# Use a mock in lieu of the actual batch class.
batch = mock.Mock(spec=client._batch_class)
# Set the mock up to claim indiscriminately that it accepts all messages.
batch.will_accept.return_value = True

topic = "topic/path"
client._set_batch(topic, batch)
Expand Down Expand Up @@ -391,7 +389,6 @@ def test_publish_with_ordering_key():
# Use a mock in lieu of the actual batch class.
batch = mock.Mock(spec=client._batch_class)
# Set the mock up to claim indiscriminately that it accepts all messages.
batch.will_accept.return_value = True
batch.publish.side_effect = (mock.sentinel.future1, mock.sentinel.future2)

topic = "topic/path"
Expand Down