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

chore: bump test coverage to 100% #364

Merged
merged 7 commits into from
Apr 9, 2021
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
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ branch = True
fail_under = 100
show_missing = True
omit =
google/cloud/__init__.py
google/pubsub/__init__.py
exclude_lines =
# Re-enable the standard pragma
Expand Down
14 changes: 7 additions & 7 deletions google/cloud/pubsub_v1/publisher/_batch/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __len__(self):

@staticmethod
@abc.abstractmethod
def make_lock():
def make_lock(): # pragma: NO COVER
"""Return a lock in the chosen concurrency model.

Returns:
Expand All @@ -60,7 +60,7 @@ def make_lock():

@property
@abc.abstractmethod
def messages(self):
def messages(self): # pragma: NO COVER
"""Return the messages currently in the batch.

Returns:
Expand All @@ -70,7 +70,7 @@ def messages(self):

@property
@abc.abstractmethod
def size(self):
def size(self): # pragma: NO COVER
"""Return the total size of all of the messages currently in the batch.

The size includes any overhead of the actual ``PublishRequest`` that is
Expand All @@ -84,7 +84,7 @@ def size(self):

@property
@abc.abstractmethod
def settings(self):
def settings(self): # pragma: NO COVER
"""Return the batch settings.

Returns:
Expand All @@ -95,7 +95,7 @@ def settings(self):

@property
@abc.abstractmethod
def status(self):
def status(self): # pragma: NO COVER
"""Return the status of this batch.

Returns:
Expand All @@ -106,7 +106,7 @@ def status(self):
"""
raise NotImplementedError

def cancel(self, cancellation_reason):
def cancel(self, cancellation_reason): # pragma: NO COVER
"""Complete pending futures with an exception.

This method must be called before publishing starts (ie: while the
Expand All @@ -119,7 +119,7 @@ def cancel(self, cancellation_reason):
raise NotImplementedError

@abc.abstractmethod
def publish(self, message):
def publish(self, message): # pragma: NO COVER
"""Publish a single message.

Add the given message to this object; this will cause it to be
Expand Down
6 changes: 3 additions & 3 deletions google/cloud/pubsub_v1/publisher/_sequencer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Sequencer(metaclass=abc.ABCMeta):

@staticmethod
@abc.abstractmethod
def is_finished(self):
def is_finished(self): # pragma: NO COVER
""" Whether the sequencer is finished and should be cleaned up.

Returns:
Expand All @@ -34,7 +34,7 @@ def is_finished(self):

@staticmethod
@abc.abstractmethod
def unpause(self, message):
def unpause(self, message): # pragma: NO COVER
""" Unpauses this sequencer.

Raises:
Expand All @@ -45,7 +45,7 @@ def unpause(self, message):

@staticmethod
@abc.abstractmethod
def publish(self, message, retry=None):
def publish(self, message, retry=None): # pragma: NO COVER
""" Publish message for this ordering key.

Args:
Expand Down
8 changes: 0 additions & 8 deletions google/cloud/pubsub_v1/publisher/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@
_raw_proto_pubbsub_message = gapic_types.PubsubMessage.pb()


def _set_nested_value(container, value, keys):
current = container
for key in keys[:-1]:
current = current.setdefault(key, {})
current[keys[-1]] = value
return container


@_gapic.add_methods(publisher_client.PublisherClient, blacklist=_BLACKLISTED_METHODS)
class Client(object):
"""A publisher client for Google Cloud Pub/Sub.
Expand Down
9 changes: 2 additions & 7 deletions google/cloud/pubsub_v1/publisher/flow_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def _would_overflow(self, message):

return size_overflow or msg_count_overflow

def _load_info(self, message_count=None, total_bytes=None, reserved_bytes=None):
def _load_info(self, message_count=None, total_bytes=None):
"""Return the current flow control load information.

The caller can optionally adjust some of the values to fit its reporting
Expand All @@ -271,8 +271,6 @@ def _load_info(self, message_count=None, total_bytes=None, reserved_bytes=None):
The value to override the current message count with.
total_bytes (Optional[int]):
The value to override the current total bytes with.
reserved_bytes (Optional[int]):
The value to override the current number of reserved bytes with.

Returns:
str
Expand All @@ -285,13 +283,10 @@ def _load_info(self, message_count=None, total_bytes=None, reserved_bytes=None):
if total_bytes is None:
total_bytes = self._total_bytes

if reserved_bytes is None:
reserved_bytes = self._reserved_bytes

return msg.format(
message_count,
self._settings.message_limit,
total_bytes,
self._settings.byte_limit,
reserved_bytes,
self._reserved_bytes,
)
6 changes: 3 additions & 3 deletions google/cloud/pubsub_v1/subscriber/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Scheduler(metaclass=abc.ABCMeta):

@property
@abc.abstractmethod
def queue(self):
def queue(self): # pragma: NO COVER
"""Queue: A concurrency-safe queue specific to the underlying
concurrency implementation.

Expand All @@ -40,7 +40,7 @@ def queue(self):
raise NotImplementedError

@abc.abstractmethod
def schedule(self, callback, *args, **kwargs):
def schedule(self, callback, *args, **kwargs): # pragma: NO COVER
"""Schedule the callback to be called asynchronously.

Args:
Expand All @@ -54,7 +54,7 @@ def schedule(self, callback, *args, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def shutdown(self, await_msg_callbacks=False):
def shutdown(self, await_msg_callbacks=False): # pragma: NO COVER
"""Shuts down the scheduler and immediately end all pending callbacks.

Args:
Expand Down
54 changes: 52 additions & 2 deletions synth.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
)

# Emit deprecation warning if return_immediately flag is set with synchronous pull.
count = s.replace(
s.replace(
"google/pubsub_v1/services/subscriber/*client.py",
r"import pkg_resources",
"import warnings\n\g<0>",
Expand Down Expand Up @@ -170,6 +170,48 @@
if count != 2:
raise Exception("Too many or too few replacements in pull() methods.")

# Silence deprecation warnings in pull() method flattened parameter tests.
s.replace(
"tests/unit/gapic/pubsub_v1/test_subscriber.py",
"import mock",
"\g<0>\nimport warnings",
)
count = s.replace(
"tests/unit/gapic/pubsub_v1/test_subscriber.py",
textwrap.dedent(
r"""
([^\n\S]+# Call the method with a truthy value for each flattened field,
[^\n\S]+# using the keyword arguments to the method\.)
\s+(client\.pull\(.*?\))"""
),
"""\n\g<1>
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
\g<2>""",
flags = re.MULTILINE | re.DOTALL,
)

if count < 1:
raise Exception("Catch warnings replacement failed.")

count = s.replace(
"tests/unit/gapic/pubsub_v1/test_subscriber.py",
textwrap.dedent(
r"""
([^\n\S]+# Call the method with a truthy value for each flattened field,
[^\n\S]+# using the keyword arguments to the method\.)
\s+response = (await client\.pull\(.*?\))"""
),
"""\n\g<1>
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
\g<2>""",
flags = re.MULTILINE | re.DOTALL,
)

plamut marked this conversation as resolved.
Show resolved Hide resolved
if count < 1:
raise Exception("Catch warnings replacement failed.")

# Make sure that client library version is present in user agent header.
s.replace(
[
Expand Down Expand Up @@ -206,13 +248,21 @@
"\n\g<0>",
)

# The namespace package declaration in google/cloud/__init__.py should be excluded
# from coverage.
s.replace(
".coveragerc",
r"((?P<indent>[^\n\S]+)google/pubsub/__init__\.py)",
"\g<indent>google/cloud/__init__.py\n\g<0>",
)

# ----------------------------------------------------------------------------
# Add templated files
# ----------------------------------------------------------------------------
templated_files = gcp.CommonTemplates().py_library(
microgenerator=True,
samples=True,
cov_level=99,
cov_level=100,
system_test_external_dependencies=["psutil"],
)
s.move(templated_files, excludes=[".coveragerc"])
Expand Down
25 changes: 15 additions & 10 deletions tests/unit/gapic/pubsub_v1/test_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import os
import mock
import warnings

import grpc
from grpc.experimental import aio
Expand Down Expand Up @@ -2338,11 +2339,13 @@ def test_pull_flattened():

# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
client.pull(
subscription="subscription_value",
return_immediately=True,
max_messages=1277,
)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
client.pull(
subscription="subscription_value",
return_immediately=True,
max_messages=1277,
)

# Establish that the underlying call was made with the expected
# request object values.
Expand Down Expand Up @@ -2382,11 +2385,13 @@ async def test_pull_flattened_async():
call.return_value = grpc_helpers_async.FakeUnaryUnaryCall(pubsub.PullResponse())
# Call the method with a truthy value for each flattened field,
# using the keyword arguments to the method.
response = await client.pull(
subscription="subscription_value",
return_immediately=True,
max_messages=1277,
)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=DeprecationWarning)
await client.pull(
subscription="subscription_value",
return_immediately=True,
max_messages=1277,
)

# Establish that the underlying call was made with the expected
# request object values.
Expand Down
8 changes: 3 additions & 5 deletions tests/unit/pubsub_v1/publisher/batch/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@
from google.pubsub_v1 import types as gapic_types


def create_batch(status=None, settings=types.BatchSettings()):
def create_batch(status, settings=types.BatchSettings()):
"""Create a batch object, which does not commit.

Args:
status (str): If provided, the batch's internal status will be set
to the provided status.
status (str): The batch's internal status will be set to the provided status.

Returns:
~.pubsub_v1.publisher.batch.thread.Batch: The batch object
"""
creds = mock.Mock(spec=credentials.Credentials)
client = publisher.Client(credentials=creds)
batch = Batch(client, "topic_name", settings)
if status:
batch._status = status
batch._status = status
return batch


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/pubsub_v1/publisher/batch/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def api_publish_delay(topic="", messages=(), retry=None):

start = datetime.datetime.now()
event_set = api_publish_called.wait(timeout=1.0)
if not event_set:
if not event_set: # pragma: NO COVER
pytest.fail("API publish was not called in time")
batch.publish({"data": b"second message"})
end = datetime.datetime.now()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from google.auth import credentials
from google.cloud.pubsub_v1 import publisher
from google.cloud.pubsub_v1 import types
from google.cloud.pubsub_v1.publisher._batch import base
from google.cloud.pubsub_v1.publisher._sequencer import unordered_sequencer
from google.pubsub_v1 import types as gapic_types
Expand Down Expand Up @@ -119,13 +120,19 @@ def test_publish_batch_full():
def test_publish_after_batch_error():
client = create_client()
message = create_message()
batch = mock.Mock(spec=client._batch_class)

batch = client._batch_class(
client, "topic_name", types.BatchSettings(max_latency=float("inf"))
)
batch._messages.append(mock.Mock(name="message")) # Make batch truthy (non-empty).

sequencer = unordered_sequencer.UnorderedSequencer(client, "topic_name")
sequencer._set_batch(batch)

sequencer.commit()
batch.commit.assert_called_once()
with mock.patch.object(batch, "commit") as fake_batch_commit:
sequencer.commit()

fake_batch_commit.assert_called_once()

# Simulate publish RPC failing.
batch._set_status(base.BatchStatus.ERROR)
Expand Down
Loading