mod common;
use std::sync::Arc;
use std::thread;
use net_kit::Net;
#[test]
fn move_into_function_preserves_state() {
let rt = common::build_runtime();
let net = Net::new();
rt.block_on(async { net.start().await.unwrap() });
let handle = net.register(Box::new(|_| {})).unwrap().unwrap();
fn consume(net: Net, h: net_kit::NetworkStatusListenerHandle) -> Net {
assert!(net.unregister(h).unwrap(), "handle still valid after move");
assert!(net.register(Box::new(|_| {})).unwrap().is_some());
net
}
let net = consume(net, handle);
net.shutdown().unwrap();
}
#[test]
fn move_into_thread() {
let rt = common::build_runtime();
let net = Net::new();
rt.block_on(async { net.start().await.unwrap() });
let joined = thread::spawn(move || {
assert!(net.register(Box::new(|_| {})).unwrap().is_some());
let _ = net.local_network_reachability();
net.shutdown().unwrap();
});
joined.join().expect("moved Net thread must not panic");
}
#[test]
fn move_through_container() {
let rt = common::build_runtime();
let mut bag: Vec<Net> = Vec::new();
bag.push(Net::new());
let net = bag.pop().unwrap();
rt.block_on(async { net.start().await.unwrap() });
assert!(net.register(Box::new(|_| {})).unwrap().is_some());
net.shutdown().unwrap();
}
#[test]
fn drop_without_shutdown_is_safe() {
let rt = common::build_runtime();
{
let net = Net::new();
rt.block_on(async { net.start().await.unwrap() });
let _ = net.register(Box::new(|_| {}));
}
assert_eq!(rt.block_on(async { 1 + 1 }), 2);
}
#[test]
fn independent_instances_are_isolated() {
let rt = common::build_runtime();
let a = Arc::new(Net::new());
let b = Net::new();
rt.block_on(async {
a.start().await.unwrap();
b.start().await.unwrap();
});
let ha = a.register(Box::new(|_| {})).unwrap().unwrap();
b.shutdown().unwrap();
assert!(a.unregister(ha).unwrap(), "A is unaffected by B's shutdown");
a.shutdown().unwrap();
}