Skip to content

Commit db6cb08

Browse files
test: fix flaky tests with SubAgent client builder (#1007)
1 parent 9f8178f commit db6cb08

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

agent-control/src/opamp/client_builder.rs

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

104105
use super::*;
105106

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

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ mod tests {
240240
AgentDescription, DescriptionValueType, StartSettings,
241241
};
242242
use std::collections::HashMap;
243+
use std::time::Duration;
243244
use tracing_test::traced_test;
244245

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

279280
// Infra Agent OpAMP no final stop nor health, just after stopping on reload
280-
opamp_builder.should_build_and_start(
281+
// TODO: We should discuss if this is a valid approach once we refactor the unit tests
282+
// Build an OpAMP Client and let it run so the publisher is not dropped
283+
opamp_builder.should_build_and_start_and_run(
281284
sub_agent_id.clone(),
282285
start_settings_infra,
283286
started_client,
287+
Duration::from_millis(10),
284288
);
285289

286290
let mut instance_id_getter = MockInstanceIDGetterMock::new();
@@ -375,10 +379,13 @@ mod tests {
375379
started_client.should_update_effective_config(1);
376380
started_client.should_stop(1);
377381

378-
opamp_builder.should_build_and_start(
382+
// TODO: We should discuss if this is a valid approach once we refactor the unit tests
383+
// Build an OpAMP Client and let it run so the publisher is not dropped
384+
opamp_builder.should_build_and_start_and_run(
379385
sub_agent_id.clone(),
380386
start_settings_infra,
381387
started_client,
388+
Duration::from_millis(10),
382389
);
383390

384391
effective_agent_assembler.should_assemble_agent(

0 commit comments

Comments
 (0)