Skip to content

Commit 49f2360

Browse files
authored
fix flaky macOS tests (#4187)
Signed-off-by: Lee Benson <[email protected]>
1 parent d2b77e9 commit 49f2360

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
subscription UptimeMetricsSubscription($interval: Int!) {
2-
uptimeMetrics(interval: $interval) {
1+
subscription UptimeMetricsSubscription {
2+
uptimeMetrics {
33
seconds
44
}
55
}

tests/api.rs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ mod tests {
1010
use chrono::Utc;
1111
use futures::StreamExt;
1212
use graphql_client::*;
13-
use std::{sync::Once, time::Duration};
13+
use std::{
14+
sync::Once,
15+
time::{Duration, Instant},
16+
};
1417
use tokio::{select, sync::oneshot};
1518
use vector::{
1619
self,
1720
api::{self, client::subscription::SubscriptionClient},
1821
config::Config,
19-
heartbeat,
20-
internal_events::{emit, GeneratorEventProcessed},
22+
internal_events::{emit, GeneratorEventProcessed, Heartbeat},
2123
test_util::{next_addr, retry_until},
2224
};
2325

24-
static METRIC_INIT: Once = Once::new();
26+
static METRICS_INIT: Once = Once::new();
2527

2628
#[derive(GraphQLQuery)]
2729
#[graphql(
@@ -58,10 +60,27 @@ mod tests {
5860
struct EventsProcessedMetricsSubscription;
5961

6062
// Initialize the metrics system. Idempotent.
61-
fn init_metrics() {
62-
METRIC_INIT.call_once(|| {
63+
fn init_metrics() -> oneshot::Sender<()> {
64+
METRICS_INIT.call_once(|| {
6365
let _ = vector::metrics::init();
64-
})
66+
});
67+
68+
let (shutdown_tx, mut shutdown_rx) = oneshot::channel::<()>();
69+
tokio::spawn(async move {
70+
let since = Instant::now();
71+
let mut timer = tokio::time::interval(Duration::from_secs(1));
72+
73+
loop {
74+
select! {
75+
_ = &mut shutdown_rx => break,
76+
_ = timer.tick() => {
77+
emit(Heartbeat { since });
78+
}
79+
}
80+
}
81+
});
82+
83+
shutdown_tx
6584
}
6685

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

189-
async fn new_uptime_subscription(
190-
client: &SubscriptionClient,
191-
num_results: usize,
192-
interval: i64,
193-
) {
208+
async fn new_uptime_subscription(client: &SubscriptionClient) {
194209
let request_body =
195-
UptimeMetricsSubscription::build_query(uptime_metrics_subscription::Variables {
196-
interval,
197-
});
210+
UptimeMetricsSubscription::build_query(uptime_metrics_subscription::Variables);
198211

199212
let subscription = client
200213
.start::<UptimeMetricsSubscription>(&request_body)
201214
.await
202215
.unwrap();
203216

204217
tokio::pin! {
205-
let uptime = subscription.stream().skip(num_results);
218+
let uptime = subscription.stream().skip(1);
206219
}
207220

208-
// Uptime should be at least the number of seconds as the results - 1, to account
209-
// for the initial uptime
221+
// Uptime should be above zero
210222
assert!(
211223
uptime
212224
.take(1)
@@ -218,7 +230,7 @@ mod tests {
218230
.unwrap()
219231
.uptime_metrics
220232
.seconds
221-
> num_results as f64 - 1.0
233+
> 0.00
222234
)
223235
}
224236

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

334-
init_metrics();
335-
tokio::spawn(heartbeat::heartbeat());
346+
let _metrics = init_metrics();
336347

337-
new_uptime_subscription(&client, 3, 1200).await;
348+
new_uptime_subscription(&client).await;
338349
}
339350

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

348-
init_metrics();
359+
let _metrics = init_metrics();
349360

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

361-
init_metrics();
362-
tokio::spawn(heartbeat::heartbeat());
372+
let _metrics = init_metrics();
363373

364374
futures::join! {
365-
new_uptime_subscription(&client, 3, 1200),
375+
new_uptime_subscription(&client),
366376
new_heartbeat_subscription(&client, 3, 500),
367377
};
368378
}

0 commit comments

Comments
 (0)