first tests

This commit is contained in:
NikVolf
2016-04-14 18:22:31 +03:00
parent 405e3e2e7d
commit 4931a300f2
3 changed files with 56 additions and 12 deletions

View File

@@ -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());
}
}

View File

@@ -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 {}