Skip to content

Chapter 07: Race condition in the tests #27

Description

@jclin

Hi,

Chapter 07's tests will intermittently fail because of how cargo test runs tests concurrently by default. Specifically, when unreadable_dir creates tests/inputs/cant-touch-this, other tests will unintentionally detect that dir and include it in its output.

As a workaround, the run function was modified to filter that directory out:

fn run(args: &[&str], expected_file: &str) -> Result<()> {
    let file = format_file_name(expected_file);
    let contents = fs::read_to_string(file.as_ref())?;
    let mut expected: Vec<&str> =
        contents.split('\n').filter(|s| !s.is_empty()).collect();
    expected.sort();

    let cmd = Command::cargo_bin(PRG)?.args(args).assert().success();
    let out = cmd.get_output();
    let stdout = String::from_utf8(out.stdout.clone())?;
    // unreadable_dir creates tests/inputs/cant-touch-this while tests run in
    // parallel; ignore it so tests scanning tests/inputs don't race with it.
    let mut lines: Vec<&str> = stdout
        .split('\n')
        .filter(|s| !s.is_empty() && !s.contains("cant-touch-this"))
        .collect();
    lines.sort();

    assert_eq!(lines, expected);

    Ok(())
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions