Skip to content

Commit

Permalink
add test case about Arc<String> in struct
Browse files Browse the repository at this point in the history
Signed-off-by: Li Yazhou <[email protected]>
  • Loading branch information
flaneur2020 committed Jul 29, 2024
1 parent 6b2c93f commit 97b43b7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions derive-encode/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use prometheus_client::encoding::text::encode;
use prometheus_client::encoding::{EncodeLabelSet, EncodeLabelValue};
use prometheus_client::metrics::counter::Counter;
Expand Down Expand Up @@ -137,6 +139,36 @@ fn remap_keyword_identifiers() {
assert_eq!(expected, buffer);
}

#[test]
fn arc_string() {
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
struct Labels {
client_id: Arc<String>,
}

let mut registry = Registry::default();
let family = Family::<Labels, Counter>::default();
registry.register("my_counter", "This is my counter", family.clone());

// Record a single HTTP GET request.
let client_id = Arc::new("client_id".to_string());
family
.get_or_create(&Labels {
client_id: client_id.clone(),
})
.inc();

// Encode all metrics in the registry in the text format.
let mut buffer = String::new();
encode(&mut buffer, &registry).unwrap();

let expected = "# HELP my_counter This is my counter.\n".to_owned()
+ "# TYPE my_counter counter\n"
+ "my_counter_total{client_id=\"client_id\"} 1\n"
+ "# EOF\n";
assert_eq!(expected, buffer);
}

#[test]
fn flatten() {
#[derive(EncodeLabelSet, Hash, Clone, Eq, PartialEq, Debug)]
Expand Down

0 comments on commit 97b43b7

Please sign in to comment.