Esempio n. 1
0
    def test_immediately_connectable_channel_connectivity(self):
        thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
        server = grpc.server(thread_pool, options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        callback = _Callback()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))

        ready_future = grpc.channel_ready_future(channel)
        ready_future.add_done_callback(callback.accept_value)
        self.assertIsNone(
            ready_future.result(timeout=test_constants.LONG_TIMEOUT))
        value_passed_to_callback = callback.block_until_called()
        self.assertIs(ready_future, value_passed_to_callback)
        self.assertFalse(ready_future.cancelled())
        self.assertTrue(ready_future.done())
        self.assertFalse(ready_future.running())
        # Cancellation after maturity has no effect.
        ready_future.cancel()
        self.assertFalse(ready_future.cancelled())
        self.assertTrue(ready_future.done())
        self.assertFalse(ready_future.running())
        self.assertFalse(thread_pool.was_used())
Esempio n. 2
0
    def test_reconnect(self):
        server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
        handler = grpc.method_handlers_generic_handler('test', {
            'UnaryUnary':
            grpc.unary_unary_rpc_method_handler(_handle_unary_unary)
        })
        sock_opt = _get_reuse_socket_option()
        port = _pick_and_bind_port(sock_opt)
        self.assertIsNotNone(port)

        server = grpc.server(server_pool, (handler, ))
        server.add_insecure_port('[::]:{}'.format(port))
        server.start()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))

        multi_callable = channel.unary_unary(_UNARY_UNARY)
        self.assertEqual(_RESPONSE, multi_callable(_REQUEST))
        server.stop(None)
        # By default, the channel connectivity is checked every 5s
        # GRPC_CLIENT_CHANNEL_BACKUP_POLL_INTERVAL_MS can be set to change
        # this.
        time.sleep(5.1)
        server = grpc.server(server_pool, (handler, ))
        server.add_insecure_port('[::]:{}'.format(port))
        server.start()
        self.assertEqual(_RESPONSE, multi_callable(_REQUEST))
    def test_lonely_channel_connectivity(self):
        callback = _Callback()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:12345',
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
        channel.subscribe(callback.update, try_to_connect=False)
        time.sleep(4)
        first_connectivities = callback.block_until_connectivities_satisfy(
            bool)
        channel.subscribe(callback.update, try_to_connect=True)
        time.sleep(4)
        second_connectivities = callback.block_until_connectivities_satisfy(
            lambda connectivities: 2 <= len(connectivities))
        # Wait for a connection that will never happen.
        time.sleep(test_constants.SHORT_TIMEOUT)
        third_connectivities = callback.connectivities()
        channel.unsubscribe(callback.update)
        fourth_connectivities = callback.connectivities()
        channel.unsubscribe(callback.update)
        fifth_connectivities = callback.connectivities()

        self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE, ),
                                 first_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.READY, second_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.READY, third_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.READY, fourth_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.READY, fifth_connectivities)
 def setUp(self):
     self._server = test_common.test_server()
     self._server.add_generic_rpc_handlers((_GenericHandler(), ))
     port = self._server.add_insecure_port('[::]:0')
     self._server.start()
     channel_config = grpc_gcp.api_config_from_text_pb('')
     self._channel = grpc_gcp.insecure_channel(
         'localhost:{}'.format(port),
         options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
    def setUp(self):
        self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
        self._trigger = _TestTrigger(test_constants.THREAD_CONCURRENCY)
        self._server = grpc.server(
            self._server_pool,
            handlers=(_GenericHandler(self._trigger),),
            options=(('grpc.so_reuseport', 0),),
            maximum_concurrent_rpcs=test_constants.THREAD_CONCURRENCY)
        port = self._server.add_insecure_port('[::]:0')
        self._server.start()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        self._channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config),)
        )
    def test_immediately_connectable_channel_connectivity(self):
        thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
        server = grpc.server(thread_pool, options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        first_callback = _Callback()
        second_callback = _Callback()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
        channel.subscribe(first_callback.update, try_to_connect=False)
        first_connectivities = first_callback.block_until_connectivities_satisfy(
            bool)
        # Wait for a connection that will never happen because try_to_connect=True
        # has not yet been passed.
        time.sleep(test_constants.SHORT_TIMEOUT)
        second_connectivities = first_callback.connectivities()
        channel.subscribe(second_callback.update, try_to_connect=True)
        third_connectivities = first_callback.block_until_connectivities_satisfy(
            lambda connectivities: 2 <= len(connectivities))
        fourth_connectivities = second_callback.block_until_connectivities_satisfy(
            bool)
        # Wait for a connection that will happen (or may already have happened).
        first_callback.block_until_connectivities_satisfy(
            _ready_in_connectivities)
        second_callback.block_until_connectivities_satisfy(
            _ready_in_connectivities)
        del channel

        self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE, ),
                                 first_connectivities)
        self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE, ),
                                 second_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.TRANSIENT_FAILURE,
                         third_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.SHUTDOWN,
                         third_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.TRANSIENT_FAILURE,
                         fourth_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.SHUTDOWN,
                         fourth_connectivities)
        self.assertFalse(thread_pool.was_used())
Esempio n. 7
0
 def test_lonely_channel_connectivity(self):
     callback = _Callback()
     channel_config = grpc_gcp.api_config_from_text_pb('')
     channel = grpc_gcp.insecure_channel(
         'localhost:12345',
         options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
     ready_future = grpc.channel_ready_future(channel)
     ready_future.add_done_callback(callback.accept_value)
     with self.assertRaises(grpc.FutureTimeoutError):
         ready_future.result(timeout=test_constants.SHORT_TIMEOUT)
     self.assertFalse(ready_future.cancelled())
     self.assertFalse(ready_future.done())
     self.assertTrue(ready_future.running())
     ready_future.cancel()
     value_passed_to_callback = callback.block_until_called()
     self.assertIs(ready_future, value_passed_to_callback)
     self.assertTrue(ready_future.cancelled())
     self.assertTrue(ready_future.done())
     self.assertFalse(ready_future.running())
    def setUp(self):
        self._servicer = _Servicer()
        self._server = test_common.test_server()
        self._server.add_generic_rpc_handlers(
            (_generic_handler(self._servicer), ))
        port = self._server.add_insecure_port('[::]:0')
        self._server.start()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))

        self._unary_unary = channel.unary_unary(
            '/'.join((
                '',
                _SERVICE,
                _UNARY_UNARY,
            )),
            request_serializer=_REQUEST_SERIALIZER,
            response_deserializer=_RESPONSE_DESERIALIZER,
        )
        self._unary_stream = channel.unary_stream(
            '/'.join((
                '',
                _SERVICE,
                _UNARY_STREAM,
            )), )
        self._stream_unary = channel.stream_unary(
            '/'.join((
                '',
                _SERVICE,
                _STREAM_UNARY,
            )), )
        self._stream_stream = channel.stream_stream(
            '/'.join((
                '',
                _SERVICE,
                _STREAM_STREAM,
            )),
            request_serializer=_REQUEST_SERIALIZER,
            response_deserializer=_RESPONSE_DESERIALIZER,
        )
    def test_reachable_then_unreachable_channel_connectivity(self):
        thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
        server = grpc.server(thread_pool, options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        callback = _Callback()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
        channel.subscribe(callback.update, try_to_connect=True)
        callback.block_until_connectivities_satisfy(_ready_in_connectivities)
        # Now take down the server and confirm that channel readiness is repudiated.
        server.stop(None)
        callback.block_until_connectivities_satisfy(
            _last_connectivity_is_not_ready)
        channel.unsubscribe(callback.update)
        self.assertFalse(thread_pool.was_used())
Esempio n. 10
0
    if args.scenario == UNSTARTED_SERVER:
        server = grpc.server(DaemonPool(),
                             options=(('grpc.so_reuseport', 0), ))
        if args.wait_for_interrupt:
            time.sleep(WAIT_TIME)
    elif args.scenario == RUNNING_SERVER:
        server = grpc.server(DaemonPool(),
                             options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        if args.wait_for_interrupt:
            time.sleep(WAIT_TIME)
    elif args.scenario == POLL_CONNECTIVITY_NO_SERVER:
        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:12345',
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))

        def connectivity_callback(connectivity):
            pass

        channel.subscribe(connectivity_callback, try_to_connect=True)
        if args.wait_for_interrupt:
            time.sleep(WAIT_TIME)
    elif args.scenario == POLL_CONNECTIVITY:
        server = grpc.server(DaemonPool(),
                             options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()

        channel_config = grpc_gcp.api_config_from_text_pb('')