def __init__(self, socket: WebSocket, request_handler: RequestHandler): if socket is None: raise TypeError( f"'socket: {socket.__class__.__name__}' argument can't be None" ) if not request_handler: raise TypeError( f"'request_handler: {request_handler.__class__.__name__}' argument can't be None" ) self.disconnected_event_handler: Callable[ [object, DisconnectedEventArgs], None ] = None self._web_socket_transport = WebSocketTransport(socket) self._request_handler = request_handler self._request_manager = RequestManager() self._sender = PayloadSender() self._sender.disconnected = self._on_connection_disconnected self._receiver = PayloadReceiver() self._receiver.disconnected = self._on_connection_disconnected self._protocol_adapter = ProtocolAdapter( self._request_handler, self._request_manager, self._sender, self._receiver ) self._closed_signal: Future = None self._is_disconnecting: bool = False
async def test_signal_response_returns_false_when_no_uuid(self): pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} manager = RequestManager(pending_requests=pending_requests) request_id: UUID = uuid4() response = ReceiveResponse() signal = await manager.signal_response(request_id=request_id, response=response) self.assertFalse(signal)
async def test_get_response_returns_null_on_duplicate_call(self): pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} request_id: UUID = uuid4() pending_requests[request_id] = Future() manager = RequestManager(pending_requests=pending_requests) response = await manager.get_response(request_id) self.assertIsNone(response)
async def test_signal_response_null_response_is_ok(self): pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} request_id: UUID = uuid4() pending_requests[request_id] = Future() manager = RequestManager(pending_requests=pending_requests) # noinspection PyTypeChecker _ = await manager.signal_response(request_id=request_id, response=None) self.assertIsNone(pending_requests[request_id].result())
async def test_signal_response_response(self): pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} request_id: UUID = uuid4() pending_requests[request_id] = Future() manager = RequestManager(pending_requests=pending_requests) response = ReceiveResponse() _ = await manager.signal_response(request_id=request_id, response=response) self.assertEqual(response, pending_requests[request_id].result())
async def test_get_response_returns_null_response(self): pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} request_id: UUID = uuid4() manager = RequestManager(pending_requests=pending_requests) async def set_response(): nonlocal manager nonlocal request_id while True: # noinspection PyTypeChecker signal = await manager.signal_response(request_id, response=None) if signal: break await asyncio.sleep(2) ensure_future(set_response()) response = await manager.get_response(request_id) self.assertIsNone(response)
async def test_get_response_returns_response(self): pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} request_id: UUID = uuid4() manager = RequestManager(pending_requests=pending_requests) test_response = ReceiveResponse() async def set_response(): nonlocal manager nonlocal request_id nonlocal test_response while True: signal = await manager.signal_response(request_id, response=test_response) if signal: break await asyncio.sleep(2) ensure_future(set_response()) response = await manager.get_response(request_id) self.assertEqual(test_response, response)
def test_ctor_empty_dictionary(self): pending_requests: Dict[UUID, Future[ReceiveResponse]] = {} _ = RequestManager(pending_requests=pending_requests) self.assertEqual(0, len(pending_requests))