async def test_eater_connection_closed(self, mocker, test_eater, test_dumpling_pktcount): """ Test getting a ConnectionClosed exception from the websocket. The hub should remove the eater from its eater list. """ mock_websocket = mocker.Mock() mock_websocket.remote_address = ['eaterhost', 22222] mock_websocket.recv = asynctest.CoroutineMock( return_value=json.dumps(test_eater)) mock_websocket.send = asynctest.CoroutineMock() # Prepare to send one dumpling before faking a ConnectionClosed. mock_queue = mocker.patch('asyncio.Queue') mock_queue.return_value.get = asynctest.CoroutineMock(side_effect=[ json.dumps(test_dumpling_pktcount), ConnectionClosed(1006, reason='unknown'), ]) hub = DumplingHub() assert len(hub._dumpling_eaters) == 0 assert hub._system_stats['dumplings_out'] == 0 await hub._emit_dumplings(mock_websocket, path=None) # Check that we no longer have any eaters in the eater list, but that # we still sent a dumpling. assert len(hub._dumpling_eaters) == 0 assert hub._system_stats['dumplings_out'] == 1 mock_websocket.send.assert_called_once_with( json.dumps(test_dumpling_pktcount))
def test_live_pipe_handle_error(): from websockets.exceptions import ConnectionClosed error_handler = mock.MagicMock() live = Live(serializer=None) live.add_error_handler(error_handler) websocket = mock.MagicMock() websocket.send = mock.MagicMock() websocket.send.side_effect = ConnectionClosed(code=1006, reason='socket closed') assert live.error_handlers assert error_handler in live.error_handlers live_filter = LiveFilter(ws=websocket, criteria={}) live.add_filter(live_filter) assert live.filters assert websocket in live.filters loop = asyncio.new_event_loop() loop.run_until_complete( live.pipe(Event(id='event-0', source='src-0', timestamp=10))) loop.close() assert live.filters is not None assert websocket not in live.filters
async def test_handle_connection_shutdown(self, handle_data, handle_exception, handle_connect, handle_disconnect): websocket = AsyncMock() exception = ConnectionClosed(None, None) websocket.__aiter__.side_effect = exception # the wait on the websocket is what throws the connection closed handle_data.side_effect = exception await _handle_connection(websocket, "path") # path is unused handle_data.assert_not_called() handle_connect.assert_called_once_with(websocket) handle_disconnect.assert_called_once_with(websocket) handle_exception.assert_not_called()
def test_player_receive_json_rpc_response_connection_closed(self): """Tests that Player.receive_json_rpc_response_connection_closed() returns None when websocket throws ConnectionClosed exception """ websocket = MagicMock() recv = AsyncMock(side_effect=ConnectionClosed(0, 'Connection Closed')) websocket.attach_mock(recv, 'recv') player = Player(None, websocket) (x, y, z) = asyncio.run(player.receive_json_rpc_response()) self.assertEqual((x, y, z), ( None, None, None ), '(None, None, None) expected for receive_json_rpc_response() when connection is closed' )
def test_player_send_json_rpc_request_throws_connection_closed(self): """Tests Player.send_json_rpc_request() when websocket send() throws a ConnectionClosed exception """ websocket = MagicMock() send = AsyncMock(side_effect=ConnectionClosed(0, 'Connection Closed')) websocket.attach_mock(send, 'send') player = Player(None, websocket) string = "Player is " + str([player]) params = dict() params['a'] = 'x' asyncio.run(player.send_json_rpc_request("general_request", params)) # No exception should propage as expected send.assert_called_once()
async def test_kitchen_connection_closed(self, mocker, test_kitchen, test_dumpling_pktcount): """ Test getting a ConnectionClosed exception from the websocket. The hub should remove the kitchen from its kitchen list. """ mock_websocket = mocker.Mock() mock_websocket.remote_address = ['kitchenhost', 11111] mock_websocket.recv = asynctest.CoroutineMock(side_effect=[ json.dumps(test_kitchen), json.dumps(test_dumpling_pktcount), ConnectionClosed(1006, reason='unknown'), ]) hub = DumplingHub() # Set up a mock eater. eater_1 = mocker.Mock() hub._dumpling_eaters = { eater_1: { 'queue': mocker.Mock(), }, } hub._dumpling_eaters[eater_1]['queue'].put = asynctest.CoroutineMock() # We should start with no kitchens. assert len(hub._dumpling_kitchens) == 0 await hub._grab_dumplings(mock_websocket, path=None) # Check that our kitchen list is empty again. assert len(hub._dumpling_kitchens) == 0 # Check that the dumpling was still put onto the eater queue. hub._dumpling_eaters[eater_1]['queue'].put.assert_called_once_with( json.dumps(test_dumpling_pktcount))
async def recv(self): if not self.responses: await asyncio.sleep(1) # delay to give test time to finish raise ConnectionClosed(0, 'ran out of responses') return json.dumps(self.responses.popleft())
async def recv(self): if not self.responses: raise ConnectionClosed(0, 'ran out of responses') return json.dumps(self.responses.popleft())
def setUp(self): self.client = WebsocketClient() self.client.recv = CoroutineMock(side_effect=ConnectionClosed(1, "reason"))
async def _recv(self): if not self._running: raise ConnectionClosed(1, "str") val = MagicMock() val.result = MagicMock(return_value="OK") return val