Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed Jan 17, 2024
1 parent e9ee990 commit 1d5ba03
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions relay-server/src/actors/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,7 @@ impl Project {
mod tests {
use std::sync::{Arc, Mutex};

use itertools::Itertools;
use relay_common::time::UnixTimestamp;
use relay_metrics::BucketValue;
use relay_test::mock_service;
Expand Down Expand Up @@ -1437,4 +1438,116 @@ mod tests {

assert!(metrics.is_empty());
}

fn get_test_buckets(names: &[&str]) -> Vec<Bucket> {
let create_bucket = |name: &&str| -> Bucket {
let json = json!({
"timestamp": 1615889440,
"width": 10,
"name": name,
"type": "c",
"value": 4.0,
"tags": {
"route": "user_index"
}});

serde_json::from_value(json).unwrap()
};

names.iter().map(create_bucket).collect_vec()
}

fn get_test_bucket_names() -> Vec<&'static str> {
[
"g:transactions/foo@none",
"c:custom/foo@none",
"transactions/foo@second",
"transactions/foo",
"c:custom/foo_bar@none",
"endpoint.response_time",
"endpoint.hits",
"endpoint.parallel_requests",
"endpoint.users",
]
.into()
}

fn apply_pattern_to_names(names: &[&str], patterns: &[&str]) -> Vec<String> {
let mut buckets = get_test_buckets(names);
let patterns = patterns.iter().map(|s| String::from(*s)).collect_vec();
let deny_list = GlobPatterns::new(patterns);
Project::apply_metrics_deny_list(&deny_list, &mut buckets);
buckets.into_iter().map(|bucket| bucket.name).collect_vec()
}

#[test]
fn test_metric_deny_list_exact() {
let names = get_test_bucket_names();
let input_qty = names.len();
let remaining_names = apply_pattern_to_names(&names, &["endpoint.parallel_requests"]);

// There's 1 bucket name with that exact name.
let buckets_to_remove = 1;

assert_eq!(remaining_names.len(), input_qty - buckets_to_remove);
}

#[test]
fn test_metric_deny_list_end_glob() {
let names = get_test_bucket_names();
let input_qty = names.len();
let remaining_names = apply_pattern_to_names(&names, &["*foo"]);

// There's 1 bucket name with 'foo' in the end.
let buckets_to_remove = 1;

assert_eq!(remaining_names.len(), input_qty - buckets_to_remove);
}

#[test]
fn test_metric_deny_list_middle_glob() {
let names = get_test_bucket_names();
let input_qty = names.len();
let remaining_names = apply_pattern_to_names(&names, &["*foo*"]);

// There's 4 bucket names with 'foo' in the middle, and one with foo in the end.
let buckets_to_remove = 5;

assert_eq!(remaining_names.len(), input_qty - buckets_to_remove);
}

#[test]
fn test_metric_deny_list_beginning_glob() {
let names = get_test_bucket_names();
let input_qty = names.len();
let remaining_names = apply_pattern_to_names(&names, &["endpoint*"]);

// There's 4 buckets starting with "endpoint".
let buckets_to_remove = 4;

assert_eq!(remaining_names.len(), input_qty - buckets_to_remove);
}

#[test]
fn test_metric_deny_list_everything() {
let names = get_test_bucket_names();
let remaining_names = apply_pattern_to_names(&names, &["*"]);

assert_eq!(remaining_names.len(), 0);
}

#[test]
fn test_metric_deny_list_multiple() {
let names = get_test_bucket_names();
let input_qty = names.len();
let remaining_names = apply_pattern_to_names(&names, &["endpoint*", "*transactions*"]);

let endpoint_buckets = 4;
let transaction_buckets = 3;

assert_eq!(
remaining_names.len(),
input_qty - endpoint_buckets - transaction_buckets
);
}
}

0 comments on commit 1d5ba03

Please sign in to comment.