mod common;
use net_kit::{Net, NetworkStatus};
#[test]
fn start_with_external_multi_thread_runtime() {
let rt = common::build_runtime();
let net = Net::new();
rt.block_on(async {
net.start_with_tokio_rt(tokio::runtime::Handle::current())
.await
.expect("start_with_tokio_rt should succeed");
});
assert!(matches!(
net.local_network_reachability().unwrap(),
NetworkStatus::Available | NetworkStatus::Unavailable
));
assert!(net.register(Box::new(|_| {})).unwrap().is_some());
net.shutdown().expect("shutdown should succeed");
}
#[test]
fn external_runtime_survives_shutdown() {
let rt = common::build_runtime();
let net = Net::new();
rt.block_on(async {
net.start_with_tokio_rt(tokio::runtime::Handle::current())
.await
.unwrap();
});
net.shutdown().unwrap();
let answer = rt.block_on(async { 21 * 2 });
assert_eq!(answer, 42, "external runtime remains usable after shutdown");
}
#[test]
fn external_runtime_restart() {
let rt = common::build_runtime();
let net = Net::new();
rt.block_on(async {
net.start_with_tokio_rt(tokio::runtime::Handle::current())
.await
.unwrap();
});
net.shutdown().unwrap();
rt.block_on(async {
net.start_with_tokio_rt(tokio::runtime::Handle::current())
.await
.unwrap();
});
assert!(net.register(Box::new(|_| {})).unwrap().is_some());
net.shutdown().unwrap();
}
#[test]
fn start_with_current_thread_runtime() {
let rt = common::build_current_thread_runtime();
let net = Net::new();
rt.block_on(async {
net.start_with_tokio_rt(tokio::runtime::Handle::current())
.await
.expect("current-thread runtime should also work");
let _ = net.local_network_reachability();
});
net.shutdown().unwrap();
}