Ejemplo n.º 1
0
    def test_call_wait_for_ready_enabled(self):
        # To test the wait mechanism, Python thread is required to make
        #   client set up first without handling them case by case.
        # Also, Python thread don't pass the unhandled exceptions to
        #   main thread. So, it need another method to store the
        #   exceptions and raise them again in main thread.
        unhandled_exceptions = queue.Queue()

        # We just need an unused TCP port
        host, port, sock = get_socket(sock_options=(socket.SO_REUSEADDR, ))
        sock.close()

        addr = '{}:{}'.format(host, port)
        wg = test_common.WaitGroup(len(_ALL_CALL_CASES))

        def wait_for_transient_failure(channel_connectivity):
            if channel_connectivity == grpc.ChannelConnectivity.TRANSIENT_FAILURE:
                wg.done()

        def test_call(perform_call):
            with grpc.insecure_channel(addr) as channel:
                try:
                    channel.subscribe(wait_for_transient_failure)
                    perform_call(channel, wait_for_ready=True)
                except BaseException as e:  # pylint: disable=broad-except
                    # If the call failed, the thread would be destroyed. The
                    # channel object can be collected before calling the
                    # callback, which will result in a deadlock.
                    wg.done()
                    unhandled_exceptions.put(e, True)

        test_threads = []
        for perform_call in _ALL_CALL_CASES:
            test_thread = threading.Thread(target=test_call,
                                           args=(perform_call, ))
            test_thread.daemon = True
            test_thread.exception = None
            test_thread.start()
            test_threads.append(test_thread)

        # Start the server after the connections are waiting
        wg.wait()
        server = test_common.test_server(reuse_port=True)
        server.add_generic_rpc_handlers(
            (_GenericHandler(weakref.proxy(self)), ))
        server.add_insecure_port(addr)
        server.start()

        for test_thread in test_threads:
            test_thread.join()

        # Stop the server to make test end properly
        server.stop(0)

        if not unhandled_exceptions.empty():
            raise unhandled_exceptions.get(True)
Ejemplo n.º 2
0
    def test_default_wait_for_ready(self):
        addr, port, sock = get_socket()
        sock.close()
        target = f'{addr}:{port}'
        channel = grpc._simple_stubs.ChannelCache.get().get_channel(
            target, (), None, True, None)
        rpc_finished_event = threading.Event()
        rpc_failed_event = threading.Event()
        server = None

        def _on_connectivity_changed(connectivity):
            nonlocal server
            if connectivity is grpc.ChannelConnectivity.TRANSIENT_FAILURE:
                self.assertFalse(rpc_finished_event.is_set())
                self.assertFalse(rpc_failed_event.is_set())
                server = test_common.test_server()
                server.add_insecure_port(target)
                server.add_generic_rpc_handlers((_GenericHandler(), ))
                server.start()
                channel.unsubscribe(_on_connectivity_changed)
            elif connectivity in (grpc.ChannelConnectivity.IDLE,
                                  grpc.ChannelConnectivity.CONNECTING):
                pass
            else:
                self.fail("Encountered unknown state.")

        channel.subscribe(_on_connectivity_changed)

        def _send_rpc():
            try:
                response = grpc.experimental.unary_unary(_REQUEST,
                                                         target,
                                                         _UNARY_UNARY,
                                                         timeout=None,
                                                         insecure=True)
                rpc_finished_event.set()
            except Exception as e:
                rpc_failed_event.set()

        t = threading.Thread(target=_send_rpc)
        t.start()
        t.join()
        self.assertFalse(rpc_failed_event.is_set())
        self.assertTrue(rpc_finished_event.is_set())
        if server is not None:
            server.stop(None)
Ejemplo n.º 3
0
def _pick_an_unused_port() -> int:
    """Picks an unused TCP port."""
    _, port, sock = get_socket()
    sock.close()
    return port
Ejemplo n.º 4
0
def create_dummy_channel():
    """Creating dummy channels is a workaround for retries"""
    host, port, sock = get_socket(sock_options=(socket.SO_REUSEADDR, ))
    sock.close()
    return grpc.insecure_channel('{}:{}'.format(host, port))
Ejemplo n.º 5
0
 async def setUp(self):
     address, self._port, self._socket = get_socket(
         listen=False, sock_options=(socket.SO_REUSEADDR, ))
     self._channel = aio.insecure_channel(f"{address}:{self._port}")
     self._socket.close()
Ejemplo n.º 6
0
 async def setUp(self):
     address, self._port, self._socket = get_socket(listen=False)
     self._channel = aio.insecure_channel(f"{address}:{self._port}")
     self._stub = test_pb2_grpc.TestServiceStub(self._channel)
     self._socket.close()
Ejemplo n.º 7
0
 async def setUp(self):
     address, self._port, self._socket = get_socket(listen=False)
     self._channel = aio.insecure_channel(f"{address}:{self._port}")
     self._socket.close()
Ejemplo n.º 8
0
 def start(self):
     _, self._port, self._listen_socket = get_socket(
         bind_address=self._bind_address)
     self._proxy_socket = _init_proxy_socket(self._gateway_address,
                                             self._gateway_port)
     self._thread.start()