1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0
use super::get_num_cpus;
use serde::Deserialize;
use std::{cmp::Ordering, fmt};
/// Type for the threads-required config key.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ThreadsRequired {
/// Take up "slots" equal to the number of threads.
Count(usize),
/// Take up as many slots as the number of CPUs.
NumCpus,
/// Take up as many slots as the number of test threads specified.
NumTestThreads,
}
impl ThreadsRequired {
/// Gets the actual number of test threads computed at runtime.
pub fn compute(self, test_threads: usize) -> usize {
match self {
Self::Count(threads) => threads,
Self::NumCpus => get_num_cpus(),
Self::NumTestThreads => test_threads,
}
}
}
impl<'de> Deserialize<'de> for ThreadsRequired {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct V;
impl<'de2> serde::de::Visitor<'de2> for V {
type Value = ThreadsRequired;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"an integer, the string \"num-cpus\" or the string \"num-test-threads\""
)
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if v == "num-cpus" {
Ok(ThreadsRequired::NumCpus)
} else if v == "num-test-threads" {
Ok(ThreadsRequired::NumTestThreads)
} else {
Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(v),
&self,
))
}
}
// Note that TOML uses i64, not u64.
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v.cmp(&0) {
Ordering::Greater => Ok(ThreadsRequired::Count(v as usize)),
// TODO: we don't currently support negative numbers here because it's not clear
// whether num-cpus or num-test-threads is better. It would probably be better
// to support a small expression syntax with +, -, * and /.
//
// I (Rain) checked out a number of the expression syntax crates and found that they
// either support too much or too little. We want just this minimal set of operators,
// plus. Probably worth just forking https://docs.rs/mexe or working with upstream
// to add support for operators.
Ordering::Equal | Ordering::Less => Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Signed(v),
&self,
)),
}
}
}
deserializer.deserialize_any(V)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::{test_helpers::*, NextestConfig};
use camino_tempfile::tempdir;
use indoc::indoc;
use test_case::test_case;
#[test_case(
indoc! {r#"
[profile.custom]
threads-required = 2
"#},
Some(2)
; "positive"
)]
#[test_case(
indoc! {r#"
[profile.custom]
threads-required = 0
"#},
None
; "zero"
)]
#[test_case(
indoc! {r#"
[profile.custom]
threads-required = -1
"#},
None
; "negative"
)]
#[test_case(
indoc! {r#"
[profile.custom]
threads-required = "num-cpus"
"#},
Some(get_num_cpus())
; "num-cpus"
)]
#[test_case(
indoc! {r#"
[profile.custom]
test-threads = 1
threads-required = "num-cpus"
"#},
Some(get_num_cpus())
; "num-cpus-with-custom-test-threads"
)]
#[test_case(
indoc! {r#"
[profile.custom]
threads-required = "num-test-threads"
"#},
Some(get_num_cpus())
; "num-test-threads"
)]
#[test_case(
indoc! {r#"
[profile.custom]
test-threads = 1
threads-required = "num-test-threads"
"#},
Some(1)
; "num-test-threads-with-custom-test-threads"
)]
fn parse_threads_required(config_contents: &str, threads_required: Option<usize>) {
let workspace_dir = tempdir().unwrap();
let graph = temp_workspace(workspace_dir.path(), config_contents);
let config = NextestConfig::from_sources(
graph.workspace().root(),
&graph,
None,
[],
&Default::default(),
);
match threads_required {
None => assert!(config.is_err()),
Some(t) => {
let config = config.unwrap();
let profile = config
.profile("custom")
.unwrap()
.apply_build_platforms(&build_platforms());
let test_threads = profile.test_threads().compute();
let threads_required = profile.threads_required().compute(test_threads);
assert_eq!(threads_required, t)
}
}
}
}