From c0c75d36fff4ccd7b45cbdae82fa80fdc7612e17 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Wed, 13 Dec 2023 13:13:24 +0200 Subject: [PATCH] sound: add init_logger() helper for testing The init_logger() function gets compiled in tests only, and serves to initialize logging only once per test thread. Signed-off-by: Manos Pitsidianakis --- .../vhost-device-sound/src/audio_backends/null.rs | 2 ++ .../src/audio_backends/pipewire.rs | 2 ++ staging/vhost-device-sound/src/device.rs | 4 ++++ staging/vhost-device-sound/src/lib.rs | 12 ++++++++++++ staging/vhost-device-sound/src/main.rs | 6 ++++++ 5 files changed, 26 insertions(+) diff --git a/staging/vhost-device-sound/src/audio_backends/null.rs b/staging/vhost-device-sound/src/audio_backends/null.rs index ef5aa22..3101e9b 100644 --- a/staging/vhost-device-sound/src/audio_backends/null.rs +++ b/staging/vhost-device-sound/src/audio_backends/null.rs @@ -34,6 +34,7 @@ mod tests { #[test] fn test_null_backend_write() { + crate::init_logger(); let streams = Arc::new(RwLock::new(vec![Stream::default()])); let null_backend = NullBackend::new(streams.clone()); @@ -45,6 +46,7 @@ mod tests { #[test] fn test_null_backend_read() { + crate::init_logger(); let streams = Arc::new(RwLock::new(vec![Stream::default()])); let null_backend = NullBackend::new(streams.clone()); diff --git a/staging/vhost-device-sound/src/audio_backends/pipewire.rs b/staging/vhost-device-sound/src/audio_backends/pipewire.rs index 71f6eaf..4528689 100644 --- a/staging/vhost-device-sound/src/audio_backends/pipewire.rs +++ b/staging/vhost-device-sound/src/audio_backends/pipewire.rs @@ -575,6 +575,7 @@ mod tests { #[test] fn test_pipewire_backend_success() { + crate::init_logger(); let streams = Arc::new(RwLock::new(vec![Stream::default()])); let stream_params = streams.clone(); @@ -603,6 +604,7 @@ mod tests { #[test] fn test_pipewire_backend_invalid_stream() { + crate::init_logger(); let stream_params = Arc::new(RwLock::new(vec![])); let _test_harness = PipewireTestHarness::new(); diff --git a/staging/vhost-device-sound/src/device.rs b/staging/vhost-device-sound/src/device.rs index 4bd9ea2..3691ab4 100644 --- a/staging/vhost-device-sound/src/device.rs +++ b/staging/vhost-device-sound/src/device.rs @@ -827,6 +827,7 @@ mod tests { #[test] fn test_sound_thread_success() { + crate::init_logger(); let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null); let chmaps = Arc::new(RwLock::new(vec![])); @@ -921,6 +922,7 @@ mod tests { #[test] fn test_sound_thread_failure() { + crate::init_logger(); let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null); let chmaps = Arc::new(RwLock::new(vec![])); @@ -995,6 +997,7 @@ mod tests { #[test] fn test_sound_backend() { + crate::init_logger(); let test_dir = tempdir().expect("Could not create a temp test directory."); let socket_path = test_dir.path().join(SOCKET_PATH).display().to_string(); let config = SoundConfig::new(socket_path, false, BackendType::Null); @@ -1069,6 +1072,7 @@ mod tests { #[test] fn test_sound_backend_failures() { + crate::init_logger(); let test_dir = tempdir().expect("Could not create a temp test directory."); let socket_path = test_dir diff --git a/staging/vhost-device-sound/src/lib.rs b/staging/vhost-device-sound/src/lib.rs index 438ba38..aa1c6d7 100644 --- a/staging/vhost-device-sound/src/lib.rs +++ b/staging/vhost-device-sound/src/lib.rs @@ -30,6 +30,12 @@ clippy::significant_drop_tightening )] +#[cfg(test)] +pub fn init_logger() { + std::env::set_var("RUST_LOG", "trace"); + let _ = env_logger::builder().is_test(true).try_init(); +} + pub mod audio_backends; pub mod device; pub mod stream; @@ -319,6 +325,7 @@ mod tests { #[test] fn test_sound_server() { const SOCKET_PATH: &str = "vsound.socket"; + crate::init_logger(); let config = SoundConfig::new(SOCKET_PATH.to_string(), false, BackendType::Null); @@ -341,6 +348,7 @@ mod tests { #[test] fn test_control_message_kind_try_from() { + crate::init_logger(); assert_eq!( ControlMessageKind::try_from(>::into(VIRTIO_SND_R_JACK_INFO)), Ok(ControlMessageKind::JackInfo) @@ -361,6 +369,7 @@ mod tests { #[test] fn test_control_message_kind_try_from_invalid() { + crate::init_logger(); // Test an invalid value that should result in an InvalidControlMessage error let invalid_value: u32 = 0x1101; assert_eq!( @@ -371,6 +380,7 @@ mod tests { #[test] fn test_try_from_valid_output() { + crate::init_logger(); let val = virtio_sound::VIRTIO_SND_D_OUTPUT; assert_eq!(Direction::try_from(val).unwrap(), Direction::Output); @@ -383,6 +393,7 @@ mod tests { #[test] fn test_display() { + crate::init_logger(); let error = InvalidControlMessage(42); let formatted_error = format!("{}", error); assert_eq!(formatted_error, "Invalid control message code 42"); @@ -390,6 +401,7 @@ mod tests { #[test] fn test_into_error() { + crate::init_logger(); let error = InvalidControlMessage(42); let _error: Error = error.into(); diff --git a/staging/vhost-device-sound/src/main.rs b/staging/vhost-device-sound/src/main.rs index 5226626..9bb31a7 100644 --- a/staging/vhost-device-sound/src/main.rs +++ b/staging/vhost-device-sound/src/main.rs @@ -53,8 +53,14 @@ mod tests { } } + fn init_logger() { + std::env::set_var("RUST_LOG", "trace"); + let _ = env_logger::builder().is_test(true).try_init(); + } + #[test] fn test_sound_config_setup() { + init_logger(); let args = SoundArgs::from_args("/tmp/vhost-sound.socket"); let config = SoundConfig::try_from(args);