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

Commit

Permalink
feat: log all unregister/register calls.
Browse files Browse the repository at this point in the history
also adds assert_called_included for checking calls including kwargs

closes #525
  • Loading branch information
pjenvey committed Jul 22, 2016
1 parent eb7ccfe commit cb87a82
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
51 changes: 34 additions & 17 deletions autopush/tests/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ def tearDown():
tearDown()


def assert_called_included(mock, **kwargs):
"""Like assert_called_with but asserts a call was made including
the specified kwargs (but allowing additional args/kwargs)"""
mock.assert_called()
_, mock_kwargs = mock.call_args
for name, val in kwargs.iteritems():
if name not in mock_kwargs or mock_kwargs[name] != val:
raise AssertionError("%s not called with keyword arg %s=%s" %
(mock, name, val))


class WebsocketTestCase(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -929,6 +940,7 @@ def check_register_result(msg):
eq_(msg["status"], 200)
eq_(msg["messageType"], "register")
assert "pushEndpoint" in msg
assert_called_included(self.proto.log.info, format="Register")
d.callback(True)

def check_hello_result(msg):
Expand All @@ -952,6 +964,7 @@ def test_register_webpush(self):

def check_register_result(msg):
assert self.proto.ap_settings.message.register_channel.called
assert_called_included(self.proto.log.info, format="Register")
d.callback(True)

res = self.proto.process_register(dict(channelID=chid))
Expand Down Expand Up @@ -983,6 +996,7 @@ def check_register_result(msg, test_endpoint):
eq_(test_endpoint,
self.proto.sendJSON.call_args[0][0]['pushEndpoint'])
assert self.proto.ap_settings.message.register_channel.called
assert_called_included(self.proto.log.info, format="Register")
d.callback(True)

res = self.proto.process_register(
Expand Down Expand Up @@ -1096,6 +1110,7 @@ def throw_error(*args, **kwargs):
def check_register_result(msg):
eq_(msg["status"], 500)
eq_(msg["messageType"], "register")
self.proto.log.failure.assert_called()
d.callback(True)

f = self._check_response(check_register_result)
Expand Down Expand Up @@ -1191,6 +1206,7 @@ def check_unregister_result(msg):
eq_(msg["status"], 200)
eq_(msg["channelID"], chid)
eq_(len(self.proto.log.mock_calls), 1)
assert_called_included(self.proto.log.info, format="Unregister")
d.callback(True)

def check_hello_result(msg):
Expand Down Expand Up @@ -1260,11 +1276,13 @@ def raise_exception(*args, **kwargs):
channelID=chid))

def wait_for_times(): # pragma: nocover
if len(self.proto.log.failure.mock_calls) > 0:
eq_(len(self.proto.log.failure.mock_calls), 1)
if not self.proto.log.failure.called:
reactor.callLater(0.1, wait_for_times)
else:
self.proto.log.failure.assert_called_once()
assert_called_included(self.proto.log.info,
format="Unregister")
d.callback(True)
return
reactor.callLater(0.1, wait_for_times)

reactor.callLater(0.1, wait_for_times)
return d
Expand Down Expand Up @@ -1348,11 +1366,10 @@ def check_hello_result(msg):
# Verify it was cleared out
eq_(len(self.proto.ps.direct_updates), 0)
eq_(len(self.proto.log.info.mock_calls), 1)
args, kwargs = self.proto.log.info.call_args
eq_(kwargs.get('format') or args[0], "Ack")
eq_(kwargs["router_key"], "simplepush")
eq_(kwargs["message_source"], "direct")

assert_called_included(self.proto.log.info,
format="Ack",
router_key="simplepush",
message_source="direct")
d.callback(True)

f = self._check_response(check_hello_result)
Expand Down Expand Up @@ -1380,10 +1397,10 @@ def test_ack_with_webpush_direct(self):
))
eq_(self.proto.ps.direct_updates[chid], [])
eq_(len(self.proto.log.info.mock_calls), 1)
args, kwargs = self.proto.log.info.call_args
eq_(kwargs.get('format') or args[0], "Ack")
eq_(kwargs["router_key"], "webpush")
eq_(kwargs["message_source"], "direct")
assert_called_included(self.proto.log.info,
format="Ack",
router_key="webpush",
message_source="direct")

def test_ack_with_webpush_from_storage(self):
self._connect()
Expand All @@ -1405,10 +1422,10 @@ def test_ack_with_webpush_from_storage(self):
assert self.proto.force_retry.called
assert mock_defer.addBoth.called
eq_(len(self.proto.log.info.mock_calls), 1)
args, kwargs = self.proto.log.info.call_args
eq_(kwargs.get('format') or args[0], "Ack")
eq_(kwargs["router_key"], "webpush")
eq_(kwargs["message_source"], "stored")
assert_called_included(self.proto.log.info,
format="Ack",
router_key="webpush",
message_source="stored")

def test_nack(self):
self._connect()
Expand Down
15 changes: 10 additions & 5 deletions autopush/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ def error_register(self, fail):
self.transport.resumeProducing()
msg = {"messageType": "register", "status": 500}
self.sendJSON(msg)
self.log_failure(fail, extra="Failed to register")

def finish_register(self, endpoint, chid):
"""callback for successful endpoint creation, sends register reply"""
Expand All @@ -1034,6 +1035,10 @@ def send_register_finish(self, result, endpoint, chid):
self.sendJSON(msg)
self.ps.metrics.increment("updates.client.register",
tags=self.base_tags)
self.log.info(format="Register", channelID=chid,
endpoint=endpoint,
uaid_hash=self.ps.uaid_hash,
user_agent=self.ps.user_agent)

def process_unregister(self, data):
"""Process an unregister message"""
Expand All @@ -1048,12 +1053,12 @@ def process_unregister(self, data):
self.ps.metrics.increment("updates.client.unregister",
tags=self.base_tags)

# Log out the unregister if it has a code in it
event = dict(format="Unregister", channelID=chid,
uaid_hash=self.ps.uaid_hash,
user_agent=self.ps.user_agent)
if "code" in data:
code = extract_code(data)
self.log.info(format="Unregister", channelID=chid,
uaid_hash=self.ps.uaid_hash,
user_agent=self.ps.user_agent, code=code)
event["code"] = extract_code(data)
self.log.info(**event)

# Clear out any existing tracked messages for this channel
if self.ps.use_webpush:
Expand Down

0 comments on commit cb87a82

Please sign in to comment.