Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wmedrano committed Jan 22, 2017
1 parent fa6fefb commit c99dc0e
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 41 deletions.
37 changes: 0 additions & 37 deletions src/info.rs

This file was deleted.

7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ pub mod callbacks;
/// Create a connection to a JACK server.
pub mod client;

mod info;
/// Control error and info logging from JACK.
pub mod logging;

mod jack_enums;
mod jack_utils;

/// Types for interacting with port data from JACK.
pub mod port;

mod primitive_types;
pub use info::{set_error_callback, set_info_callback};
pub use jack_enums::{JackControl, JackErr};
pub use primitive_types::{JackFrames, JackPortId, JackTime};

Expand All @@ -40,7 +41,7 @@ pub mod traits {
pub mod prelude {
pub use callbacks::{JackHandler, ProcessHandler, ProcessScope};
pub use client::*;
pub use info::*;
pub use logging::*;
pub use jack_enums::*;
pub use port::*;
pub use primitive_types::*;
Expand Down
69 changes: 69 additions & 0 deletions src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::ffi;
use std::io::{Write, stderr};
use std::sync::{Mutex, ONCE_INIT, Once};

use jack_sys as j;

lazy_static! {
static ref INFO_FN: Mutex<Option<fn(&str)>> = Mutex::new(None);
static ref ERROR_FN: Mutex<Option<fn(&str)>> = Mutex::new(None);
}

unsafe extern "C" fn error_wrapper(msg: *const i8) {
let msg = ffi::CStr::from_ptr(msg).to_str().unwrap_or("rust failed to interpret error message");
let f = ERROR_FN.lock().unwrap();
match *f {
Some(f) => f(msg),
None => writeln!(&mut stderr(), "{}", msg).unwrap(),
}
}

unsafe extern "C" fn info_wrapper(msg: *const i8) {
let msg = ffi::CStr::from_ptr(msg).to_str().unwrap_or("rust failed to interpret info message");
let f = INFO_FN.lock().unwrap();
match *f {
Some(f) => f(msg),
None => println!("{}", msg),
}
}

static IS_INFO_CALLBACK_SET: Once = ONCE_INIT;
/// Set the global JACK info callback. It is recommended to use the [log
/// crate](https://cratse.io/crates/log).
pub fn set_info_callback(info: fn(&str)) {
*INFO_FN.lock().unwrap() = Some(info);
IS_INFO_CALLBACK_SET.call_once(|| unsafe { j::jack_set_info_function(Some(info_wrapper)) })
}

/// Resets the JACK info callback to use stdio.

/// Get the info callback that was set using `set_info_callback`. This corresponds to the one set
/// using rust-jack, not JACK itself. `None` is returned if rust-jack hasn't set a callback or has
/// reset it to use stdout.
pub fn get_info_callback() -> Option<fn(&str)> {
*INFO_FN.lock().unwrap()
}

/// Restores the JACK info callback to the JACK default, which is to write to stdout.
pub fn reset_info_callback() {
*INFO_FN.lock().unwrap() = None;
}

static IS_ERROR_CALLBACK_SET: Once = ONCE_INIT;
/// Set the global JACK error callback. It is recommended to use the [log
/// crate](https://cratse.io/crates/log).
pub fn set_error_callback(error: fn(&str)) {
*ERROR_FN.lock().unwrap() = Some(error);
IS_ERROR_CALLBACK_SET.call_once(|| unsafe { j::jack_set_error_function(Some(error_wrapper)) })
}

/// Get the error callback that was set using `set_error_callback`. This corresponds to the one set
/// using rust-jack, not JACK itself. `None` is returned if rust-jack hasn't set a callback or has
/// reset it to use stderr.
pub fn get_error_callback() -> Option<fn(&str)> {
*ERROR_FN.lock().unwrap()
}
/// Restores the JACK info callback to the JACK default, which is to write to stderr.
pub fn reset_error_callback() {
*ERROR_FN.lock().unwrap() = None;
}
2 changes: 2 additions & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod test_logging;

// time related functionality in lib.rs
mod test_time;

Expand Down
5 changes: 4 additions & 1 deletion src/test/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use jack_utils::*;

fn open_test_client(name: &str) -> (Client, ClientStatus) {
default_sleep();
Client::open(name, client_options::NO_START_SERVER).unwrap()
let ret = Client::open(name, client_options::NO_START_SERVER).unwrap();
default_sleep();
ret
}

#[test]
Expand Down Expand Up @@ -61,6 +63,7 @@ fn client_can_deactivate() {
let a = c.activate(DummyHandler).unwrap();
default_sleep();
a.deactivate().unwrap();
default_sleep();
}

#[test]
Expand Down
33 changes: 33 additions & 0 deletions src/test/test_logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use prelude::*;

fn null_log_fn(_: &str) {}

#[test]
fn logging_can_set_info() {
// initial state
reset_info_callback();
assert!(get_info_callback().is_none());

// set
set_info_callback(null_log_fn);
assert!(get_info_callback().is_some());

// reset
reset_info_callback();
assert!(get_info_callback().is_none());
}

#[test]
fn logging_can_set_error() {
// initial state
reset_error_callback();
assert!(get_error_callback().is_none());

// set
set_error_callback(null_log_fn);
assert!(get_error_callback().is_some());

// reset
reset_error_callback();
assert!(get_error_callback().is_none());
}

0 comments on commit c99dc0e

Please sign in to comment.