From 909852e4a0d604463572ef7b17e21417a68e1a52 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Wed, 25 Sep 2024 11:44:01 -0700 Subject: [PATCH 1/2] Add tests for periodic reader from various RT combinations --- .../src/metrics/periodic_reader.rs | 78 ++++++++++++++----- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/opentelemetry-sdk/src/metrics/periodic_reader.rs b/opentelemetry-sdk/src/metrics/periodic_reader.rs index c07200b274..177de02190 100644 --- a/opentelemetry-sdk/src/metrics/periodic_reader.rs +++ b/opentelemetry-sdk/src/metrics/periodic_reader.rs @@ -381,29 +381,39 @@ mod tests { use std::sync::mpsc; #[test] - fn collection_triggered_by_interval() { - // Arrange - let interval = std::time::Duration::from_millis(1); - let exporter = InMemoryMetricsExporter::default(); - let reader = PeriodicReader::builder(exporter.clone(), runtime::TokioCurrentThread) - .with_interval(interval) - .build(); - let (sender, receiver) = mpsc::channel(); + fn collection_triggered_by_interval_tokio_current() { + collection_triggered_by_interval_helper(runtime::TokioCurrentThread); + } - // Act - let meter_provider = SdkMeterProvider::builder().with_reader(reader).build(); - let meter = meter_provider.meter("test"); - let _counter = meter - .u64_observable_counter("testcounter") - .with_callback(move |_| { - sender.send(()).expect("channel should still be open"); - }) - .init(); + #[tokio::test(flavor = "multi_thread", worker_threads = 1)] + async fn collection_triggered_by_interval_from_tokio_multi_one_thread_on_runtime_tokio() { + collection_triggered_by_interval_helper(runtime::Tokio); + } - // Assert - receiver - .recv() - .expect("message should be available in channel, indicating a collection occurred"); + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn collection_triggered_by_interval_from_tokio_multi_two_thread_on_runtime_tokio() { + collection_triggered_by_interval_helper(runtime::Tokio); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 1)] + async fn collection_triggered_by_interval_from_tokio_multi_one_thread_on_runtime_tokio_current() { + collection_triggered_by_interval_helper(runtime::TokioCurrentThread); + } + + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn collection_triggered_by_interval_from_tokio_multi_two_thread_on_runtime_tokio_current() { + collection_triggered_by_interval_helper(runtime::TokioCurrentThread); + } + + #[tokio::test(flavor = "current_thread")] + #[ignore = "See issue https://github.com/open-telemetry/opentelemetry-rust/issues/2056"] + async fn collection_triggered_by_interval_from_tokio_current_on_runtime_tokio() { + collection_triggered_by_interval_helper(runtime::Tokio); + } + + #[tokio::test(flavor = "current_thread")] + async fn collection_triggered_by_interval_from_tokio_current_on_runtime_tokio_current() { + collection_triggered_by_interval_helper(runtime::TokioCurrentThread); } #[test] @@ -424,4 +434,30 @@ mod tests { matches!(result.unwrap_err(), MetricsError::Other(err) if err == "reader is not registered") ); } + + fn collection_triggered_by_interval_helper(runtime: RT) + where RT: crate::runtime::Runtime + { + let interval = std::time::Duration::from_millis(1); + let exporter = InMemoryMetricsExporter::default(); + let reader = PeriodicReader::builder(exporter.clone(), runtime) + .with_interval(interval) + .build(); + let (sender, receiver) = mpsc::channel(); + + // Act + let meter_provider = SdkMeterProvider::builder().with_reader(reader).build(); + let meter = meter_provider.meter("test"); + let _counter = meter + .u64_observable_counter("testcounter") + .with_callback(move |_| { + sender.send(()).expect("channel should still be open"); + }) + .init(); + + // Assert + receiver + .recv() + .expect("message should be available in channel, indicating a collection occurred"); + } } From c9b4e789c997afbb1627e2fb9980e4eeb55d08a4 Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Wed, 25 Sep 2024 11:47:07 -0700 Subject: [PATCH 2/2] fmt --- opentelemetry-sdk/src/metrics/periodic_reader.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/opentelemetry-sdk/src/metrics/periodic_reader.rs b/opentelemetry-sdk/src/metrics/periodic_reader.rs index 177de02190..f6aafeb06b 100644 --- a/opentelemetry-sdk/src/metrics/periodic_reader.rs +++ b/opentelemetry-sdk/src/metrics/periodic_reader.rs @@ -383,7 +383,7 @@ mod tests { #[test] fn collection_triggered_by_interval_tokio_current() { collection_triggered_by_interval_helper(runtime::TokioCurrentThread); - } + } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn collection_triggered_by_interval_from_tokio_multi_one_thread_on_runtime_tokio() { @@ -396,12 +396,14 @@ mod tests { } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] - async fn collection_triggered_by_interval_from_tokio_multi_one_thread_on_runtime_tokio_current() { + async fn collection_triggered_by_interval_from_tokio_multi_one_thread_on_runtime_tokio_current() + { collection_triggered_by_interval_helper(runtime::TokioCurrentThread); } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] - async fn collection_triggered_by_interval_from_tokio_multi_two_thread_on_runtime_tokio_current() { + async fn collection_triggered_by_interval_from_tokio_multi_two_thread_on_runtime_tokio_current() + { collection_triggered_by_interval_helper(runtime::TokioCurrentThread); } @@ -435,8 +437,9 @@ mod tests { ); } - fn collection_triggered_by_interval_helper(runtime: RT) - where RT: crate::runtime::Runtime + fn collection_triggered_by_interval_helper(runtime: RT) + where + RT: crate::runtime::Runtime, { let interval = std::time::Duration::from_millis(1); let exporter = InMemoryMetricsExporter::default();