first tests
This commit is contained in:
parent
405e3e2e7d
commit
4931a300f2
@ -80,7 +80,7 @@ pub enum SocketError {
|
||||
|
||||
impl<S> Worker<S> where S: IpcInterface<S> {
|
||||
/// New worker over specified `service`
|
||||
pub fn new(service: Arc<S>) -> Worker<S> {
|
||||
pub fn new(service: &Arc<S>) -> Worker<S> {
|
||||
Worker::<S> {
|
||||
service: service.clone(),
|
||||
sockets: Vec::new(),
|
||||
@ -226,13 +226,13 @@ mod service_tests {
|
||||
|
||||
#[test]
|
||||
fn can_create_worker() {
|
||||
let worker = Worker::<DummyService>::new(Arc::new(DummyService::new()));
|
||||
let worker = Worker::<DummyService>::new(&Arc::new(DummyService::new()));
|
||||
assert_eq!(0, worker.sockets.len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_add_duplex_socket_to_worker() {
|
||||
let mut worker = Worker::<DummyService>::new(Arc::new(DummyService::new()));
|
||||
let mut worker = Worker::<DummyService>::new(&Arc::new(DummyService::new()));
|
||||
worker.add_duplex("ipc:///tmp/parity-test10.ipc").unwrap();
|
||||
assert_eq!(1, worker.sockets.len());
|
||||
}
|
||||
@ -240,7 +240,7 @@ mod service_tests {
|
||||
#[test]
|
||||
fn worker_can_poll_empty() {
|
||||
let service = Arc::new(DummyService::new());
|
||||
let mut worker = Worker::<DummyService>::new(service.clone());
|
||||
let mut worker = Worker::<DummyService>::new(&service);
|
||||
worker.add_duplex("ipc:///tmp/parity-test20.ipc").unwrap();
|
||||
worker.poll();
|
||||
assert_eq!(0, service.methods_stack.read().unwrap().len());
|
||||
@ -250,7 +250,7 @@ mod service_tests {
|
||||
fn worker_can_poll() {
|
||||
let url = "ipc:///tmp/parity-test30.ipc";
|
||||
|
||||
let mut worker = Worker::<DummyService>::new(Arc::new(DummyService::new()));
|
||||
let mut worker = Worker::<DummyService>::new(&Arc::new(DummyService::new()));
|
||||
worker.add_duplex(url).unwrap();
|
||||
|
||||
let (_socket, _endpoint) = dummy_write(url, &vec![0, 0, 7, 7, 6, 6]);
|
||||
@ -265,7 +265,7 @@ mod service_tests {
|
||||
fn worker_can_poll_long() {
|
||||
let url = "ipc:///tmp/parity-test40.ipc";
|
||||
|
||||
let mut worker = Worker::<DummyService>::new(Arc::new(DummyService::new()));
|
||||
let mut worker = Worker::<DummyService>::new(&Arc::new(DummyService::new()));
|
||||
worker.add_duplex(url).unwrap();
|
||||
|
||||
let message = [0u8; 1024*1024];
|
||||
|
@ -21,24 +21,58 @@ pub mod service;
|
||||
pub const HYPERVISOR_IPC_URL: &'static str = "ipc:///tmp/parity-internal-hyper-status.ipc";
|
||||
|
||||
use nanoipc;
|
||||
use std::sync::{Arc,RwLock};
|
||||
use hypervisor::service::*;
|
||||
use ipc::IpcInterface;
|
||||
|
||||
pub struct Hypervisor {
|
||||
service: Arc<HypervisorService>,
|
||||
ipc_worker: RwLock<nanoipc::Worker<HypervisorService>>,
|
||||
}
|
||||
|
||||
impl Hypervisor {
|
||||
/// initializes the Hypervisor service with the open ipc socket for incoming clients
|
||||
pub fn init() -> Arc<Hypervisor>{
|
||||
let mut worker = nanoipc::Worker::<HypervisorService>::new(Arc::new(Service::new()));
|
||||
worker.add_reqrep(HYPERVISOR_IPC_URL);
|
||||
Hypervisor::with_url(HYPERVISOR_IPC_URL)
|
||||
}
|
||||
|
||||
fn with_url(addr: &str) -> Arc<Hypervisor>{
|
||||
let service = HypervisorService::new();
|
||||
let mut worker = nanoipc::Worker::new(&service);
|
||||
worker.add_reqrep(addr);
|
||||
|
||||
Arc::new(Hypervisor{
|
||||
ipc_worker: worker
|
||||
service: service,
|
||||
ipc_worker: RwLock::new(worker),
|
||||
})
|
||||
}
|
||||
|
||||
/// Waits for every required module to check in
|
||||
pub fn wait_for_startup() {
|
||||
pub fn modules_ready(&self) -> bool {
|
||||
self.service.unchecked_count() == 0
|
||||
}
|
||||
|
||||
/// Waits for every required module to check in
|
||||
pub fn wait_for_startup(&self) {
|
||||
let mut worker = self.ipc_worker.write().unwrap();
|
||||
while !self.modules_ready() {
|
||||
worker.poll()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn can_init() {
|
||||
let hypervisor = Hypervisor::with_url("ipc:///tmp/test-parity-hypervisor-10");
|
||||
assert_eq!(false, hypervisor.modules_ready());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_wait_for_startup() {
|
||||
let hypervisor = Hypervisor::with_url("ipc:///tmp/test-parity-hypervisor-10");
|
||||
hypervisor.wait_for_startup();
|
||||
assert_eq!(false, hypervisor.modules_ready());
|
||||
}
|
||||
}
|
||||
|
@ -39,12 +39,22 @@ impl HypervisorService {
|
||||
|
||||
impl HypervisorService {
|
||||
pub fn new() -> Arc<HypervisorService> {
|
||||
HypervisorService::with_modules(vec![DATABASE_MODULE_ID]);
|
||||
}
|
||||
|
||||
pub fn with_modules(module_ids: Vec<IpcModuleID>) -> Arc<HypervisorService> {
|
||||
let mut check_list = HashMap::new();
|
||||
check_list.insert(DATABASE_MODULE_ID, false);
|
||||
for module_id in module_ids {
|
||||
check_list.insert(module_id, false);
|
||||
}
|
||||
Arc::new(HypervisorService {
|
||||
check_list: RwLock::new(check_list),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn unchecked_count(&self) -> usize {
|
||||
self.check_list.read().unwrap().iter().filter(|&(_, status)| !status).count()
|
||||
}
|
||||
}
|
||||
|
||||
impl ::ipc::IpcConfig for HypervisorService {}
|
||||
|
Loading…
Reference in New Issue
Block a user