Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
refactor: rearrange/hack back to 100% coverage
Browse files Browse the repository at this point in the history
Closes #1143
  • Loading branch information
pjenvey committed Mar 3, 2018
1 parent 36ad90f commit 80e79b0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 42 deletions.
33 changes: 15 additions & 18 deletions autopush/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions autopush/memusage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion autopush/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

try:
SSL_PROTO = ssl.PROTOCOL_TLS
except AttributeError:
except AttributeError: # pragma: nocover
SSL_PROTO = ssl.PROTOCOL_SSLv23


Expand Down
28 changes: 16 additions & 12 deletions autopush/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand Down
14 changes: 5 additions & 9 deletions autopush/tests/test_rs_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@
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
from autopush.tests.test_integration import (
Client,
_get_vapid,
)
from autopush.webpush_server import WebPushServer

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 80e79b0

Please sign in to comment.