Skip to content

fix(tests): Fix uptime_metrics GraphQL/API tests #4187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions graphql/subscriptions/uptime_metrics.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
subscription UptimeMetricsSubscription($interval: Int!) {
uptimeMetrics(interval: $interval) {
subscription UptimeMetricsSubscription {
uptimeMetrics {
seconds
}
}
62 changes: 36 additions & 26 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ mod tests {
use chrono::Utc;
use futures::StreamExt;
use graphql_client::*;
use std::{sync::Once, time::Duration};
use std::{
sync::Once,
time::{Duration, Instant},
};
use tokio::{select, sync::oneshot};
use vector::{
self,
api::{self, client::subscription::SubscriptionClient},
config::Config,
heartbeat,
internal_events::{emit, GeneratorEventProcessed},
internal_events::{emit, GeneratorEventProcessed, Heartbeat},
test_util::{next_addr, retry_until},
};

static METRIC_INIT: Once = Once::new();
static METRICS_INIT: Once = Once::new();

#[derive(GraphQLQuery)]
#[graphql(
Expand Down Expand Up @@ -58,10 +60,27 @@ mod tests {
struct EventsProcessedMetricsSubscription;

// Initialize the metrics system. Idempotent.
fn init_metrics() {
METRIC_INIT.call_once(|| {
fn init_metrics() -> oneshot::Sender<()> {
METRICS_INIT.call_once(|| {
let _ = vector::metrics::init();
})
});

let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();
tokio::spawn(async move {
let since = Instant::now();
let mut timer = tokio::time::interval(Duration::from_secs(1));

loop {
select! {
_ = &mut shutdown_rx => break,
_ = timer.tick() => {
emit(Heartbeat { since });
}
}
}
});

shutdown_tx
}

// Provides a config that enables the API server, assigned to a random port. Implicitly
Expand Down Expand Up @@ -186,27 +205,20 @@ mod tests {
assert_matches!(heartbeats.next().await, None);
}

async fn new_uptime_subscription(
client: &SubscriptionClient,
num_results: usize,
interval: i64,
) {
async fn new_uptime_subscription(client: &SubscriptionClient) {
let request_body =
UptimeMetricsSubscription::build_query(uptime_metrics_subscription::Variables {
interval,
});
UptimeMetricsSubscription::build_query(uptime_metrics_subscription::Variables);

let subscription = client
.start::<UptimeMetricsSubscription>(&request_body)
.await
.unwrap();

tokio::pin! {
let uptime = subscription.stream().skip(num_results);
let uptime = subscription.stream().skip(1);
}

// Uptime should be at least the number of seconds as the results - 1, to account
// for the initial uptime
// Uptime should be above zero
assert!(
uptime
.take(1)
Expand All @@ -218,7 +230,7 @@ mod tests {
.unwrap()
.uptime_metrics
.seconds
> num_results as f64 - 1.0
> 0.00
)
}

Expand Down Expand Up @@ -331,10 +343,9 @@ mod tests {
let bind = config.api.bind.unwrap();
let client = new_subscription_client(bind).await;

init_metrics();
tokio::spawn(heartbeat::heartbeat());
let _metrics = init_metrics();

new_uptime_subscription(&client, 3, 1200).await;
new_uptime_subscription(&client).await;
}

#[tokio::test]
Expand All @@ -345,7 +356,7 @@ mod tests {
let bind = config.api.bind.unwrap();
let client = new_subscription_client(bind).await;

init_metrics();
let _metrics = init_metrics();

new_events_processed_subscription(&client, 3, 100).await;
}
Expand All @@ -358,11 +369,10 @@ mod tests {
let bind = config.api.bind.unwrap();
let client = new_subscription_client(bind).await;

init_metrics();
tokio::spawn(heartbeat::heartbeat());
let _metrics = init_metrics();

futures::join! {
new_uptime_subscription(&client, 3, 1200),
new_uptime_subscription(&client),
new_heartbeat_subscription(&client, 3, 500),
};
}
Expand Down