Fixed test execution.

This commit is contained in:
Aaron Weiss 2016-01-16 11:47:25 -05:00
parent 506fa3d78f
commit 8113c47842
2 changed files with 19 additions and 4 deletions

View file

@ -200,6 +200,8 @@ mod test {
encoding: Some(format!("UTF-8")), encoding: Some(format!("UTF-8")),
channels: Some(vec![format!("#test"), format!("#test2")]), channels: Some(vec![format!("#test"), format!("#test2")]),
user_info: None, user_info: None,
ping_time: None,
ping_timeout: None,
options: Some(HashMap::new()), options: Some(HashMap::new()),
}; };
assert_eq!(Config::load(Path::new("client_config.json")).unwrap(), cfg); assert_eq!(Config::load(Path::new("client_config.json")).unwrap(), cfg);
@ -222,6 +224,8 @@ mod test {
encoding: Some(format!("UTF-8")), encoding: Some(format!("UTF-8")),
channels: Some(vec![format!("#test"), format!("#test2")]), channels: Some(vec![format!("#test"), format!("#test2")]),
user_info: None, user_info: None,
ping_time: None,
ping_timeout: None,
options: Some(HashMap::new()), options: Some(HashMap::new()),
}; };
assert_eq!(Config::load("client_config.json").unwrap(), cfg); assert_eq!(Config::load("client_config.json").unwrap(), cfg);

View file

@ -48,6 +48,8 @@ pub struct IrcServer<T: IrcRead, U: IrcWrite> {
/// Thread-safe internal state for an IRC server connection. /// Thread-safe internal state for an IRC server connection.
struct ServerState<T: IrcRead, U: IrcWrite> { struct ServerState<T: IrcRead, U: IrcWrite> {
/// A global copy of the channel for sending messages to write.
tx: Mutex<Option<Sender<Message>>>,
/// The thread-safe IRC connection. /// The thread-safe IRC connection.
conn: Connection<T, U>, conn: Connection<T, U>,
/// The handle for the message sending thread. /// The handle for the message sending thread.
@ -60,8 +62,6 @@ struct ServerState<T: IrcRead, U: IrcWrite> {
alt_nick_index: RwLock<usize>, alt_nick_index: RwLock<usize>,
/// A thread-safe count of reconnection attempts used for synchronization. /// A thread-safe count of reconnection attempts used for synchronization.
reconnect_count: Mutex<u32>, reconnect_count: Mutex<u32>,
/// A global copy of the channel for sending messages to write.
tx: Mutex<Option<Sender<Message>>>,
/// A thread-safe store for the time of the last action. /// A thread-safe store for the time of the last action.
last_action_time: Mutex<Tm>, last_action_time: Mutex<Tm>,
/// A thread-safe store for the last ping data. /// A thread-safe store for the last ping data.
@ -71,13 +71,13 @@ struct ServerState<T: IrcRead, U: IrcWrite> {
impl<T: IrcRead, U: IrcWrite> ServerState<T, U> where Connection<T, U>: Reconnect { impl<T: IrcRead, U: IrcWrite> ServerState<T, U> where Connection<T, U>: Reconnect {
fn new(conn: Connection<T, U>, config: Config) -> ServerState<T, U> { fn new(conn: Connection<T, U>, config: Config) -> ServerState<T, U> {
ServerState { ServerState {
tx: Mutex::new(None),
conn: conn, conn: conn,
write_handle: Mutex::new(None), write_handle: Mutex::new(None),
config: config, config: config,
chanlists: Mutex::new(HashMap::new()), chanlists: Mutex::new(HashMap::new()),
alt_nick_index: RwLock::new(0), alt_nick_index: RwLock::new(0),
reconnect_count: Mutex::new(0), reconnect_count: Mutex::new(0),
tx: Mutex::new(None),
last_action_time: Mutex::new(now()), last_action_time: Mutex::new(now()),
last_ping_data: Mutex::new(None), last_ping_data: Mutex::new(None),
} }
@ -145,6 +145,7 @@ impl<T: IrcRead, U: IrcWrite> Clone for IrcServer<T, U> {
impl<T: IrcRead, U: IrcWrite> Drop for ServerState<T, U> { impl<T: IrcRead, U: IrcWrite> Drop for ServerState<T, U> {
fn drop(&mut self) { fn drop(&mut self) {
let _ = self.tx.lock().unwrap().take();
let mut guard = self.write_handle.lock().unwrap(); let mut guard = self.write_handle.lock().unwrap();
if let Some(handle) = guard.take() { if let Some(handle) = guard.take() {
handle.join().unwrap() handle.join().unwrap()
@ -493,6 +494,7 @@ mod test {
use super::{IrcServer, Server}; use super::{IrcServer, Server};
use std::default::Default; use std::default::Default;
use std::io::{Cursor, sink}; use std::io::{Cursor, sink};
use std::sync::mpsc::channel;
use client::conn::{Connection, Reconnect}; use client::conn::{Connection, Reconnect};
use client::data::{Config, Message, User}; use client::data::{Config, Message, User};
use client::data::command::Command::PRIVMSG; use client::data::command::Command::PRIVMSG;
@ -511,8 +513,17 @@ mod test {
} }
} }
pub fn get_server_value<T: IrcRead>(server: IrcServer<T, Vec<u8>>) -> String pub fn get_server_value<T: IrcRead>(mut server: IrcServer<T, Vec<u8>>) -> String
where Connection<T, Vec<u8>>: Reconnect { where Connection<T, Vec<u8>>: Reconnect {
let _ = server.state.tx.lock().unwrap().take();
// This is a terrible hack to get the real channel to drop.
// Otherwise, joining would never finish.
let (tx, _) = channel();
server.tx = tx;
let mut guard = server.state.write_handle.lock().unwrap();
if let Some(handle) = guard.take() {
handle.join().unwrap()
}
let vec = server.conn().writer().clone(); let vec = server.conn().writer().clone();
String::from_utf8(vec).unwrap() String::from_utf8(vec).unwrap()
} }