Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in measurement register with discovery service #353

Merged
merged 49 commits into from
Aug 24, 2023

Conversation

MounikaBattu17
Copy link
Contributor

@MounikaBattu17 MounikaBattu17 commented Aug 16, 2023

What does this Pull Request accomplish?

This PR stops measurement service from starting if the measurement is not successfully registered with discovery service.

Currently, On a unsuccessful measurement register with discovery service, the exceptions are just logged and service continues to run. So to improve the error handling(not starting the measurement service on unsuccessful register), made the following changes:

  1. Raised exceptions instead of returning False in register_measurement_service and unregister_service from discovery_client.py.
  2. Propagated the raised exceptions to callers service_manager.py, service.py.

Tests Updates:

  1. Updated unit/test_service.py to cover the exception scenario of host_service method.
  2. Updated integration/test_service_manager.py to cover the exception scenario of start method.
    • Added a new fixture expect_discovery_service_error_stub return a boolean to choose from FakeDiscoveryServiceStub or FakeDiscoveryServiceStubError. By default, Return value False such that choosing FakeDiscoveryServiceStub for all test cases
    • Overridden the value to True to pick FakeDiscoveryServiceStubError for the one test cases testing exception scenario.
  3. Updated test cases assert in unit/test_discovery_client.py.

Why should this Pull Request be merged?

This PR stops measurement service from starting if the measurement is not successfully registered with discovery service by raising an exception.

What testing has been done?

  1. Added new tests to cover the exception scenarios and verified existing and new tests pass by running poetry run pytest.
  2. Ran sample measurement example and verified the below scenarios,
  • Sample measurement successfully registered with discovery service -> Able to run the measurement without any error
  • Sample measurement not successfully registered with discovery service (removed measurementlink json file) -> measurement is not started and throwed an error
Traceback (most recent call last):
  File "measurementlink-python\ni_measurementlink_service\_internal\discovery_client.py", line 125, in register_measurement_service
    register_response = self.stub.RegisterService(request)
  File "measurementlink-python\ni_measurementlink_service\_internal\discovery_client.py", line 84, in stub
    address = _get_discovery_service_address()
  File "measurementlink-python\ni_measurementlink_service\_internal\discovery_client.py", line 220, in _get_discovery_service_address
    _ensure_discovery_service_started(key_file_path)
  File "measurementlink-python\ni_measurementlink_service\_internal\discovery_client.py", line 232, in _ensure_discovery_service_started
    exe_file_path = _get_discovery_service_location()
  File "measurementlink-python\ni_measurementlink_service\_internal\discovery_client.py", line 240, in _get_discovery_service_location
    registration_json_obj = json.loads(registration_json_path.read_text())
  File "Python\Python38\lib\pathlib.py", line 1236, in read_text        
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
  File "Python\Python38\lib\pathlib.py", line 1222, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "Python\Python38\lib\pathlib.py", line 1078, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Program Files\\National Instruments\\Shared\\MeasurementLink\\MeasurementLinkService.json'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/measurementlink-python/examples/sample_measurement/measurement.py", line 113, in <module>
    main()
  File "measurementlink-python\examples\sample_measurement\.venv\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "measurementlink-python\examples\sample_measurement\.venv\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "measurementlink-python\examples\sample_measurement\.venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "measurementlink-python\examples\sample_measurement\.venv\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/measurementlink-python/examples/sample_measurement/measurement.py", line 108, in main
    with sample_measurement_service.host_service():
  File "measurementlink-python\ni_measurementlink_service\measurement\service.py", line 388, in host_service
    raise e
  File "measurementlink-python\ni_measurementlink_service\measurement\service.py", line 379, in host_service
    self.grpc_service.start(
  File "measurementlink-python\ni_measurementlink_service\_internal\service_manager.py", line 111, in start
    raise e
  File "measurementlink-python\ni_measurementlink_service\_internal\service_manager.py", line 105, in start
    self.discovery_client.register_measurement_service(port, service_info, measurement_info)
  File "measurementlink-python\ni_measurementlink_service\_internal\discovery_client.py", line 140, in register_measurement_service
    raise FileNotFoundError(e.strerror, e.filename) from e
FileNotFoundError: [Errno No such file or directory] C:\Program Files\National Instruments\Shared\MeasurementLink\MeasurementLinkService.json
(sample-measurement-py3.8) PS measurementlink-python> 

@MounikaBattu17 MounikaBattu17 requested a review from csjall as a code owner August 16, 2023 12:26
Copy link

@vigkre vigkre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with suggestions

@MounikaBattu17 MounikaBattu17 requested a review from bkeryan August 21, 2023 18:37
@MounikaBattu17 MounikaBattu17 merged commit 703f137 into main Aug 24, 2023
@dixonjoel dixonjoel deleted the users/mounika/improve-error-handling branch September 21, 2023 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Service registration errors are not reported to the calling process via exit code
6 participants