Skip to content

Commit 8503c0c

Browse files
test: fix flaky tests with SubAgent client builder
1 parent 88ca4d5 commit 8503c0c

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

agent-control/src/opamp/client_builder.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ pub(crate) mod tests {
100100
Client, ClientResult, NotStartedClient, NotStartedClientResult, StartedClient,
101101
StartedClientResult,
102102
};
103+
use std::thread;
104+
use std::thread::sleep;
103105

104106
use super::*;
105107

@@ -220,5 +222,34 @@ pub(crate) mod tests {
220222
.once()
221223
.return_once(move |_, _, _| Ok(client));
222224
}
225+
226+
// This is a Mock OpAMP Client Builder, which builds the Callbacks and the OpAMP Client
227+
// and starts the OpAMP Client thread. This thread owns the callbacks, and these publish
228+
// into the OpAMP Publisher <-- Sub Agent OpAMP Consumer
229+
// Sub Agent OpAMP Consumer consumes the OpAMP events.
230+
// Using the Mock, makes the OpAMP publisher to be dropped, as it's part of the expectations
231+
// Until we refactor these tests (and find a better solution for this pattern) we'll
232+
// spawn a thread with the publisher, just not to be dropped in the test
233+
// TL;DR: Let the OpAMP Publisher leave for Duration
234+
pub fn should_build_and_start_and_run(
235+
&mut self,
236+
agent_id: AgentID,
237+
start_settings: StartSettings,
238+
client: MockStartedOpAMPClientMock<C>,
239+
run_for: Duration,
240+
) {
241+
self.expect_build_and_start()
242+
.withf(move |publisher, _sub_agent_id, _start_settings| {
243+
let publisher = publisher.clone();
244+
thread::spawn(move || {
245+
sleep(run_for);
246+
drop(publisher)
247+
});
248+
//
249+
agent_id == _sub_agent_id.clone() && start_settings == *_start_settings
250+
})
251+
.once()
252+
.return_once(move |_, _, _| Ok(client));
253+
}
223254
}
224255
}

agent-control/src/sub_agent/on_host/builder.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ mod tests {
240240
AgentDescription, DescriptionValueType, StartSettings,
241241
};
242242
use std::collections::HashMap;
243+
use std::thread;
244+
use std::thread::sleep;
245+
use std::time::Duration;
243246
use tracing_test::traced_test;
244247

245248
// TODO: tests below are testing not only the builder but also the sub-agent start/stop behavior.
@@ -277,10 +280,13 @@ mod tests {
277280
started_client.should_stop(1);
278281

279282
// Infra Agent OpAMP no final stop nor health, just after stopping on reload
280-
opamp_builder.should_build_and_start(
283+
// TODO: We should discussed if this is a valid approach once we refactor the unit tests
284+
// Build an OpAMP Client and let it run so the publisher is not dropped
285+
opamp_builder.should_build_and_start_and_run(
281286
sub_agent_id.clone(),
282287
start_settings_infra,
283288
started_client,
289+
Duration::from_millis(10),
284290
);
285291

286292
let mut instance_id_getter = MockInstanceIDGetterMock::new();
@@ -320,12 +326,15 @@ mod tests {
320326
Arc::new(signature_validator),
321327
);
322328

323-
on_host_builder
329+
let agent = on_host_builder
324330
.build(sub_agent_id, &sub_agent_config, opamp_publisher)
325331
.unwrap()
326-
.run()
327-
.stop()
328-
.unwrap();
332+
.run();
333+
334+
// Force OpAMP Publisher drop error
335+
// sleep(Duration::from_millis(30));
336+
337+
agent.stop().unwrap();
329338
}
330339

331340
#[traced_test]
@@ -375,10 +384,13 @@ mod tests {
375384
started_client.should_update_effective_config(1);
376385
started_client.should_stop(1);
377386

378-
opamp_builder.should_build_and_start(
387+
// TODO: We should discussed if this is a valid approach once we refactor the unit tests
388+
// Build an OpAMP Client and let it run so the publisher is not dropped
389+
opamp_builder.should_build_and_start_and_run(
379390
sub_agent_id.clone(),
380391
start_settings_infra,
381392
started_client,
393+
Duration::from_millis(10),
382394
);
383395

384396
effective_agent_assembler.should_assemble_agent(
@@ -412,6 +424,10 @@ mod tests {
412424
.build(sub_agent_id, &sub_agent_config, opamp_publisher)
413425
.expect("Subagent build should be OK");
414426
let started_sub_agent = sub_agent.run(); // Running the sub-agent should report the failed configuration.
427+
428+
// Force OpAMP Publisher drop error
429+
// sleep(Duration::from_millis(20));
430+
415431
started_sub_agent.stop().unwrap();
416432
}
417433

0 commit comments

Comments
 (0)