From df20cf2838a9b262cfe1692017e7809c3c1c913a Mon Sep 17 00:00:00 2001 From: krishung5 Date: Wed, 7 Jun 2023 16:08:22 -0700 Subject: [PATCH] Add test case for metric lifetime error handling --- qa/python_models/custom_metrics/model.py | 28 ++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/qa/python_models/custom_metrics/model.py b/qa/python_models/custom_metrics/model.py index 219868c757..6645aa27c3 100644 --- a/qa/python_models/custom_metrics/model.py +++ b/qa/python_models/custom_metrics/model.py @@ -213,6 +213,34 @@ def test_dup_metric_empty_labels(self): # underlying metric, and all instances will be updated self._dup_metric_helper() + def test_metric_lifetime_error(self): + # Test the error handling when the corresponding 'MetricFamily' is + # deleted before the 'Metric' is deleted, and the 'Metric' is still + # being used for metric operations + metric_family = pb_utils.MetricFamily( + name="test_metric_lifetime_error", + description="test metric lifetime error", + kind=pb_utils.MetricFamily.COUNTER) + labels = {"example1": "counter_label1", "example2": "counter_label2"} + metric = metric_family.Metric(labels=labels) + + # Intentionally delete the 'MetricFamily' before the 'Metric' being deleted + del metric_family + + error_msg = "Invalid metric operation as the corresponding 'MetricFamily' has been deleted." + + with self.assertRaises(pb_utils.TritonModelException) as ex: + metric.set(10) + self.assertIn(error_msg, str(ex.exception)) + + with self.assertRaises(pb_utils.TritonModelException) as ex: + metric.increment(10) + self.assertIn(error_msg, str(ex.exception)) + + with self.assertRaises(pb_utils.TritonModelException) as ex: + metric.value() + self.assertIn(error_msg, str(ex.exception)) + class TritonPythonModel: