def test_immediately_connectable_channel_connectivity(self):
        recording_thread_pool = thread_pool.RecordingThreadPool(
            max_workers=None)
        server = grpc.server(recording_thread_pool,
                             options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        channel = grpc.insecure_channel('localhost:{}'.format(port))
        callback = _Callback()

        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(recording_thread_pool.was_used())

        channel.close()
        server.stop(None)
Пример #2
0
    def setUp(self):
        self._control = test_control.PauseFailControl()
        self._thread_pool = thread_pool.RecordingThreadPool(max_workers=None)
        self._handler = _Handler(self._control, self._thread_pool)

        self._server = test_common.test_server()
        port = self._server.add_insecure_port('[::]:0')
        self._server.add_generic_rpc_handlers((_GenericHandler(self._handler),))
        self._server.start()

        self._channel = grpc.insecure_channel('localhost:%d' % port)
Пример #3
0
    def test_immediately_connectable_channel_connectivity(self):
        recording_thread_pool = thread_pool.RecordingThreadPool(
            max_workers=None)
        server = grpc.server(recording_thread_pool,
                             options=(('grpc.so_reuseport', 0),))
        port = server.add_insecure_port('[::]:0')
        server.start()
        first_callback = _Callback()
        second_callback = _Callback()

        channel = grpc.insecure_channel('localhost:{}'.format(port))
        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)
        channel.close()
        server.stop(None)

        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(recording_thread_pool.was_used())
Пример #4
0
    def test_reachable_then_unreachable_channel_connectivity(self):
        recording_thread_pool = thread_pool.RecordingThreadPool(
            max_workers=None)
        server = grpc.server(recording_thread_pool,
                             options=(('grpc.so_reuseport', 0),))
        port = server.add_insecure_port('[::]:0')
        server.start()
        callback = _Callback()

        channel = grpc.insecure_channel('localhost:{}'.format(port))
        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)
        channel.close()
        self.assertFalse(recording_thread_pool.was_used())
Пример #5
0
 def setUp(self):
     self._thread_pool = thread_pool.RecordingThreadPool(max_workers=None)
     super(HealthServicerTest,
           self).start_server(non_blocking=True,
                              thread_pool=self._thread_pool)