Skip to content

Commit

Permalink
add scope for measurement series
Browse files Browse the repository at this point in the history
Signed-off-by: mimir-d <[email protected]>
  • Loading branch information
mimir-d committed Oct 8, 2024
1 parent 47c790f commit e7b160b
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 116 deletions.
81 changes: 43 additions & 38 deletions src/output/measure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use std::collections::BTreeMap;
use std::sync::atomic::{self, Ordering};
use std::sync::Arc;

#[cfg(feature = "boxed-scopes")]
use futures::future::BoxFuture;
use maplit::{btreemap, convert_args};

use crate::output as tv;
Expand Down Expand Up @@ -73,44 +75,47 @@ impl MeasurementSeries {
})
}

// /// Builds a scope in the [`MeasurementSeries`] object, taking care of starting and
// /// ending it. View [`MeasurementSeries::start`] and [`MeasurementSeries::end`] methods.
// /// After the scope is constructed, additional objects may be added to it.
// /// This is the preferred usage for the [`MeasurementSeries`], since it guarantees
// /// all the messages are emitted between the start and end messages, the order
// /// is respected and no messages is lost.
// ///
// /// # Examples
// ///
// /// ```rust
// /// # tokio_test::block_on(async {
// /// # use ocptv::output::*;
// ///
// /// let run = TestRun::new("diagnostic_name", "my_dut", "1.0").start().await?;
// /// let step = run.add_step("step_name").start().await?;
// ///
// /// let series = step.add_measurement_series("name");
// /// series.start().await?;
// /// series.scope(|s| async {
// /// s.add_measurement(60.into()).await?;
// /// s.add_measurement(70.into()).await?;
// /// s.add_measurement(80.into()).await?;
// /// Ok(())
// /// }).await?;
// ///
// /// # Ok::<(), OcptvError>(())
// /// # });
// /// ```
// pub async fn scope<'s, F, R>(&'s self, func: F) -> Result<(), tv::OcptvError>
// where
// R: Future<Output = Result<(), tv::OcptvError>>,
// F: std::ops::FnOnce(&'s MeasurementSeries) -> R,
// {
// self.start().await?;
// func(self).await?;
// self.end().await?;
// Ok(())
// }
/// Builds a scope in the [`MeasurementSeries`] object, taking care of starting and
/// ending it. View [`MeasurementSeries::start`] and [`MeasurementSeries::end`] methods.
/// After the scope is constructed, additional objects may be added to it.
/// This is the preferred usage for the [`MeasurementSeries`], since it guarantees
/// all the messages are emitted between the start and end messages, the order
/// is respected and no messages is lost.
///
/// # Examples
///
/// ```rust
/// # tokio_test::block_on(async {
/// # use futures::FutureExt;
/// # use ocptv::output::*;
///
/// let run = TestRun::new("diagnostic_name", "my_dut", "1.0").start().await?;
/// let step = run.add_step("step_name").start().await?;
///
/// let series = step.add_measurement_series("name");
/// series.scope(|s| {
/// async move {
/// s.add_measurement(60.into()).await?;
/// s.add_measurement(70.into()).await?;
/// s.add_measurement(80.into()).await?;
/// Ok(())
/// }.boxed()
/// }).await?;
///
/// # Ok::<(), OcptvError>(())
/// # });
/// ```
#[cfg(feature = "boxed-scopes")]
pub async fn scope<F>(self, func: F) -> Result<(), tv::OcptvError>
where
F: FnOnce(&StartedMeasurementSeries) -> BoxFuture<'_, Result<(), tv::OcptvError>>,
{
let series = self.start().await?;
func(&series).await?;
series.end().await?;

Ok(())
}
}

pub struct StartedMeasurementSeries {
Expand Down
2 changes: 1 addition & 1 deletion src/output/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl TestStep {
/// Builds a scope in the [`TestStep`] object, taking care of starting and
/// ending it. View [`TestStep::start`] and [`TestStep::end`] methods.
/// After the scope is constructed, additional objects may be added to it.
/// This is the preferred usage for the [`TestStep`], since it guarantees
/// This is the preferred usaggste for the [`TestStep`], since it guarantees
/// all the messages are emitted between the start and end messages, the order
/// is respected and no messages is lost.
///
Expand Down
171 changes: 94 additions & 77 deletions tests/output/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,83 +1232,100 @@ async fn test_step_with_measurement_series_element_with_metadata_index_no() -> R
.await
}

// #[tokio::test]
// async fn test_step_with_measurement_series_scope() -> Result<()> {
// let expected = [
// json_schema_version(),
// json_run_default_start(),
// json_step_default_start(),
// json!({
// "testStepArtifact": {
// "measurementSeriesStart": {
// "measurementSeriesId": "series_0",
// "name": "name"
// }
// },
// "sequenceNumber": 3
// }),
// json!({
// "testStepArtifact": {
// "measurementSeriesElement": {
// "index": 0,
// "measurementSeriesId": "series_0",
// "value": 60
// }
// },
// "sequenceNumber": 4
// }),
// json!({
// "testStepArtifact": {
// "measurementSeriesElement": {
// "index": 1,
// "measurementSeriesId": "series_0",
// "value": 70
// }
// },
// "sequenceNumber": 5
// }),
// json!({
// "testStepArtifact": {
// "measurementSeriesElement": {
// "index": 2,
// "measurementSeriesId": "series_0",
// "value": 80
// }
// },
// "sequenceNumber": 6
// }),
// json!({
// "testStepArtifact": {
// "measurementSeriesEnd": {
// "measurementSeriesId": "series_0",
// "totalCount": 3
// }
// },
// "sequenceNumber": 7
// }),
// json_step_complete(8),
// json_run_pass(9),
// ];

// check_output_step(&expected, |step| {
// async {
// let series = step.add_measurement_series("name");
// series
// .scope(|s| async {
// s.add_measurement(60.into()).await?;
// s.add_measurement(70.into()).await?;
// s.add_measurement(80.into()).await?;

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

// Ok(())
// }
// .boxed()
// })
// .await
// }
#[cfg(feature = "boxed-scopes")]
#[tokio::test]
async fn test_step_with_measurement_series_scope() -> Result<()> {
let expected = [
json_schema_version(),
json_run_default_start(),
json_step_default_start(),
json!({
"testStepArtifact": {
"testStepId": "step_0",
"measurementSeriesStart": {
"measurementSeriesId": "series_0",
"name": "name"
}
},
"sequenceNumber": 3,
"timestamp": DATETIME_FORMATTED
}),
json!({
"testStepArtifact": {
"testStepId": "step_0",
"measurementSeriesElement": {
"index": 0,
"measurementSeriesId": "series_0",
"value": 60,
"timestamp": DATETIME_FORMATTED
}
},
"sequenceNumber": 4,
"timestamp": DATETIME_FORMATTED
}),
json!({
"testStepArtifact": {
"testStepId": "step_0",
"measurementSeriesElement": {
"index": 1,
"measurementSeriesId": "series_0",
"value": 70,
"timestamp": DATETIME_FORMATTED
}
},
"sequenceNumber": 5,
"timestamp": DATETIME_FORMATTED
}),
json!({
"testStepArtifact": {
"testStepId": "step_0",
"measurementSeriesElement": {
"index": 2,
"measurementSeriesId": "series_0",
"value": 80,
"timestamp": DATETIME_FORMATTED
}
},
"sequenceNumber": 6,
"timestamp": DATETIME_FORMATTED
}),
json!({
"testStepArtifact": {
"testStepId": "step_0",
"measurementSeriesEnd": {
"measurementSeriesId": "series_0",
"totalCount": 3
}
},
"sequenceNumber": 7,
"timestamp": DATETIME_FORMATTED
}),
json_step_complete(8),
json_run_pass(9),
];

check_output_step(&expected, |step| {
async {
let series = step.add_measurement_series("name");
series
.scope(|s| {
async move {
s.add_measurement(60.into()).await?;
s.add_measurement(70.into()).await?;
s.add_measurement(80.into()).await?;

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

Ok(())
}
.boxed()
})
.await
}

// reasoning: the coverage(off) attribute is experimental in llvm-cov, so because we cannot
// disable the coverage itself, only run this test when in coverage mode because assert_fs
Expand Down

0 comments on commit e7b160b

Please sign in to comment.