From 4b9e238aa844e4964820118e82c33d5b464330d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Evren=20Esat=20=C3=96zkan?= Date: Thu, 21 Jul 2016 07:28:20 +0300 Subject: [PATCH] pre-e2e-refactor rref #5367 rref #5366 ref zetaops/zengine#66 ref zetaops/zengine#65 --- tests/async_amqp/messaging_tests.py | 3 +-- zengine/lib/concurrent_amqp_test_client.py | 15 ++++++++------- zengine/messaging/model.py | 8 +++++--- zengine/messaging/views.py | 6 ++++++ zengine/tornado_server/ws_to_queue.py | 4 ++-- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/tests/async_amqp/messaging_tests.py b/tests/async_amqp/messaging_tests.py index e17a6e78..254b4ad4 100644 --- a/tests/async_amqp/messaging_tests.py +++ b/tests/async_amqp/messaging_tests.py @@ -14,8 +14,7 @@ def test_channel_list(self): self.post('ulakbus', {"view": "_zops_list_channels"}) def test_search_user(self): - self.post('ulakbus', {"view": "_zops_search_user", - "query": "x"}) + self.post('ulakbus', {"view": "_zops_search_user", "query": "x"}) def test_show_channel(self): self.post('ulakbus', diff --git a/zengine/lib/concurrent_amqp_test_client.py b/zengine/lib/concurrent_amqp_test_client.py index df42fb0a..ffb7fdda 100644 --- a/zengine/lib/concurrent_amqp_test_client.py +++ b/zengine/lib/concurrent_amqp_test_client.py @@ -33,7 +33,6 @@ from zengine.views.auth import Login sys.sessid_to_userid = {} -sys.test_method_names = {} UserModel = get_object_from_path(settings.USER_MODEL) @@ -97,6 +96,8 @@ def backend_to_client(self, body): try: self.message_callbacks[body['callbackID']](body) except KeyError: + print("No cb for %s" % body['callbackID']) + print("CB HELL %s" % self.message_callbacks) self.message_stack[body['callbackID']] = body log.info("WRITE MESSAGE TO CLIENT:\n%s" % (body,)) @@ -107,10 +108,11 @@ def client_to_backend(self, message, callback, caller_fn_name): cbid = uuid.uuid4().hex message = json_encode({"callbackID": cbid, "data": message}) def cb(res): - print("Testing: %s :: " % caller_fn_name, end='') - callback(res, message) + result = callback(res, message) + print("API Request: %s :: %s\n" % (caller_fn_name, 'PASS' if result else 'FAIL!')) # self.message_callbacks[cbid] = lambda res: callable(res, message) self.message_callbacks[cbid] = cb + print(caller_fn_name, self.message_callbacks) log.info("GOT MESSAGE FOR BACKEND %s: %s" % (self.sess_id, message)) self.queue_manager.redirect_incoming_message(self.sess_id, message, self.request) @@ -128,7 +130,6 @@ def __init__(self, queue_manager): self.queue_manager = queue_manager self.clients = {} self.make_client('ulakbus') - self.test_fn_name = '' self.run_tests() def make_client(self, username): @@ -147,12 +148,12 @@ def post(self, username, data, callback=None): if username not in self.clients: self.make_client(username) callback = callback or self.stc - self.clients[username].client_to_backend(data, callback, self.test_fn_name) + view_name = data['view'] if 'view' in data else sys._getframe(1).f_code.co_name + self.clients[username].client_to_backend(data, callback, view_name) def run_tests(self): for name in sorted(self.__class__.__dict__): if name.startswith("test_"): - self.test_fn_name = name[5:] getattr(self, name)() def process_error_reponse(self, resp): @@ -174,7 +175,7 @@ def stc(self, response, request=None): print("\nRESP:\n%s") print("\nREQ:\n %s" % (response, request)) else: - print("PASS!\n") + return True def pstc(self, response, request=None): """ diff --git a/zengine/messaging/model.py b/zengine/messaging/model.py index 6d4483f8..394f12aa 100644 --- a/zengine/messaging/model.py +++ b/zengine/messaging/model.py @@ -228,8 +228,9 @@ def bind_to_channel(self): Binds (subscribes) users private exchange to channel exchange Automatically called at creation of subscription record. """ - channel = self._connect_mq() - channel.exchange_bind(source=self.channel.code_name, destination=self.user.prv_exchange) + if self.channel.code_name != self.user.prv_exchange: + channel = self._connect_mq() + channel.exchange_bind(source=self.channel.code_name, destination=self.user.prv_exchange) def post_creation(self): self.create_exchange() @@ -303,13 +304,14 @@ def serialize(self, user=None): return { 'content': self.body, 'type': self.typ, - 'updated_at': self.updated_at.strftime(DATE_TIME_FORMAT) if self.updated_at else None, + 'updated_at': self.updated_at, 'timestamp': self.timestamp.strftime(DATE_TIME_FORMAT), 'is_update': self.exist, 'attachments': [attachment.serialize() for attachment in self.attachment_set], 'title': self.msg_title, 'sender_name': self.sender.full_name, 'sender_key': self.sender.key, + 'cmd': 'message', 'avatar_url': self.sender.avatar, 'key': self.key, 'actions': self.get_actions_for(user), diff --git a/zengine/messaging/views.py b/zengine/messaging/views.py index 71a8a5ec..26f14d44 100644 --- a/zengine/messaging/views.py +++ b/zengine/messaging/views.py @@ -32,6 +32,7 @@ 'type': int, 'avatar_url': string, 'key': key, + 'cmd': 'message', 'actions':[('action name', 'view name'), ('Add to Favorite', '_zops_add_to_favorites'), # applicable to everyone @@ -112,6 +113,11 @@ def create_message(current): msg = current.input['message'] msg_obj = Channel.add_message(msg['channel'], body=msg['body'], typ=msg['type'], sender=current.user, title=msg['title'], receiver=msg['receiver'] or None) + current.output = { + 'msg_key': msg_obj.key, + 'status': 'OK', + 'code': 201 + } if 'attachment' in msg: for atch in msg['attachments']: typ = current._dedect_file_type(atch['name'], atch['content']) diff --git a/zengine/tornado_server/ws_to_queue.py b/zengine/tornado_server/ws_to_queue.py index cf8e9f4b..037333e3 100644 --- a/zengine/tornado_server/ws_to_queue.py +++ b/zengine/tornado_server/ws_to_queue.py @@ -268,13 +268,13 @@ def on_message(self, channel, method, header, body): if user_id in self.websockets: log.info("write msg to client") self.websockets[user_id].write_message(body) - channel.basic_ack(delivery_tag=method.delivery_tag) + # channel.basic_ack(delivery_tag=method.delivery_tag) elif 'sessid_to_userid' in body: reply = json_decode(body) sys.sessid_to_userid[reply['sess_id']] = reply['user_id'] self.websockets[reply['user_id']] = self.websockets[reply['sess_id']] del self.websockets[reply['sess_id']] - channel.basic_ack(delivery_tag=method.delivery_tag) + channel.basic_ack(delivery_tag=method.delivery_tag) # else: # channel.basic_reject(delivery_tag=method.delivery_tag)