def test_sends_serialized_messages_down_websocket(self, websocket): handler = mock.Mock(return_value={'just': 'some message'}) message = messages.Message(topic='foo', payload={'foo': 'bar'}) socket = FakeSocket('a') websocket.instances = [socket] messages.handle_message(message, topic_handlers={'foo': handler}) socket.send.assert_called_once_with('{"just": "some message"}')
def test_does_not_send_messages_down_websocket_if_handler_response_is_none(self, websocket): handler = mock.Mock(return_value=None) message = messages.Message(topic='foo', payload={'foo': 'bar'}) socket = FakeSocket('a') websocket.instances = [socket] messages.handle_message(message, topic_handlers={'foo': handler}) assert socket.send.call_count == 0
def test_calls_handler_with_list_of_sockets(self, websocket): handler = mock.Mock(return_value=None) session = mock.sentinel.db_session message = messages.Message(topic='foo', payload={'foo': 'bar'}) websocket.instances = [FakeSocket('a'), FakeSocket('b')] messages.handle_message(message, session, topic_handlers={'foo': handler}) handler.assert_called_once_with(message.payload, list(websocket.instances), session)
def test_does_not_send_messages_down_websocket_if_socket_terminated(self, websocket): handler = mock.Mock(return_value={'just': 'some message'}) message = messages.Message(topic='foo', payload={'foo': 'bar'}) socket = FakeSocket('a') socket.terminated = True websocket.instances = [socket] messages.handle_message(message, topic_handlers={'foo': handler}) assert socket.send.call_count == 0
def test_it_raises_RuntimeError_for_bad_topics(self, registry): message = messages.Message(topic="unknown", payload={}) topic_handlers = {"known": sentinel.handler} with pytest.raises(RuntimeError): messages.handle_message( message, registry, session=sentinel.db_session, topic_handlers=topic_handlers, )
def process_work_queue(settings, queue, session_factory=None): """ Process each message from the queue in turn, handling exceptions. This is the core of the streamer: we pull messages off the work queue, dispatching them as appropriate. The handling of each message is wrapped in code that ensures the database session is appropriately committed and closed between messages. """ if session_factory is None: session_factory = _get_session s = stats.get_client(settings).pipeline() session = session_factory(settings) topic_handlers = { ANNOTATION_TOPIC: messages.handle_annotation_event, USER_TOPIC: messages.handle_user_event, } for msg in queue: t_total = s.timer("streamer.msg.handler_total") t_total.start() try: # All access to the database in the streamer is currently # read-only, so enforce that: session.execute( "SET TRANSACTION " "ISOLATION LEVEL SERIALIZABLE " "READ ONLY " "DEFERRABLE" ) if isinstance(msg, messages.Message): with s.timer("streamer.msg.handler_message"): messages.handle_message(msg, settings, session, topic_handlers) elif isinstance(msg, websocket.Message): with s.timer("streamer.msg.handler_websocket"): websocket.handle_message(msg, session) else: raise UnknownMessageType(repr(msg)) except (KeyboardInterrupt, SystemExit): session.rollback() raise except Exception as exc: log.warning("Caught exception handling streamer message:", exc_info=exc) session.rollback() else: session.commit() finally: session.close() t_total.stop() s.send()
def test_calls_handler_once_per_socket_with_deserialized_message(self, websocket): handler = mock.Mock(return_value=None) message = messages.Message(topic='foo', payload={'foo': 'bar'}) websocket.instances = [FakeSocket('a'), FakeSocket('b')] messages.handle_message(message, topic_handlers={'foo': handler}) print(repr(mock.call({'foo': 'bar'}, websocket.instances[0]))) print(repr(handler.mock_calls[0])) assert handler.mock_calls == [ mock.call({'foo': 'bar'}, websocket.instances[0]), mock.call({'foo': 'bar'}, websocket.instances[1]), ]
def test_calls_handler_with_list_of_sockets(self, websocket): handler = mock.Mock(return_value=None) session = mock.sentinel.db_session settings = mock.sentinel.settings message = messages.Message(topic="foo", payload={"foo": "bar"}) websocket.instances = [FakeSocket("a"), FakeSocket("b")] messages.handle_message( message, settings, session, topic_handlers={"foo": handler} ) handler.assert_called_once_with( message.payload, list(websocket.instances), settings, session )
def test_calls_handler_with_list_of_sockets(self, websocket, registry): handler = Mock(return_value=None) session = sentinel.db_session message = messages.Message(topic="foo", payload={"foo": "bar"}) websocket.instances = [sentinel.socket_1, sentinel.socket_2] messages.handle_message(message, registry, session, topic_handlers={"foo": handler}) handler.assert_called_once_with( message.payload, websocket.instances, Any.object.of_type(Request).with_attrs({"registry": registry}), session, )
def process_work_queue(registry, queue): """ Process each message from the queue in turn, handling exceptions. This is the core of the streamer: we pull messages off the work queue, dispatching them as appropriate. The handling of each message is wrapped in code that ensures the database session is appropriately committed and closed between messages. """ session = db.get_session(registry.settings) for msg in queue: with db.read_only_transaction(session): if isinstance(msg, messages.Message): messages.handle_message(msg, registry, session, TOPIC_HANDLERS) elif isinstance(msg, websocket.Message): websocket.handle_message(msg, session) else: raise UnknownMessageType(repr(msg))
def process_work_queue(settings, queue, session_factory=db.Session): """ Process each message from the queue in turn, handling exceptions. This is the core of the streamer: we pull messages off the work queue, dispatching them as appropriate. The handling of each message is wrapped in code that ensures the database session is appropriately committed and closed between messages. """ session = session_factory() topic_handlers = { ANNOTATION_TOPIC: messages.handle_annotation_event, USER_TOPIC: messages.handle_user_event, } for msg in queue: try: # All access to the database in the streamer is currently # read-only, so enforce that: session.execute("SET TRANSACTION " "ISOLATION LEVEL SERIALIZABLE " "READ ONLY " "DEFERRABLE") if isinstance(msg, messages.Message): messages.handle_message(msg, topic_handlers=topic_handlers) elif isinstance(msg, websocket.Message): websocket.handle_message(msg) else: raise UnknownMessageType(repr(msg)) except (KeyboardInterrupt, SystemExit): session.rollback() raise except: log.exception('Caught exception handling streamer message:') session.rollback() else: session.commit() finally: session.close()