11# Copyright (c) Microsoft. All rights reserved.
22
33import multiprocessing
4+ import sys
45from typing import Any , Optional , Union
56
67import agentops
8+ import pytest
79from agentops .sdk .core import TraceContext
810from opentelemetry .trace .status import StatusCode
911
@@ -20,7 +22,8 @@ def _func_without_exception():
2022 pass
2123
2224
23- def test_trace_error_status_from_instance ():
25+ @pytest .mark .parametrize ("with_exception" , [True , False ])
26+ def test_trace_error_status_from_instance (with_exception : bool ):
2427 """
2528 Test that AgentOpsTracer correctly sets trace end state based on execution result.
2629
@@ -30,7 +33,7 @@ def test_trace_error_status_from_instance():
3033 """
3134
3235 ctx = multiprocessing .get_context ("spawn" )
33- proc = ctx .Process (target = _test_trace_error_status_from_instance_imp )
36+ proc = ctx .Process (target = _test_trace_error_status_from_instance_imp , args = ( with_exception ,) )
3437 proc .start ()
3538 proc .join (30.0 ) # On GPU server, the time is around 10 seconds.
3639
@@ -42,13 +45,19 @@ def test_trace_error_status_from_instance():
4245
4346 assert False , "Child process hung. Check test output for details."
4447
45- assert proc .exitcode == 0 , (
46- f"Child process for test_trace_error_status_from_instance failed with exit code { proc .exitcode } . "
47- "Check child traceback in test output."
48- )
48+ if with_exception :
49+ assert proc .exitcode != 0 , (
50+ f"Child process for test_trace_error_status_from_instance with exception exited with exit code { proc .exitcode } . "
51+ "Check child traceback in test output."
52+ )
53+ else :
54+ assert proc .exitcode == 0 , (
55+ f"Child process for test_trace_error_status_from_instance without exception failed with exit code { proc .exitcode } . "
56+ "Check child traceback in test output."
57+ )
4958
5059
51- def _test_trace_error_status_from_instance_imp ():
60+ def _test_trace_error_status_from_instance_imp (with_exception : bool ):
5261 captured_state = {}
5362 old_end_trace = agentops .end_trace
5463
@@ -65,12 +74,14 @@ def custom_end_trace(
6574 tracer .init_worker (0 )
6675
6776 try :
68- tracer .trace_run (_func_with_exception )
69- assert captured_state ["state" ] == StatusCode .ERROR
70-
71- tracer .trace_run (_func_without_exception )
72- assert captured_state ["state" ] == StatusCode .OK
73-
77+ if with_exception :
78+ tracer .trace_run (_func_with_exception )
79+ if captured_state ["state" ] != StatusCode .ERROR :
80+ sys .exit (- 1 )
81+ else :
82+ tracer .trace_run (_func_without_exception )
83+ if captured_state ["state" ] != StatusCode .OK :
84+ sys .exit (- 1 )
7485 finally :
7586 agentops .end_trace = old_end_trace
7687 tracer .teardown_worker (0 )
0 commit comments