Skip to content

Commit e071b23

Browse files
fix(tests): Make tests pass with cargo test (#23520)
* Fix tests failing with cargo test * Use tokio Mutex instead * Implement global lock as suggested in PR review * Fix clippy * Drop guard after binding * Rename wait_for_tcp->wait_for_tcp_and_release * Lock when using log_schema in tests * Lock in tracing-limit's tests due to tracing::subcriber::with_default * Revert "Lock when using log_schema in tests" This reverts commit eed7a3878a5c0c74b3e759c8c470d42b353964d7. --------- Co-authored-by: Pavlos Rontidis <pavlos.rontidis@gmail.com>
1 parent 590c14b commit e071b23

File tree

4 files changed

+169
-81
lines changed

4 files changed

+169
-81
lines changed

lib/portpicker/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ pub fn pick_unused_port(ip: IpAddr) -> Port {
9494
}
9595
}
9696

97+
pub fn bind_unused_udp(ip: IpAddr) -> UdpSocket {
98+
let mut rng = rng();
99+
100+
loop {
101+
// Try random port first
102+
for _ in 0..10 {
103+
let port = rng.random_range(15000..25000);
104+
105+
if let Ok(socket) = UdpSocket::bind(SocketAddr::new(ip, port)) {
106+
return socket;
107+
}
108+
}
109+
110+
// Ask the OS for a port
111+
for _ in 0..10 {
112+
if let Ok(socket) = UdpSocket::bind(SocketAddr::new(ip, 0)) {
113+
return socket;
114+
}
115+
}
116+
}
117+
}
118+
97119
#[cfg(test)]
98120
mod tests {
99121
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

lib/tracing-limit/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,15 @@ impl Visit for MessageVisitor {
445445
#[cfg(test)]
446446
mod test {
447447
use std::{
448-
sync::{Arc, Mutex},
448+
sync::{Arc, LazyLock, Mutex},
449449
time::Duration,
450450
};
451451

452452
use mock_instant::global::MockClock;
453453
use tracing_subscriber::layer::SubscriberExt;
454454

455+
static TRACING_DEFAULT_LOCK: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
456+
455457
use super::*;
456458

457459
#[derive(Default)]
@@ -494,6 +496,8 @@ mod test {
494496

495497
#[test]
496498
fn rate_limits() {
499+
let _guard = TRACING_DEFAULT_LOCK.lock().unwrap();
500+
497501
let events: Arc<Mutex<Vec<String>>> = Default::default();
498502

499503
let recorder = RecordingLayer::new(Arc::clone(&events));
@@ -527,6 +531,8 @@ mod test {
527531

528532
#[test]
529533
fn override_rate_limit_at_callsite() {
534+
let _guard = TRACING_DEFAULT_LOCK.lock().unwrap();
535+
530536
let events: Arc<Mutex<Vec<String>>> = Default::default();
531537

532538
let recorder = RecordingLayer::new(Arc::clone(&events));
@@ -564,6 +570,8 @@ mod test {
564570

565571
#[test]
566572
fn rate_limit_by_span_key() {
573+
let _guard = TRACING_DEFAULT_LOCK.lock().unwrap();
574+
567575
let events: Arc<Mutex<Vec<String>>> = Default::default();
568576

569577
let recorder = RecordingLayer::new(Arc::clone(&events));
@@ -628,6 +636,8 @@ mod test {
628636

629637
#[test]
630638
fn rate_limit_by_event_key() {
639+
let _guard = TRACING_DEFAULT_LOCK.lock().unwrap();
640+
631641
let events: Arc<Mutex<Vec<String>>> = Default::default();
632642

633643
let recorder = RecordingLayer::new(Arc::clone(&events));

src/sources/exec/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ async fn test_drop_receiver() {
331331

332332
#[tokio::test]
333333
#[cfg(unix)]
334+
#[cfg_attr(target_os = "macos", ignore)] // Flaky when running `cargo test`
334335
async fn test_run_command_linux() {
335336
let config = standard_scheduled_test_config();
336337

0 commit comments

Comments
 (0)