Skip to content

Commit

Permalink
Update Measurement Plug-In Generated Client to Resolve with the Corre…
Browse files Browse the repository at this point in the history
…sponding Measurement Service Version (#925)

* fix: update resolve service api and its dependencies

* fix: formatting

* fix: update resolve service

* fix: add deployment_target

* fix: update version of service

* fix: update version and revert the unwanted changes

* fix: revert unwanted change

* fix: update poetry

* fix: formatting

* fix: update version of test measurements in service config

(cherry picked from commit 90d3ab7)
  • Loading branch information
Jotheeswaran-Nandagopal committed Sep 25, 2024
1 parent 95afba8 commit 324e9c5
Show file tree
Hide file tree
Showing 18 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def _create_client(
directory_out=directory_out,
class_name=class_name,
display_name=metadata.measurement_details.display_name,
version=metadata.measurement_details.version,
configuration_metadata=configuration_metadata,
output_metadata=output_metadata,
service_class=measurement_service_class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import re
from typing import Any
%>\
\
<%page args="class_name, display_name, configuration_metadata, output_metadata, service_class, configuration_parameters_with_type_and_default_values, measure_api_parameters, output_parameters_with_type, built_in_import_modules, custom_import_modules, enum_by_class_name, configuration_parameters_type_url"/>\
<%page args="class_name, display_name, version, configuration_metadata, output_metadata, service_class, configuration_parameters_with_type_and_default_values, measure_api_parameters, output_parameters_with_type, built_in_import_modules, custom_import_modules, enum_by_class_name, configuration_parameters_type_url"/>\
\
<%
def _format_default_value(value: Any) -> Any:
Expand Down Expand Up @@ -107,6 +107,7 @@ class ${class_name}:
"""
self._initialization_lock = threading.RLock()
self._service_class = ${service_class | repr}
self._version = ${version | repr}
self._grpc_channel_pool = grpc_channel_pool
self._discovery_client = discovery_client
self._pin_map_client = pin_map_client
Expand Down Expand Up @@ -188,6 +189,7 @@ class ${class_name}:
service_location = self._get_discovery_client().resolve_service(
provided_interface=_V2_MEASUREMENT_SERVICE_INTERFACE,
service_class=self._service_class,
version=self._version,
)
channel = self._get_grpc_channel_pool().get_channel(service_location.insecure_address)
self._stub = v2_measurement_service_pb2_grpc.MeasurementServiceStub(channel)
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/generator/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ni_measurement_plugin_sdk_generator"
version = "2.1.0-dev1"
version = "2.1.0-dev2"
description = "Measurement Plugin Code Generator for Python"
authors = ["NI <[email protected]>"]
readme = "README.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def __init__(
"""
self._initialization_lock = threading.RLock()
self._service_class = "ni.tests.NonStreamingDataMeasurement_Python"
self._version = "0.1.0.0"
self._grpc_channel_pool = grpc_channel_pool
self._discovery_client = discovery_client
self._pin_map_client = pin_map_client
Expand Down Expand Up @@ -458,6 +459,7 @@ def _get_stub(self) -> v2_measurement_service_pb2_grpc.MeasurementServiceStub:
service_location = self._get_discovery_client().resolve_service(
provided_interface=_V2_MEASUREMENT_SERVICE_INTERFACE,
service_class=self._service_class,
version=self._version,
)
channel = self._get_grpc_channel_pool().get_channel(
service_location.insecure_address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(
"""
self._initialization_lock = threading.RLock()
self._service_class = "ni.tests.VoidMeasurement_Python"
self._version = "0.1.0.0"
self._grpc_channel_pool = grpc_channel_pool
self._discovery_client = discovery_client
self._pin_map_client = pin_map_client
Expand Down Expand Up @@ -111,6 +112,7 @@ def _get_stub(self) -> v2_measurement_service_pb2_grpc.MeasurementServiceStub:
service_location = self._get_discovery_client().resolve_service(
provided_interface=_V2_MEASUREMENT_SERVICE_INTERFACE,
service_class=self._service_class,
version=self._version,
)
channel = self._get_grpc_channel_pool().get_channel(
service_location.insecure_address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"services": [
{
"displayName": "Non-Streaming Data Measurement (Py)",
"version": "0.1.0.0",
"serviceClass": "ni.tests.NonStreamingDataMeasurement_Python",
"descriptionUrl": "",
"providedInterfaces": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
service_directory = Path(__file__).resolve().parent
measurement_service = nims.MeasurementService(
service_config_path=service_directory / "NonStreamingDataMeasurement.serviceconfig",
version="0.1.0.0",
ui_file_paths=[
service_directory,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"services": [
{
"displayName": "Pin Aware Measurement (Py)",
"version": "0.1.0.0",
"serviceClass": "ni.tests.PinAwareMeasurement_Python",
"descriptionUrl": "",
"providedInterfaces": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
service_directory = pathlib.Path(__file__).resolve().parent
measurement_service = nims.MeasurementService(
service_config_path=service_directory / "PinAwareMeasurement.serviceconfig",
version="0.1.0.0",
ui_file_paths=[
service_directory,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"services": [
{
"displayName": "Streaming Data Measurement (Py)",
"version": "0.1.0.0",
"serviceClass": "ni.tests.StreamingDataMeasurement_Python",
"descriptionUrl": "",
"providedInterfaces": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
service_directory = pathlib.Path(__file__).resolve().parent
measurement_service = nims.MeasurementService(
service_config_path=service_directory / "StreamingDataMeasurement.serviceconfig",
version="0.1.0.0",
ui_file_paths=[
service_directory,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"services": [
{
"displayName": "Void Measurement (Py)",
"version": "0.1.0.0",
"serviceClass": "ni.tests.VoidMeasurement_Python",
"descriptionUrl": "",
"providedInterfaces": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
service_directory = Path(__file__).resolve().parent
measurement_service = nims.MeasurementService(
service_config_path=service_directory / "VoidMeasurement.serviceconfig",
version="0.1.0.0",
ui_file_paths=[
service_directory,
],
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ni_measurement_plugin_sdk"
version = "2.1.0-dev1"
version = "2.1.0-dev2"
description = "Measurement Plugin SDK for Python"
authors = ["NI <[email protected]>"]
readme = "README.md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,13 @@ def unregister_service(self, registration_id: str = "") -> bool:
_logger.exception("Error in unregistering with discovery service.")
raise

def resolve_service(self, provided_interface: str, service_class: str = "") -> ServiceLocation:
def resolve_service(
self,
provided_interface: str,
service_class: str = "",
deployment_target: str = "",
version: str = "",
) -> ServiceLocation:
"""Resolve the location of a service.
Given a description of a service, returns information that can be used to establish
Expand All @@ -218,12 +224,18 @@ def resolve_service(self, provided_interface: str, service_class: str = "") -> S
service_class: The service "class" that should be matched. If the value is not
specified and there is more than one matching service registered, an error
is returned.
deployment_target: The deployment target from which the service should be resolved.
version: The version of the service to resolve. If not specified, the latest version
will be resolved.
Returns:
The service location.
"""
request = discovery_service_pb2.ResolveServiceRequest(
provided_interface=provided_interface, service_class=service_class
provided_interface=provided_interface,
service_class=service_class,
deployment_target=deployment_target,
version=version,
)

response = self._get_stub().ResolveService(request)
Expand Down
2 changes: 1 addition & 1 deletion packages/service/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extend_exclude = '.tox/,*_pb2_grpc.py,*_pb2_grpc.pyi,*_pb2.py,*_pb2.pyi,ni_measu

[tool.poetry]
name = "ni_measurement_plugin_sdk_service"
version = "2.1.0-dev1"
version = "2.1.0-dev2"
description = "Measurement Plugin Support for Python"
authors = ["NI <[email protected]>"]
readme = "README.md" # apply the repo readme to the package as well
Expand Down
25 changes: 24 additions & 1 deletion packages/service/tests/unit/test_discovery_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test___service_registered___unregister_service___sends_request(
assert unregistration_success_flag


def test___service_registered___resolve_service___sends_request(
def test___service_registered___resolve_service_without_version___sends_request(
discovery_client: DiscoveryClient, discovery_service_stub: Mock
):
discovery_service_stub.ResolveService.return_value = GrpcServiceLocation(
Expand All @@ -138,6 +138,29 @@ def test___service_registered___resolve_service___sends_request(
_assert_service_location_equal(_TEST_SERVICE_LOCATION, service_location)


def test___service_registered___resolve_service_with_version___sends_request(
discovery_client: DiscoveryClient, discovery_service_stub: Mock
):
discovery_service_stub.ResolveService.return_value = GrpcServiceLocation(
location=_TEST_SERVICE_LOCATION.location,
insecure_port=_TEST_SERVICE_LOCATION.insecure_port,
ssl_authenticated_port=_TEST_SERVICE_LOCATION.ssl_authenticated_port,
)

service_location = discovery_client.resolve_service(
provided_interface=_TEST_SERVICE_INFO.provided_interfaces[0],
service_class=_TEST_SERVICE_INFO.service_class,
version=_TEST_SERVICE_INFO.versions[0],
)

discovery_service_stub.ResolveService.assert_called_once()
request: ResolveServiceRequest = discovery_service_stub.ResolveService.call_args.args[0]
assert _TEST_SERVICE_INFO.provided_interfaces[0] == request.provided_interface
assert _TEST_SERVICE_INFO.service_class == request.service_class
assert _TEST_SERVICE_INFO.versions[0] == request.version
_assert_service_location_equal(_TEST_SERVICE_LOCATION, service_location)


def test___service_not_registered___resolve_service___raises_not_found_error(
discovery_client: DiscoveryClient, discovery_service_stub: Mock
):
Expand Down

0 comments on commit 324e9c5

Please sign in to comment.