async def test_receive_with_timeout(self): message = Message(MessageType.WEBSOCKET_INACTIVE) data = message.to_json() websocket = AsyncMock() websocket.recv = CoroutineMock() websocket.recv.return_value = data assert await receive(websocket, timeout_sec=3) == message
async def test_handle_exception_value_error(self, send, close): exception = ValueError("Hello!") websocket = AsyncMock() context = RequestFailedContext(FailureReason.INVALID_REQUEST, "Hello!") message = Message(MessageType.REQUEST_FAILED, context=context) json = message.to_json() await _handle_exception(exception, websocket) send.assert_awaited_once_with(websocket, json) close.assert_not_awaited()
async def test_handle_exception_processing_error_comment( self, send, close): exception = ProcessingError(FailureReason.INVALID_PLAYER, "comment") websocket = AsyncMock() context = RequestFailedContext(FailureReason.INVALID_PLAYER, "comment") message = Message(MessageType.REQUEST_FAILED, context=context) json = message.to_json() await _handle_exception(exception, websocket) send.assert_awaited_once_with(websocket, json) close.assert_not_awaited()
async def test_handle_exception_processing_error_websocket_limit( self, send, close): exception = ProcessingError(FailureReason.WEBSOCKET_LIMIT) websocket = AsyncMock() context = RequestFailedContext(FailureReason.WEBSOCKET_LIMIT, FailureReason.WEBSOCKET_LIMIT.value) message = Message(MessageType.REQUEST_FAILED, context=context) json = message.to_json() await _handle_exception(exception, websocket) send.assert_awaited_once_with(websocket, json) close.assert_awaited_once_with(websocket)
async def test_handle_exception_fail(self, send, close): # this just confirms that send failures aren't propogated to the caller (we intentionally ignore them) exception = ProcessingError(FailureReason.INVALID_PLAYER) websocket = AsyncMock() send.side_effect = Exception("Send failed!") context = RequestFailedContext(FailureReason.INVALID_PLAYER, FailureReason.INVALID_PLAYER.value) message = Message(MessageType.REQUEST_FAILED, context=context) json = message.to_json() await _handle_exception(exception, websocket) send.assert_awaited_once_with(websocket, json) close.assert_not_awaited()
async def test_handle_data(self, manager, event_handler, handle_message, data): handler = mock_handler() manager.return_value = handler.manager event_handler.return_value = handler websocket = AsyncMock() json = data["list.json"] message = Message.for_json(json) await _handle_data(json, websocket) event_handler.assert_called_once_with(manager.return_value) handler.manager.lock.assert_awaited() handle_message.assert_called_once_with(handler, message, websocket) handler.execute_tasks.assert_awaited_once()
async def test_send_message(self): message = Message(MessageType.WEBSOCKET_IDLE) websocket = AsyncMock() websocket.send = CoroutineMock() await send(websocket, message) websocket.send.assert_awaited_once_with(message.to_json())