Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
joechild-pace committed Oct 2, 2023
1 parent 23962e5 commit 21ebede
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 3 additions & 1 deletion nameko_grpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 4 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,21 @@ 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

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()
Expand Down
27 changes: 27 additions & 0 deletions test/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 21ebede

Please sign in to comment.