Skip to content

Commit

Permalink
feat: introduce test utils (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: MrCroxx <[email protected]>
  • Loading branch information
MrCroxx authored Jan 11, 2025
1 parent 2602237 commit 97ed567
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 8 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ jobs:
- name: Run rust clippy check (stable)
if: matrix.rust_toolchain == 'stable'
run: |
cargo clippy --all-targets --features prometheus,prometheus-client,prometheus-client_0_22,prometheus-client_0_23,opentelemetry_0_26,opentelemetry_0_27 -- -D warnings
cargo clippy --all-targets --all-features -- -D warnings
- if: steps.cache.outputs.cache-hit != 'true'
uses: taiki-e/install-action@cargo-llvm-cov
- if: steps.cache.outputs.cache-hit != 'true'
Expand All @@ -122,7 +122,7 @@ jobs:
RUST_BACKTRACE: 1
CI: true
run: |
cargo llvm-cov --no-report nextest --features "prometheus,prometheus-client,prometheus-client_0_22,prometheus-client_0_23,opentelemetry_0_26,opentelemetry_0_27"
cargo llvm-cov --no-report nextest --all-features
- name: Generate codecov report
run: |
cargo llvm-cov report --lcov --output-path lcov.info
Expand Down Expand Up @@ -158,12 +158,12 @@ jobs:
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}-${{ env.CACHE_KEY_SUFFIX }}-rustdoc-test
- name: Run rustdoc test
run: |
cargo test --features "prometheus,prometheus-client,prometheus-client_0_22,prometheus-client_0_23,opentelemetry_0_26,opentelemetry_0_27" --doc
cargo test --all-features --doc
- name: Test docs build with docs.rs
env:
RUSTDOCFLAGS: --cfg docsrs -D warnings
run: |
cargo doc --features "prometheus,prometheus-client,prometheus-client_0_22,prometheus-client_0_23,opentelemetry_0_26,opentelemetry_0_27" --no-deps
cargo doc --all-features --no-deps
- name: Upload docs.rs docs as artifacts
uses: actions/upload-artifact@v4
with:
Expand Down
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"opentelemetry",
"opentelemetry_0_27",
"opentelemetry_0_26",
"test-utils",
]
}
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,24 @@ prometheus-client_0_23 = { package = "prometheus-client", version = "0.23", opti
missing_docs = "warn"

[features]
default = []
prometheus = ["dep:prometheus"]
prometheus-client = ["prometheus-client_0_23"]
prometheus-client_0_23 = ["dep:prometheus-client_0_23"]
prometheus-client_0_22 = ["dep:prometheus-client_0_22"]
opentelemetry = ["opentelemetry_0_27"]
opentelemetry_0_27 = ["dep:opentelemetry_0_27"]
opentelemetry_0_26 = ["dep:opentelemetry_0_26"]
test-utils = [
"prometheus",
"prometheus-client",
"prometheus-client_0_23",
"prometheus-client_0_22",
"opentelemetry",
"opentelemetry_0_27",
"opentelemetry_0_26",
]
full = ["test-utils"]

[package.metadata.docs.rs]
features = [
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ check:
typos
cargo sort -w
cargo fmt --all
cargo clippy --all-targets --features prometheus,prometheus-client,prometheus-client_0_22,prometheus-client_0_23,opentelemetry_0_26,opentelemetry_0_27
cargo clippy --all-targets --all-features

test:
RUST_BACKTRACE=1 cargo nextest run --all --features "prometheus,prometheus-client,prometheus-client_0_22,prometheus-client_0_23,opentelemetry_0_26,opentelemetry_0_27"
RUST_BACKTRACE=1 cargo test --doc --features "prometheus,prometheus-client,prometheus-client_0_22,prometheus-client_0_23,opentelemetry_0_26,opentelemetry_0_27"
RUSTDOCFLAGS="--cfg docsrs -D warnings" cargo +nightly doc --features "prometheus,prometheus-client,prometheus-client_0_22,prometheus-client_0_23,opentelemetry_0_26,opentelemetry_0_27" --no-deps
RUST_BACKTRACE=1 cargo nextest run --all --all-features
RUST_BACKTRACE=1 cargo test --doc --all-features
RUSTDOCFLAGS="--cfg docsrs -D warnings" cargo +nightly doc --all-features --no-deps

machete:
cargo machete
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ mod utils;

pub mod metrics;
pub mod registry;

#[cfg(feature = "test-utils")]
pub mod test_utils;
112 changes: 112 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2025 mixtrics Project Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

pub use opentelemetry_0_26;
pub use opentelemetry_0_27;
pub use prometheus;
pub use prometheus_client_0_22;
pub use prometheus_client_0_23;

#[macro_export]
macro_rules! test {
($f:expr) => {
#[test]
fn test_prometheus() {
use $crate::{
metrics::BoxedRegistry, registry::prometheus::PrometheusMetricsRegistry,
test_utils::prometheus::Registry,
};
let registry: BoxedRegistry = Box::new(PrometheusMetricsRegistry::new(Registry::new()));
($f)(&registry);
}

#[test]
fn test_prometheus_client_0_22() {
use std::sync::Arc;

use parking_lot::Mutex;
use $crate::{
metrics::BoxedRegistry, registry::prometheus_client_0_22::PrometheusClientMetricsRegistry,
test_utils::prometheus_client_0_22::registry::Registry,
};
let registry: BoxedRegistry = Box::new(PrometheusClientMetricsRegistry::new(Arc::new(Mutex::new(
Registry::default(),
))));
($f)(&registry);
}

#[test]
fn test_prometheus_client_0_23() {
use std::sync::Arc;

use parking_lot::Mutex;
use $crate::{
metrics::BoxedRegistry, registry::prometheus_client_0_23::PrometheusClientMetricsRegistry,
test_utils::prometheus_client_0_23::registry::Registry,
};
let registry: BoxedRegistry = Box::new(PrometheusClientMetricsRegistry::new(Arc::new(Mutex::new(
Registry::default(),
))));
($f)(&registry);
}

#[test]
fn test_opentelemetry_0_26() {
use $crate::{
metrics::BoxedRegistry, registry::opentelemetry_0_26::OpenTelemetryMetricsRegistry,
test_utils::opentelemetry_0_26::global::meter,
};
let registry: BoxedRegistry = Box::new(OpenTelemetryMetricsRegistry::new(meter("test")));
($f)(&registry);
}

#[test]
fn test_opentelemetry_0_27() {
use $crate::{
metrics::BoxedRegistry, registry::opentelemetry_0_27::OpenTelemetryMetricsRegistry,
test_utils::opentelemetry_0_27::global::meter,
};
let registry: BoxedRegistry = Box::new(OpenTelemetryMetricsRegistry::new(meter("test")));
($f)(&registry);
}
};
}

#[cfg(test)]
mod tests {
use crate::metrics::{BoxedCounter, BoxedGauge, BoxedHistogram, BoxedRegistry};

#[allow(dead_code)]
struct Model {
c: BoxedCounter,
g: BoxedGauge,
h: BoxedHistogram,
}

impl Model {
fn new(registry: &BoxedRegistry) -> Self {
let cv = registry.register_counter_vec("counter".into(), "counter".into(), &["k1"]);
let gv = registry.register_gauge_vec("gauge".into(), "gauge".into(), &["k1"]);
let hv = registry.register_histogram_vec("histogram".into(), "histogram".into(), &["k1"]);

let c = cv.counter(&["v1".into()]);
let g = gv.gauge(&["v1".into()]);
let h = hv.histogram(&["v1".into()]);

Self { c, g, h }
}
}

test! {Model::new}
}

0 comments on commit 97ed567

Please sign in to comment.