use std::mem;
use std::sync::Arc;
use std::sync::Mutex;
use client::ClientOptions;
use hub::Hub;
use transport::Transport;
use protocol::Event;
use Dsn;
lazy_static! {
static ref TEST_DSN: Dsn = "https://public@sentry.invalid/1".parse().unwrap();
}
pub struct TestTransport {
collected: Mutex<Vec<Event<'static>>>,
}
impl TestTransport {
pub fn new() -> Arc<TestTransport> {
Arc::new(TestTransport {
collected: Mutex::new(vec![]),
})
}
pub fn fetch_and_clear_events(&self) -> Vec<Event<'static>> {
let mut guard = self.collected.lock().unwrap();
mem::replace(&mut *guard, vec![])
}
}
impl Transport for TestTransport {
fn send_event(&self, event: Event<'static>) {
self.collected.lock().unwrap().push(event);
}
}
pub fn with_captured_events<F: FnOnce()>(f: F) -> Vec<Event<'static>> {
with_captured_events_options(f, ClientOptions::default())
}
pub fn with_captured_events_options<F: FnOnce(), O: Into<ClientOptions>>(
f: F,
options: O,
) -> Vec<Event<'static>> {
let transport = TestTransport::new();
let mut options = options.into();
options.dsn = Some(options.dsn.unwrap_or_else(|| TEST_DSN.clone()));
options.transport = Box::new(transport.clone());
Hub::run(
Arc::new(Hub::new(
Some(Arc::new(options.into())),
Arc::new(Default::default()),
)),
f,
);
transport.fetch_and_clear_events()
}