Actually test callbacks in GetUpdatesTest.

The tests in GetUpdatesTest had some callback logic that has
been dead code for at least three months.  We now fully exercise
the callback codepath and make sure that the callbacks do happen.

(imported from commit f5d8fbab28ecc34dc81d3d0c29058b66c10f378f)
This commit is contained in:
Steve Howell
2013-07-01 18:11:59 -04:00
parent 3f047ec703
commit a4db7de330
3 changed files with 30 additions and 8 deletions

View File

@@ -297,7 +297,7 @@ def do_send_messages(messages):
cache_save_message(message['message']) cache_save_message(message['message'])
# We can only publish messages to longpolling clients if the Tornado server is running. # We can only publish messages to longpolling clients if the Tornado server is running.
if settings.TORNADO_SERVER: if True:
for message in messages: for message in messages:
# Render Markdown etc. here and store (automatically) in # Render Markdown etc. here and store (automatically) in
# memcached, so that the single-threaded Tornado server # memcached, so that the single-threaded Tornado server

View File

@@ -1555,10 +1555,22 @@ class S3Test(AuthedTestCase):
self.test_uris.remove(uri) self.test_uris.remove(uri)
class DummyStream:
def closed(self):
return False
class DummyObject:
pass
class DummyTornadoRequest:
def __init__(self):
self.connection = DummyObject()
self.connection.stream = DummyStream()
class DummyHandler(object): class DummyHandler(object):
def __init__(self, assert_callback): def __init__(self, assert_callback):
self.assert_callback = assert_callback self.assert_callback = assert_callback
self.request = DummyTornadoRequest()
# Mocks RequestHandler.async_callback, which wraps a callback to # Mocks RequestHandler.async_callback, which wraps a callback to
# handle exceptions. We return the callback as-is. # handle exceptions. We return the callback as-is.
@@ -1568,10 +1580,11 @@ class DummyHandler(object):
def write(self, response): def write(self, response):
raise NotImplemented raise NotImplemented
def finish(self, response): def humbug_finish(self, response, *ignore):
if self.assert_callback: if self.assert_callback:
self.assert_callback(response) self.assert_callback(response)
class DummySession(object): class DummySession(object):
session_key = "0" session_key = "0"
@@ -1589,18 +1602,25 @@ class GetUpdatesTest(AuthedTestCase):
def common_test_get_updates(self, view_func, extra_post_data = {}): def common_test_get_updates(self, view_func, extra_post_data = {}):
user_profile = self.get_user_profile("hamlet@humbughq.com") user_profile = self.get_user_profile("hamlet@humbughq.com")
message_content = 'tornado test message'
self.got_callback = False
def callback(response): def callback(response):
correct_message_ids = [m.id for m in self.got_callback = True
filter_by_subscriptions(Message.objects.all(), user_profile)] msg = response['messages'][0]
for message in response['messages']: if str(msg['content_type']) == 'text/html':
self.assertGreater(message['id'], 1) self.assertEqual('<p>%s</p>' % message_content, msg['content'])
self.assertIn(message['id'], correct_message_ids) else:
self.assertEqual(message_content, msg['content'])
post_data = {} post_data = {}
post_data.update(extra_post_data) post_data.update(extra_post_data)
request = POSTRequestMock(post_data, user_profile, callback) request = POSTRequestMock(post_data, user_profile, callback)
self.assertEqual(view_func(request), RespondAsynchronously) self.assertEqual(view_func(request), RespondAsynchronously)
self.send_message("hamlet@humbughq.com", "hamlet@humbughq.com",
Recipient.PERSONAL, message_content)
self.assertTrue(self.got_callback)
def test_json_get_updates(self): def test_json_get_updates(self):
""" """

View File

@@ -120,7 +120,7 @@ def initialize_user_messages():
stream = streams[m.recipient.type_id] stream = streams[m.recipient.type_id]
add_stream_message(stream.realm.id, stream.name, m.id) add_stream_message(stream.realm.id, stream.name, m.id)
if not settings.DEPLOYED: if not settings.DEPLOYED and not settings.TEST_SUITE:
# Filling the memcached cache is a little slow, so do it in a child process. # Filling the memcached cache is a little slow, so do it in a child process.
# For DEPLOYED cases, we run this from restart_server. # For DEPLOYED cases, we run this from restart_server.
subprocess.Popen(["python", os.path.join(os.path.dirname(__file__), "..", "manage.py"), subprocess.Popen(["python", os.path.join(os.path.dirname(__file__), "..", "manage.py"),
@@ -352,6 +352,8 @@ def send_notification_http(data):
requests.post(settings.TORNADO_SERVER + '/notify_tornado', data=dict( requests.post(settings.TORNADO_SERVER + '/notify_tornado', data=dict(
data = ujson.dumps(data), data = ujson.dumps(data),
secret = settings.SHARED_SECRET)) secret = settings.SHARED_SECRET))
else:
process_notification(data)
def send_notification(data): def send_notification(data):
return queue_json_publish("notify_tornado", data, send_notification_http) return queue_json_publish("notify_tornado", data, send_notification_http)