Skip to content

Commit

Permalink
refactor scopes for step + measurement series
Browse files Browse the repository at this point in the history
- similar to previous commit, this removes the dependency on boxed
  futures; remove the cargo dependency as well, and the feature flag

Signed-off-by: mimir-d <[email protected]>
  • Loading branch information
mimir-d committed Oct 12, 2024
1 parent 2db3e47 commit 53a6e23
Show file tree
Hide file tree
Showing 22 changed files with 372 additions and 418 deletions.
36 changes: 0 additions & 36 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ repository = "https://github.com/opencomputeproject/ocp-diag-core-rust"
license = "MIT"
edition = "2021"

[features]
boxed-scopes = []

[dependencies]
async-trait = "0.1.83"
chrono = "0.4.38"
chrono-tz = "0.10.0"
delegate = "0.13.1"
futures = "0.3.30"
maplit = "1.0.2"
mime = "0.3.17"
serde = { version = "1.0.210", features = ["derive"] }
Expand Down Expand Up @@ -47,35 +43,3 @@ rand = "0.8.5"
unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(coverage,coverage_nightly)',
] }

[[example]]
name = "extensions"
required-features = ["boxed-scopes"]

[[example]]
name = "measurement_series"
required-features = ["boxed-scopes"]

[[example]]
name = "measurement_single"
required-features = ["boxed-scopes"]

[[example]]
name = "measurement_subcomponent"
required-features = ["boxed-scopes"]

[[example]]
name = "measurement_validators"
required-features = ["boxed-scopes"]

[[example]]
name = "simple_step_fail"
required-features = ["boxed-scopes"]

[[example]]
name = "diagnosis"
required-features = ["boxed-scopes"]

[[example]]
name = "file"
required-features = ["boxed-scopes"]
15 changes: 4 additions & 11 deletions examples/diagnosis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// https://opensource.org/licenses/MIT.

use anyhow::Result;
use futures::FutureExt;

use ocptv::output as tv;
use ocptv::{ocptv_diagnosis_fail, ocptv_diagnosis_pass};
Expand All @@ -17,7 +16,7 @@ fn get_fan_speed() -> i32 {
rng.gen_range(1500..1700)
}

async fn run_diagnosis_step(step: &tv::StartedTestStep) -> Result<TestStatus, tv::OcptvError> {
async fn run_diagnosis_step(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
let fan_speed = get_fan_speed();

if fan_speed >= 1600 {
Expand All @@ -31,9 +30,7 @@ async fn run_diagnosis_step(step: &tv::StartedTestStep) -> Result<TestStatus, tv
Ok(TestStatus::Complete)
}

async fn run_diagnosis_macros_step(
step: &tv::StartedTestStep,
) -> Result<TestStatus, tv::OcptvError> {
async fn run_diagnosis_macros_step(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
let fan_speed = get_fan_speed();

if fan_speed >= 1600 {
Expand All @@ -53,12 +50,8 @@ async fn main() -> Result<()> {
tv::TestRun::builder("simple measurement", "1.0")
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(|s| run_diagnosis_step(s).boxed())
.await?;
r.add_step("step1")
.scope(|s| run_diagnosis_macros_step(s).boxed())
.await?;
r.add_step("step0").scope(run_diagnosis_step).await?;
r.add_step("step1").scope(run_diagnosis_macros_step).await?;

Ok(tv::TestRunOutcome {
status: TestStatus::Complete,
Expand Down
1 change: 0 additions & 1 deletion examples/error_with_dut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ async fn main() -> Result<()> {
.build(),
);

#[cfg(feature = "boxed-scopes")]
tv::TestRun::builder("run error with dut", "1.0")
.build()
.scope(dut, |r| async move {
Expand Down
5 changes: 2 additions & 3 deletions examples/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// https://opensource.org/licenses/MIT.

use anyhow::Result;
use futures::FutureExt;
use serde::Serialize;

use ocptv::output as tv;
Expand All @@ -25,7 +24,7 @@ struct ComplexExtension {
subtypes: Vec<u32>,
}

async fn step0(s: &tv::StartedTestStep) -> Result<TestStatus, tv::OcptvError> {
async fn step0(s: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
s.add_extension("simple", "extension_identifier").await?;

s.add_extension(
Expand All @@ -49,7 +48,7 @@ async fn main() -> Result<()> {
tv::TestRun::builder("extensions", "1.0")
.build()
.scope(dut, |r| async move {
r.add_step("step0").scope(|s| step0(s).boxed()).await?;
r.add_step("step0").scope(step0).await?;

Ok(tv::TestRunOutcome {
status: TestStatus::Complete,
Expand Down
5 changes: 2 additions & 3 deletions examples/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use std::str::FromStr;

use anyhow::Result;
use futures::FutureExt;

use ocptv::output as tv;
use tv::{TestResult, TestStatus};

async fn run_file_step(step: &tv::StartedTestStep) -> Result<TestStatus, tv::OcptvError> {
async fn run_file_step(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
let uri = tv::Uri::from_str("file:///root/mem_cfg_log").unwrap();
step.add_file("mem_cfg_log", uri).await?;

Expand All @@ -28,7 +27,7 @@ async fn main() -> Result<()> {
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(|s| run_file_step(s).boxed())
.scope(run_file_step)
.await?;

Ok(tv::TestRunOutcome {
Expand Down
16 changes: 6 additions & 10 deletions examples/measurement_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

use anyhow::Result;
use chrono::Duration;
use futures::FutureExt;

use ocptv::output::{self as tv};
use tv::{TestResult, TestStatus};

async fn step0_measurements(step: &tv::StartedTestStep) -> Result<TestStatus, tv::OcptvError> {
async fn step0_measurements(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
let fan_speed = step
.add_measurement_series_detail(
tv::MeasurementSeriesDetail::builder("fan_speed")
Expand All @@ -30,8 +29,7 @@ async fn step0_measurements(step: &tv::StartedTestStep) -> Result<TestStatus, tv
Ok(TestStatus::Complete)
}

#[cfg(feature = "boxed-scopes")]
async fn step1_measurements(step: &tv::StartedTestStep) -> Result<TestStatus, tv::OcptvError> {
async fn step1_measurements(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
step.add_measurement_series_detail(
tv::MeasurementSeriesDetail::builder("temp0")
.unit("C")
Expand All @@ -51,14 +49,13 @@ async fn step1_measurements(step: &tv::StartedTestStep) -> Result<TestStatus, tv
s.add_measurement(43.into()).await?;
Ok(())
}
.boxed()
})
.await?;

Ok(TestStatus::Complete)
}

async fn step2_measurements(step: &tv::StartedTestStep) -> Result<TestStatus, tv::OcptvError> {
async fn step2_measurements(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
let freq0 = step
.add_measurement_series_detail(
tv::MeasurementSeriesDetail::builder("freq0")
Expand Down Expand Up @@ -99,16 +96,15 @@ async fn main() -> Result<()> {
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(|s| step0_measurements(s).boxed())
.scope(step0_measurements)
.await?;

#[cfg(feature = "boxed-scopes")]
r.add_step("step1")
.scope(|s| step1_measurements(s).boxed())
.scope(step1_measurements)
.await?;

r.add_step("step2")
.scope(|s| step2_measurements(s).boxed())
.scope(step2_measurements)
.await?;

Ok(tv::TestRunOutcome {
Expand Down
5 changes: 2 additions & 3 deletions examples/measurement_single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
// https://opensource.org/licenses/MIT.

use anyhow::Result;
use futures::FutureExt;

use ocptv::output as tv;
use tv::{TestResult, TestStatus};

async fn run_measure_step(step: &tv::StartedTestStep) -> Result<TestStatus, tv::OcptvError> {
async fn run_measure_step(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
step.add_measurement("temperature", 42.5.into()).await?;
step.add_measurement_detail(
tv::Measurement::builder("fan_speed", 1200.into())
Expand All @@ -31,7 +30,7 @@ async fn main() -> Result<()> {
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(|s| run_measure_step(s).boxed())
.scope(run_measure_step)
.await?;

Ok(tv::TestRunOutcome {
Expand Down
14 changes: 5 additions & 9 deletions examples/measurement_subcomponent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

use anyhow::Result;

use futures::FutureExt;
use ocptv::output as tv;
use tv::{SubcomponentType, TestResult, TestStatus};

async fn run_measure_step(
step: &tv::StartedTestStep,
step: tv::ScopedTestStep,
ram0: tv::DutHardwareInfo,
) -> Result<TestStatus, tv::OcptvError> {
step.add_measurement_detail(
Expand Down Expand Up @@ -40,13 +39,10 @@ async fn run_measure_step(
);

chip1_temp
.scope(|s| {
async move {
s.add_measurement(79.into()).await?;
.scope(|s| async move {
s.add_measurement(79.into()).await?;

Ok(())
}
.boxed()
Ok(())
})
.await?;

Expand Down Expand Up @@ -90,7 +86,7 @@ async fn main() -> Result<()> {
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(|s| run_measure_step(s, ram0).boxed())
.scope(|s| run_measure_step(s, ram0))
.await?;

Ok(tv::TestRunOutcome {
Expand Down
16 changes: 5 additions & 11 deletions examples/measurement_validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
// #![allow(warnings)]

use anyhow::Result;
use futures::FutureExt;

use ocptv::output as tv;
use tv::{TestResult, TestStatus, ValidatorType};

async fn run_measure_step(step: &tv::StartedTestStep) -> Result<TestStatus, tv::OcptvError> {
async fn run_measure_step(step: tv::ScopedTestStep) -> Result<TestStatus, tv::OcptvError> {
step.add_measurement_detail(
tv::Measurement::builder("temp", 40.into())
.add_validator(
Expand All @@ -31,13 +30,10 @@ async fn run_measure_step(step: &tv::StartedTestStep) -> Result<TestStatus, tv::
)
.build(),
)
.scope(|s| {
async move {
s.add_measurement(1000.into()).await?;
.scope(|s| async move {
s.add_measurement(1000.into()).await?;

Ok(())
}
.boxed()
Ok(())
})
.await?;

Expand All @@ -60,9 +56,7 @@ async fn main() -> Result<()> {
tv::TestRun::builder("simple measurement", "1.0")
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(|s| run_measure_step(s).boxed())
.await?;
r.add_step("step0").scope(run_measure_step).await?;

Ok(tv::TestRunOutcome {
status: TestStatus::Complete,
Expand Down
12 changes: 4 additions & 8 deletions examples/simple_step_fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// https://opensource.org/licenses/MIT.

use anyhow::Result;
use futures::FutureExt;

use ocptv::ocptv_log_info;
use ocptv::output as tv;
Expand All @@ -21,17 +20,14 @@ async fn main() -> Result<()> {
.build()
.scope(dut, |r| async move {
r.add_step("step0")
.scope(|s| {
async move {
ocptv_log_info!(s, "info log").await?;
Ok(TestStatus::Complete)
}
.boxed()
.scope(|s| async move {
ocptv_log_info!(s, "info log").await?;
Ok(TestStatus::Complete)
})
.await?;

r.add_step("step1")
.scope(|_s| async move { Ok(TestStatus::Error) }.boxed())
.scope(|_s| async move { Ok(TestStatus::Error) })
.await?;

Ok(tv::TestRunOutcome {
Expand Down
Loading

0 comments on commit 53a6e23

Please sign in to comment.