Skip to content

Commit 426aa54

Browse files
Mike ProsserMike Prosser
authored andcommitted
cleanup
1 parent d971106 commit 426aa54

File tree

4 files changed

+24
-27
lines changed

4 files changed

+24
-27
lines changed

src/nipanel/_streamlit_panel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
class StreamlitPanel(Panel):
88
"""This class allows you to connect to a Streamlit panel and specify values for its controls."""
99

10+
PYTHON_PANEL_SERVICE = "ni.pythonpanel.v1.PythonPanelService"
11+
1012
__slots__ = ()
1113

1214
def __init__(self, panel_id: str, streamlit_script_uri: str) -> None:
@@ -24,7 +26,5 @@ def __init__(self, panel_id: str, streamlit_script_uri: str) -> None:
2426
def _resolve_service_address(self) -> str:
2527
with GrpcChannelPool() as grpc_channel_pool:
2628
discovery_client = DiscoveryClient(grpc_channel_pool=grpc_channel_pool)
27-
service_location = discovery_client.resolve_service(
28-
"ni.pythonpanel.v1.PythonPanelService"
29-
)
30-
return service_location.insecure_address
29+
service_location = discovery_client.resolve_service(self.PYTHON_PANEL_SERVICE)
30+
return service_location.insecure_address

tests/conftest.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Fixtures for testing gRPC services."""
1+
"""Fixtures for testing."""
22

33
from concurrent import futures
44
from typing import Any, Generator
@@ -14,24 +14,23 @@
1414

1515

1616
@pytest.fixture
17-
def fake_python_panel_service() -> Generator[tuple[grpc.Server, int], Any, None]:
17+
def fake_python_panel_service() -> Generator[tuple[FakePythonPanelServicer, int], Any, None]:
1818
"""Fixture to create a FakePythonPanelServicer for testing."""
19-
# Create an in-process gRPC server
20-
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
19+
thread_pool = futures.ThreadPoolExecutor(max_workers=10)
20+
server = grpc.server(thread_pool)
2121
servicer = FakePythonPanelServicer()
22-
2322
add_PythonPanelServiceServicer_to_server(servicer, server)
24-
port = server.add_insecure_port("[::]:0") # Bind to an available port
23+
port = server.add_insecure_port("[::]:0")
2524
server.start()
26-
yield server, port
25+
yield servicer, port
2726
server.stop(None)
2827

2928

3029
@pytest.fixture
3130
def fake_python_panel_service_stub(
32-
fake_python_panel_service: tuple[grpc.Server, int],
31+
fake_python_panel_service: tuple[FakePythonPanelServicer, int],
3332
) -> Generator[PythonPanelServiceStub, Any, None]:
34-
"""Fixture to create a gRPC stub for the FakePythonPanelService."""
33+
"""Fixture to attach a PythonPanelSericeStub to a FakePythonPanelService."""
3534
_, port = fake_python_panel_service
3635
channel = grpc.insecure_channel(f"localhost:{port}")
3736
yield PythonPanelServiceStub(channel)

tests/unit/test_panel.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import grpc
21
import pytest
32

43
from tests.utils._fake_python_panel_servicer import FakePythonPanelServicer
@@ -12,7 +11,7 @@ def test___panel___has_panel_id_and_panel_uri() -> None:
1211

1312

1413
def test___connected_panel___set_value___gets_same_value(
15-
fake_python_panel_service: tuple[grpc.Server, int],
14+
fake_python_panel_service: tuple[FakePythonPanelServicer, int],
1615
) -> None:
1716
_, port = fake_python_panel_service
1817
panel = PortPanel(port, "my_panel", "path/to/script")
@@ -26,7 +25,7 @@ def test___connected_panel___set_value___gets_same_value(
2625

2726

2827
def test___with_panel___set_value___gets_same_value(
29-
fake_python_panel_service: tuple[grpc.Server, int],
28+
fake_python_panel_service: tuple[FakePythonPanelServicer, int],
3029
) -> None:
3130
_, port = fake_python_panel_service
3231
with PortPanel(port, "my_panel", "path/to/script") as panel:
@@ -37,7 +36,7 @@ def test___with_panel___set_value___gets_same_value(
3736

3837

3938
def test___new_panel___disconnect___raises_runtime_error(
40-
fake_python_panel_service: tuple[grpc.Server, int],
39+
fake_python_panel_service: tuple[FakePythonPanelServicer, int],
4140
) -> None:
4241
_, port = fake_python_panel_service
4342
panel = PortPanel(port, "my_panel", "path/to/script")
@@ -47,12 +46,12 @@ def test___new_panel___disconnect___raises_runtime_error(
4746

4847

4948
def test___first_connect_fails___connect___gets_value(
50-
fake_python_panel_service: tuple[grpc.Server, int],
49+
fake_python_panel_service: tuple[FakePythonPanelServicer, int],
5150
) -> None:
5251
"""Test that panel.connect() will automatically retry once."""
52+
servicer, port = fake_python_panel_service
5353
# Simulate a failure on the first connect attempt
54-
FakePythonPanelServicer.fail_next_connect()
55-
_, port = fake_python_panel_service
54+
servicer.fail_next_connect()
5655
panel = PortPanel(port, "my_panel", "path/to/script")
5756

5857
panel.connect()

tests/utils/_fake_python_panel_servicer.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class FakePythonPanelServicer(PythonPanelServiceServicer):
2121
_fail_next_connect = False
2222

2323
def Connect(self, request: ConnectRequest, context: Any) -> ConnectResponse: # noqa: N802
24-
"""Just a trivial implementation for testing."""
24+
"""Trivial implementation for testing."""
2525
if self._fail_next_connect:
2626
self._fail_next_connect = False
2727
raise ValueError("Simulate a failure to Connect.")
@@ -30,20 +30,19 @@ def Connect(self, request: ConnectRequest, context: Any) -> ConnectResponse: #
3030
def Disconnect( # noqa: N802
3131
self, request: DisconnectRequest, context: Any
3232
) -> DisconnectResponse:
33-
"""Just a trivial implementation for testing."""
33+
"""Trivial implementation for testing."""
3434
return DisconnectResponse()
3535

3636
def GetValue(self, request: GetValueRequest, context: Any) -> GetValueResponse: # noqa: N802
37-
"""Just a trivial implementation for testing."""
37+
"""Trivial implementation for testing."""
3838
value = self._values[request.value_id]
3939
return GetValueResponse(value=value)
4040

4141
def SetValue(self, request: SetValueRequest, context: Any) -> SetValueResponse: # noqa: N802
42-
"""Just a trivial implementation for testing."""
42+
"""Trivial implementation for testing."""
4343
self._values[request.value_id] = request.value
4444
return SetValueResponse()
4545

46-
@classmethod
47-
def fail_next_connect(cls) -> None:
46+
def fail_next_connect(self) -> None:
4847
"""Set whether the Connect method should fail the next time it is called."""
49-
cls._fail_next_connect = True
48+
self._fail_next_connect = True

0 commit comments

Comments
 (0)