Skip to content

Commit dd4a3c2

Browse files
committed
refactor: use Arc<Mutex<...>> for SectionCapture
1 parent b6f2bc7 commit dd4a3c2

4 files changed

Lines changed: 32 additions & 20 deletions

File tree

crates/moon/src/cli/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ fn rr_run_from_plan(
738738

739739
let res = default_rt()
740740
.context("Failed to create runtime")?
741-
.block_on(crate::run::run(&mut [], false, run_cmd.command))
741+
.block_on(crate::run::run(&[], false, run_cmd.command))
742742
.context("failed to run command")?;
743743

744744
if let Some(code) = res.code() {

crates/moon/src/run/child.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
//! Handles spawning of a child process under the govern of `moon run`
2020
21-
use std::process::{ExitStatus, Stdio};
21+
use std::{
22+
process::{ExitStatus, Stdio},
23+
sync::Arc,
24+
};
2225

2326
use anyhow::Context;
2427
use moonbuild::section_capture::{SectionCapture, handle_stdout_async};
2528
use moonutil::platform::macos_with_sigchild_blocked;
26-
use tokio::process::Command;
29+
use tokio::{process::Command, sync::Mutex};
2730

2831
/// Run a command under the governing of `moon run`.
2932
///
@@ -39,7 +42,7 @@ use tokio::process::Command;
3942
/// output since the running process might not have any other method to interact
4043
/// with the host `moon` process.
4144
pub async fn run<'a>(
42-
captures: &mut [&mut SectionCapture<'a>],
45+
captures: &[Arc<Mutex<SectionCapture<'a>>>],
4346
capture: bool,
4447
mut cmd: Command,
4548
) -> anyhow::Result<ExitStatus> {

crates/moon/src/run/runtest.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ use moonutil::common::{
7979
MOON_COVERAGE_DELIMITER_BEGIN, MOON_COVERAGE_DELIMITER_END, MOON_TEST_DELIMITER_BEGIN,
8080
MOON_TEST_DELIMITER_END, MbtTestInfo, MooncGenTestInfo,
8181
};
82-
use tokio::runtime::Runtime;
82+
use tokio::{runtime::Runtime, sync::Mutex};
8383
use tracing::{debug, info, instrument, trace, warn};
8484

8585
use crate::{rr_build::BuildMeta, run::default_rt};
@@ -402,20 +402,18 @@ fn run_one_test_executable(
402402
test.executable,
403403
Some(&test_args),
404404
)?;
405-
let mut cov_cap = mk_coverage_capture();
406-
let mut test_cap = make_test_capture();
405+
let cov_cap = Arc::new(Mutex::new(mk_coverage_capture()));
406+
let test_cap = Arc::new(Mutex::new(make_test_capture()));
407407
if ctx.verbose {
408408
crate::rr_build::dry_print_command(cmd.command.as_std(), ctx.source_dir, true);
409409
}
410410
info!(package = %test_args.package, executable = %test.executable.display(), "launching test executable");
411411

412+
let captures = [Arc::clone(&cov_cap), Arc::clone(&test_cap)];
413+
412414
let exit_status = ctx
413415
.rt
414-
.block_on(crate::run::run(
415-
&mut [&mut cov_cap, &mut test_cap],
416-
true,
417-
cmd.command,
418-
))
416+
.block_on(crate::run::run(&captures, true, cmd.command))
419417
.with_context(|| format!("Failed to run test for {fqn} {:?}", test.target.kind))?;
420418
debug!(?exit_status, "test process finished");
421419

@@ -427,9 +425,17 @@ fn run_one_test_executable(
427425
);
428426
}
429427

430-
handle_finished_coverage(ctx.target_dir, cov_cap)?;
431-
432-
parse_test_results(meta, test_cap).with_context(|| {
428+
let cov_result = Arc::try_unwrap(cov_cap)
429+
.ok()
430+
.expect("cov_cap should have no other references")
431+
.into_inner();
432+
handle_finished_coverage(ctx.target_dir, cov_result)?;
433+
434+
let test_result = Arc::try_unwrap(test_cap)
435+
.ok()
436+
.expect("test_cap should have no other references")
437+
.into_inner();
438+
parse_test_results(meta, test_result).with_context(|| {
433439
format!(
434440
"Failed to parse test results for {fqn} {:?}",
435441
test.target.kind

crates/moonbuild/src/section_capture.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
//
1717
// For inquiries, you can contact us via e-mail at jichuruanjian@idea.edu.cn.
1818

19-
use std::io::BufRead;
19+
use std::{io::BufRead, sync::Arc};
2020

21-
use tokio::io::{AsyncBufRead, AsyncWriteExt};
21+
use tokio::{
22+
io::{AsyncBufRead, AsyncWriteExt},
23+
sync::Mutex,
24+
};
2225

2326
pub struct SectionCapture<'a> {
2427
begin_delimiter: &'a str,
@@ -121,7 +124,7 @@ pub fn handle_stdout<P: FnMut(&str)>(
121124
/// Async version of [`handle_stdout`].
122125
pub async fn handle_stdout_async<'a>(
123126
proc: impl AsyncBufRead,
124-
captures: &mut [&mut SectionCapture<'a>],
127+
captures: &[Arc<Mutex<SectionCapture<'a>>>],
125128
) -> anyhow::Result<()> {
126129
use tokio::io::AsyncBufReadExt;
127130
let mut buf = String::new();
@@ -136,8 +139,8 @@ pub async fn handle_stdout_async<'a>(
136139
break;
137140
}
138141
let capture_status = captures
139-
.iter_mut()
140-
.find_map(|capture| capture.feed_line(&buf));
142+
.iter()
143+
.find_map(|capture| capture.blocking_lock().feed_line(&buf));
141144
match capture_status {
142145
None => {
143146
stdout.write_all(buf.as_bytes()).await?;

0 commit comments

Comments
 (0)