From 80e79b0e93c719bf0dafd979fb4e978d879ff868 Mon Sep 17 00:00:00 2001 From: Philip Jenvey Date: Fri, 2 Mar 2018 15:43:15 -0800 Subject: [PATCH] refactor: rearrange/hack back to 100% coverage Closes #1143 --- autopush/main.py | 33 ++++++++++++--------------- autopush/memusage.py | 4 ++-- autopush/ssl.py | 2 +- autopush/tests/test_db.py | 28 +++++++++++++---------- autopush/tests/test_rs_integration.py | 14 ++++-------- 5 files changed, 39 insertions(+), 42 deletions(-) diff --git a/autopush/main.py b/autopush/main.py index e4a457ab..42aec6b3 100644 --- a/autopush/main.py +++ b/autopush/main.py @@ -323,26 +323,21 @@ class RustConnectionApplication(AutopushMultiService): logger_name = "AutopushRust" push_server = None - def __init__(self, conf, resource=None): - # type: (AutopushConfig, DynamoDBResource) -> None - super(RustConnectionApplication, self).__init__( - conf, - resource=resource - ) - - def setup(self, rotate_tables=True): + def setup(self, rotate_tables=True, num_threads=10): super(RustConnectionApplication, self).setup(rotate_tables) self.db.setup(self.conf.preflight_uaid) - if self.conf.memusage_port: - self.add_memusage() - - self.push_server = WebPushServer(self.conf, self.db, num_threads=10) + # No add_memusage: requires twisted + self.push_server = WebPushServer( + self.conf, + self.db, + num_threads=num_threads + ) - def run(self): + def run(self): # pragma: nocover try: - self.push_server.start() + self.startService() while True: try: # handle a graceful shutdown on SIGINT w/ a busy @@ -352,14 +347,16 @@ def run(self): except KeyboardInterrupt: return 1 finally: - self.push_server.stop() + self.stopService() + + def startService(self): + self.push_server.start() - @inlineCallbacks def stopService(self): - yield super(RustConnectionApplication, self).stopService() + self.push_server.stop() @classmethod - def from_argparse(cls, ns, resource=None): + def from_argparse(cls, ns, resource=None): # pragma: nocover # type: (Namespace, DynamoDBResource) -> AutopushMultiService return super(RustConnectionApplication, cls)._from_argparse( ns, diff --git a/autopush/memusage.py b/autopush/memusage.py index c7f29947..d8ad0e3d 100644 --- a/autopush/memusage.py +++ b/autopush/memusage.py @@ -115,7 +115,7 @@ def glibc_malloc_info(stream): stream.writelines(fp.readlines()) -def jemalloc_stats(stream): +def jemalloc_stats(stream): # pragma: nocover """Write jemalloc's malloc_stats_print()""" try: malloc_stats_print = lib.malloc_stats_print @@ -127,7 +127,7 @@ def jemalloc_stats(stream): @ffi.callback("void (*write_cb) (void *, const char *)") -def _jemalloc_write_cb(handle, msg): +def _jemalloc_write_cb(handle, msg): # pragma: nocover """Callback for jemalloc's malloc_stats_print Writes to a Python stream passed via the cbopaque pointer diff --git a/autopush/ssl.py b/autopush/ssl.py index 604a4a81..a1663f0f 100644 --- a/autopush/ssl.py +++ b/autopush/ssl.py @@ -15,7 +15,7 @@ try: SSL_PROTO = ssl.PROTOCOL_TLS -except AttributeError: +except AttributeError: # pragma: nocover SSL_PROTO = ssl.PROTOCOL_SSLv23 diff --git a/autopush/tests/test_db.py b/autopush/tests/test_db.py index 6dc65cc6..e35b1ad8 100644 --- a/autopush/tests/test_db.py +++ b/autopush/tests/test_db.py @@ -89,12 +89,14 @@ class DdbResourceTest(unittest.TestCase): @patch("boto3.resource") def test_ddb_no_endpoint(self, mresource): safe = os.getenv("AWS_LOCAL_DYNAMODB") - os.unsetenv("AWS_LOCAL_DYANMODB") - del(os.environ["AWS_LOCAL_DYNAMODB"]) - DynamoDBResource(region_name="us-east-1") - assert mresource.call_args[0] == ('dynamodb',) - if safe: - os.environ["AWS_LOCAL_DYNAMODB"] = safe + try: + os.unsetenv("AWS_LOCAL_DYANMODB") + del(os.environ["AWS_LOCAL_DYNAMODB"]) + DynamoDBResource(region_name="us-east-1") + assert mresource.call_args[0] == ('dynamodb',) + finally: + if safe: # pragma: nocover + os.environ["AWS_LOCAL_DYNAMODB"] = safe def test_ddb_env(self): ddb_session_args = dict( @@ -103,12 +105,14 @@ def test_ddb_env(self): aws_secret_access_key="BogusKey", ) safe = os.getenv("AWS_DEFAULT_REGION") - os.environ["AWS_DEFAULT_REGION"] = "us-west-2" - boto_resource = DynamoDBResource(**ddb_session_args) - assert boto_resource._resource.meta.client.meta.region_name == \ - 'us-west-2' - if safe: - os.environ["AWS_DEFAULT_REGION"] = safe + try: + os.environ["AWS_DEFAULT_REGION"] = "us-west-2" + boto_resource = DynamoDBResource(**ddb_session_args) + assert boto_resource._resource.meta.client.meta.region_name == \ + 'us-west-2' + finally: + if safe: # pragma: nocover + os.environ["AWS_DEFAULT_REGION"] = safe class DbCheckTestCase(unittest.TestCase): diff --git a/autopush/tests/test_rs_integration.py b/autopush/tests/test_rs_integration.py index b8acdb09..c1342b1e 100644 --- a/autopush/tests/test_rs_integration.py +++ b/autopush/tests/test_rs_integration.py @@ -27,11 +27,8 @@ import autopush.db as db import autopush.tests from autopush.config import AutopushConfig -from autopush.db import ( - DatabaseManager, -) from autopush.logging import begin_or_register -from autopush.main import EndpointApplication +from autopush.main import EndpointApplication, RustConnectionApplication from autopush.utils import base64url_encode from autopush.metrics import SinkMetrics from autopush.tests.support import TestingLogObserver @@ -39,7 +36,6 @@ Client, _get_vapid, ) -from autopush.webpush_server import WebPushServer log = logging.getLogger(__name__) @@ -113,13 +109,13 @@ def setUp(self): self.addCleanup(ep.stopService) # Websocket server - db = DatabaseManager.from_config( + self.conn = conn = RustConnectionApplication( conn_conf, resource=autopush.tests.boto_resource ) - self.conn = WebPushServer(conn_conf, db, num_threads=2) - self.conn.start() - self.addCleanup(self.conn.stop) + conn.setup(rotate_tables=False, num_threads=2) + conn.startService() + self.addCleanup(conn.stopService) def endpoint_kwargs(self): return self._endpoint_defaults