@@ -2,8 +2,10 @@ use std::{fs, io, path::Path, time::Duration};
22
33use crate :: {
44 common:: {
5- agent_control:: start_agent_control_with_custom_config, base_paths:: TempBasePaths ,
6- retry:: retry, runtime:: tokio_runtime,
5+ agent_control:: { StartedAgentControl , start_agent_control_with_custom_config} ,
6+ base_paths:: TempBasePaths ,
7+ retry:: retry,
8+ runtime:: tokio_runtime,
79 } ,
810 on_host:: tools:: {
911 config:: { OnHostAgentControlConfigBuilder , create_file, create_local_config} ,
@@ -123,7 +125,7 @@ fn run_file_logging_scenario(
123125 initial_message : & str ,
124126 reload_file_logging : bool ,
125127 reload_message : & str ,
126- ) -> TempBasePaths {
128+ ) -> ( TempBasePaths , StartedAgentControl ) {
127129 let mut opamp_server = FakeServer :: start ( tokio_runtime ( ) . handle ( ) ) ;
128130
129131 let dirs = TempBasePaths :: default ( ) ;
@@ -154,7 +156,7 @@ fn run_file_logging_scenario(
154156 dirs. local_dir ( ) ,
155157 ) ;
156158
157- let _agent_control =
159+ let agent_control =
158160 start_agent_control_with_custom_config ( dirs. base_paths ( ) , AGENT_CONTROL_MODE_ON_HOST ) ;
159161
160162 let sub_agent_instance_id =
@@ -172,7 +174,8 @@ fn run_file_logging_scenario(
172174 // Give some time for the echo output to be captured in the log files
173175 std:: thread:: sleep ( Duration :: from_secs ( 5 ) ) ;
174176
175- dirs
177+ // Return the handle so AC keeps running while the caller waits for the post-reload logs.
178+ ( dirs, agent_control)
176179}
177180
178181/// File logging enable/disable combinations with before and after reload checks
@@ -188,7 +191,8 @@ fn test_file_logging_reload(
188191) {
189192 let agent_id = format ! ( "file-logging-agent-{first_run_enabled}-{second_run_enabled}" ) ;
190193
191- let dirs = run_file_logging_scenario (
194+ // Keep the handle alive so AC keeps running during the wait below.
195+ let ( dirs, _agent_control) = run_file_logging_scenario (
192196 & agent_id,
193197 first_run_enabled,
194198 first_run_message,
@@ -197,24 +201,26 @@ fn test_file_logging_reload(
197201 ) ;
198202
199203 let log_dir_path = dirs. log_dir ( ) ;
200- let agent_logs_dir = log_dir_path. join ( & agent_id) ;
201- assert ! (
202- agent_logs_dir. exists( ) ,
203- "Log directory {agent_logs_dir:?} does not exist"
204- ) ;
205204
205+ // Wait (AC still running) until the logs match the expected end state after the reload.
206206 let all_contents = retry ( 60 , Duration :: from_secs ( 1 ) , || {
207- Ok ( collect_stdout_logs ( & log_dir_path, & agent_id) ?)
207+ let contents = collect_stdout_logs ( & log_dir_path, & agent_id) ?;
208+ if first_run_enabled == contents. contains ( first_run_message)
209+ && second_run_enabled == contents. contains ( second_run_message)
210+ {
211+ Ok ( contents)
212+ } else {
213+ Err ( format ! ( "log state not settled yet. Log contents: {contents}" ) . into ( ) )
214+ }
208215 } ) ;
209216
210- // If the logs are enabled for the run the string must be found, same for disabled and not found
211217 assert ! (
212218 first_run_enabled == all_contents. contains( first_run_message) ,
213- "First run log not found (pre-reload). Log contents: {all_contents}"
219+ "First run log mismatch (pre-reload). Log contents: {all_contents}"
214220 ) ;
215221 assert ! (
216222 second_run_enabled == all_contents. contains( second_run_message) ,
217- "Second run log not found (post-reload). Log contents: {all_contents}"
223+ "Second run log mismatch (post-reload). Log contents: {all_contents}"
218224 ) ;
219225}
220226
@@ -226,7 +232,8 @@ fn onhost_supervisor_reloading_keeps_file_logging_disabled() {
226232 let unique_str_2 = "keeps_disabled_run2" ;
227233 let agent_id = "test-agent-logs-always-disabled" ;
228234
229- let dirs = run_file_logging_scenario ( agent_id, false , unique_str_1, false , unique_str_2) ;
235+ let ( dirs, _agent_control) =
236+ run_file_logging_scenario ( agent_id, false , unique_str_1, false , unique_str_2) ;
230237
231238 let log_dir_path = dirs. log_dir ( ) ;
232239 let agent_logs_dir = log_dir_path. join ( agent_id) ;
0 commit comments