Exemple #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()
        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(thread_pool.was_used())

        channel.close()
        server.stop(None)
  def test_reachable_then_unreachable_channel_connectivity(self):
    thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
    server = grpc.server(thread_pool)
    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)
    self.assertFalse(thread_pool.was_used())
Exemple #3
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()
        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(thread_pool.was_used())