diff --git a/nameko_grpc/client.py b/nameko_grpc/client.py index 0f6d53a..224943d 100644 --- a/nameko_grpc/client.py +++ b/nameko_grpc/client.py @@ -152,7 +152,9 @@ def _get_channel(self): return channel def stop(self): - self._channel.stop() + if self._channel is not None: + self._channel.stop() + self._channel = None @property def channel(self): diff --git a/test/conftest.py b/test/conftest.py index 3fa9aac..7701b01 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -352,6 +352,8 @@ def make( proto_name=None, compression_algorithm="none", compression_level="high", + lazy=False, + service_url="//localhost:{}".format(grpc_port), ): if proto_name is None: proto_name = service_name @@ -359,11 +361,12 @@ def make( stubs = load_stubs(proto_name) stub_cls = getattr(stubs, "{}Stub".format(service_name)) client = Client( - "//localhost:{}".format(grpc_port), + service_url, stub_cls, compression_algorithm, compression_level, ssl_options, + lazy=lazy, ) clients.append(client) return client.start() diff --git a/test/test_basic.py b/test/test_basic.py index b24038e..7069bc3 100644 --- a/test/test_basic.py +++ b/test/test_basic.py @@ -109,3 +109,30 @@ def generate_requests(): ("A", 1), ("B", 2), ] + + +class TestLazy: + def test_lazy_client(self, start_nameko_client, server, protobufs): + client = start_nameko_client("example", lazy=True) + response = client.unary_unary(protobufs.ExampleRequest(value="A")) + assert response.message == "A" + + # Note lack of server fixture + def test_lazy_client_does_not_connect_on_start( + self, start_nameko_client, protobufs, start_grpc_server + ): + client = start_nameko_client("example", lazy=True) + + with pytest.raises(ConnectionRefusedError): + client.unary_unary(protobufs.ExampleRequest(value="A")) + + start_grpc_server("example") + + # After starting the server, should now work + response = client.unary_unary(protobufs.ExampleRequest(value="A")) + assert response.message == "A" + + # Note lack of server fixture + def test_nonlazy_client_connects_on_start(self, start_nameko_client, protobufs): + with pytest.raises(ConnectionRefusedError): + start_nameko_client("example", lazy=False)