Deliver messages inside Tornado when running tests

This saves us from having to run queue processors while testing.

(imported from commit 927bfa497a47b34603761a148c57d82b2f34f813)
This commit is contained in:
Zev Benjamin
2013-10-11 15:31:20 -04:00
parent a04ac35012
commit b1eebc0b84
2 changed files with 22 additions and 3 deletions

View File

@@ -11,8 +11,10 @@ import ujson
import logging
import time
from zerver.models import UserProfile
from zerver.models import UserProfile, get_user_profile_by_id, get_client
from zerver.lib.queue import queue_json_publish
from zerver.lib.actions import check_send_message, extract_recipients
from zerver.decorator import JsonableError
djsession_engine = import_module(settings.SESSION_ENGINE)
def get_user_profile(session_id):
@@ -127,7 +129,7 @@ class SocketConnection(sockjs.tornado.SockJSConnection):
server_meta=dict(connection_id=self.connection_id,
return_queue="tornado_return",
start_time=start_time)),
lambda e: None)
fake_message_sender)
def on_close(self):
deregister_connection(self)
@@ -138,6 +140,23 @@ class SocketConnection(sockjs.tornado.SockJSConnection):
fake_log_line(self.session.conn_info, 0, 200,
'Connection closed', 'unknown')
def fake_message_sender(event):
req = event['request']
try:
sender = get_user_profile_by_id(req['sender_id'])
client = get_client(req['client_name'])
msg_id = check_send_message(sender, client, req['type'],
extract_recipients(req['to']),
req['subject'], req['content'])
resp = {"result": "success", "msg": "", "id": msg_id}
except JsonableError as e:
resp = {"result": "error", "msg": str(e)}
result = {'response': resp, 'client_meta': event['client_meta'],
'server_meta': event['server_meta']}
respond_send_message(None, None, None, result)
def respond_send_message(chan, method, props, data):
connection = get_connection(data['server_meta']['connection_id'])
if connection is not None:

View File

@@ -456,7 +456,7 @@ def process_notification(data):
# different types and for compatibility with non-HTTP transports.
def send_notification_http(data):
if settings.TORNADO_SERVER:
if settings.TORNADO_SERVER and not settings.RUNNING_INSIDE_TORNADO:
requests.post(settings.TORNADO_SERVER + '/notify_tornado', data=dict(
data = ujson.dumps(data),
secret = settings.SHARED_SECRET))