async def test_send_static_ctrl_msg(mocker): # AsyncMock does not seem to exist in python 3.7, this is a manual workaround receive_cb = mocker.Mock() async def mock_wrapper(msg): receive_cb() args = set_pea_parser().parse_args([]) grpclet = Grpclet(args=args, message_callback=mock_wrapper) asyncio.get_event_loop().create_task(grpclet.start()) receive_cb.assert_not_called() while True: try: def send_status(): return Grpclet.send_ctrl_msg( pod_address=f'{args.host}:{args.port_in}', command='STATUS') await asyncio.get_event_loop().run_in_executor(None, send_status) break except RpcError: await asyncio.sleep(0.1) receive_cb.assert_called() await grpclet.close(None)
async def test_send_receive(mocker): # AsyncMock does not seem to exist in python 3.7, this is a manual workaround receive_cb = mocker.Mock() async def mock_wrapper(msg): receive_cb() args = set_pea_parser().parse_args([]) grpclet = Grpclet(args=args, message_callback=mock_wrapper) asyncio.get_event_loop().create_task(grpclet.start()) receive_cb.assert_not_called() await grpclet.send_message(_create_msg(args)) await asyncio.sleep(0.1) receive_cb.assert_called() await grpclet.close(None)
def __init__(self, args: argparse.Namespace, **kwargs): """Initialize grpc and data request handling. :param args: args from CLI :param kwargs: extra keyword arguments """ super().__init__(args, **kwargs) self._id = random_identity() self._loop = get_or_reuse_loop() self._last_active_time = time.perf_counter() self._pending_msgs = defaultdict(list) # type: Dict[str, List[Message]] self._partial_requests = None self._pending_tasks = [] self._static_routing_table = args.static_routing_table self._data_request_handler = DataRequestHandler(args, self.logger) self._grpclet = Grpclet( args=self.args, message_callback=self._callback, logger=self.logger, )
async def test_send_non_blocking(mocker): receive_cb = mocker.Mock() async def blocking_cb(msg): receive_cb() time.sleep(1.0) return msg args = set_pea_parser().parse_args([]) grpclet = Grpclet(args=args, message_callback=blocking_cb) asyncio.get_event_loop().create_task(grpclet.start()) receive_cb.assert_not_called() await grpclet.send_message(_create_msg(args)) await asyncio.sleep(0.1) assert receive_cb.call_count == 1 await grpclet.send_message(_create_msg(args)) await asyncio.sleep(0.1) assert receive_cb.call_count == 2 await grpclet.close(None)