def test_lonely_channel_connectivity(self): callback = _Callback() channel = _channel.Channel('localhost:12345', None, None) channel.subscribe(callback.update, try_to_connect=False) first_connectivities = callback.block_until_connectivities_satisfy(bool) channel.subscribe(callback.update, try_to_connect=True) 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 insecure_channel(target, options=None): """Creates an insecure Channel to a server. Args: target: The target to which to connect. options: A sequence of string-value pairs according to which to configure the created channel. Returns: A Channel to the target through which RPCs may be conducted. """ from grpc import _channel return _channel.Channel(target, options, None)
def test_reachable_then_unreachable_channel_connectivity(self): server = _server.Server((), futures.ThreadPoolExecutor(max_workers=0)) port = server.add_insecure_port('[::]:0') server.start() callback = _Callback() channel = _channel.Channel('localhost:{}'.format(port), None, None) 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)
def insecure_channel(target, options=None): """Creates an insecure Channel to a server. Args: target: The server address options: An optional list of key-value pairs (channel args in gRPC runtime) to configure the channel. Returns: A Channel object. """ from grpc import _channel # pylint: disable=cyclic-import return _channel.Channel(target, () if options is None else options, None)
def secure_channel(target, credentials, options=None): """Creates a secure Channel to a server. Args: target: The target to which to connect. credentials: A ChannelCredentials instance. options: A sequence of string-value pairs according to which to configure the created channel. Returns: A Channel to the target through which RPCs may be conducted. """ from grpc import _channel # pylint: disable=cyclic-import return _channel.Channel(target, () if options is None else options, credentials._credentials)
def test_reachable_then_unreachable_channel_connectivity(self): thread_pool = _thread_pool.RecordingThreadPool(max_workers=None) server = _server.Server(thread_pool, (), ()) port = server.add_insecure_port('[::]:0') server.start() callback = _Callback() channel = _channel.Channel('localhost:{}'.format(port), (), None) 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())
def test_immediately_connectable_channel_connectivity(self): thread_pool = _thread_pool.RecordingThreadPool(max_workers=None) server = _server.Server(thread_pool, ()) port = server.add_insecure_port('[::]:0') server.start() first_callback = _Callback() second_callback = _Callback() channel = _channel.Channel('localhost:{}'.format(port), None, None) 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())