@@ -38,7 +38,7 @@ def get_health_file_contents(tmp_path):
3838 return contents
3939
4040
41- @pytest .fixture (scope = "module" , autouse = True )
41+ @pytest .fixture (autouse = True )
4242def restore_settings_fixture ():
4343 # Backup settings from before this test file runs
4444 original_settings = global_settings ()
@@ -51,6 +51,10 @@ def restore_settings_fixture():
5151 original_settings .__dict__ .clear ()
5252 original_settings .__dict__ .update (backup )
5353
54+ # Re-initialize the agent to restore the settings
55+ _reset_configuration_done ()
56+ initialize ()
57+
5458
5559@pytest .mark .parametrize ("file_uri" , ["" , "file://" , "/test/dir" , "foo:/test/dir" ])
5660def test_invalid_file_directory_supplied (monkeypatch , file_uri ):
@@ -155,10 +159,18 @@ def test_no_override_on_unhealthy_shutdown(monkeypatch, tmp_path):
155159
156160
157161def test_health_check_running_threads (monkeypatch , tmp_path ):
158- running_threads = threading .enumerate ()
159- # Only the main thread should be running since not agent control env vars are set
160- assert len (running_threads ) == 1
162+ # If the Activate-Session thread is still active, give it time to close before we proceed
163+ timeout = 30.0
164+ while len (threading .enumerate ()) != 1 and timeout > 0 :
165+ time .sleep (0.1 )
166+ timeout -= 0.1
161167
168+ # Only the main thread should be running since no agent control env vars are set
169+ assert len (threading .enumerate ()) == 1 , (
170+ f"Expected only the main thread to be running before the test starts. Got: { threading .enumerate ()} "
171+ )
172+
173+ # Setup expected env vars to run agent control health check
162174 monkeypatch .setenv ("NEW_RELIC_AGENT_CONTROL_ENABLED" , "True" )
163175 file_path = tmp_path .as_uri ()
164176 monkeypatch .setenv ("NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION" , file_path )
@@ -180,6 +192,7 @@ def test_proxy_error_status(monkeypatch, tmp_path):
180192 file_path = tmp_path .as_uri ()
181193 monkeypatch .setenv ("NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION" , file_path )
182194
195+ # Re-initialize the agent to allow the health check thread to start
183196 _reset_configuration_done ()
184197 initialize ()
185198
@@ -209,6 +222,7 @@ def test_multiple_activations_running_threads(monkeypatch, tmp_path):
209222 file_path = tmp_path .as_uri ()
210223 monkeypatch .setenv ("NEW_RELIC_AGENT_CONTROL_HEALTH_DELIVERY_LOCATION" , file_path )
211224
225+ # Re-initialize the agent to allow the health check thread to start and assert that it did
212226 _reset_configuration_done ()
213227 initialize ()
214228
0 commit comments