Skip to content

Commit

Permalink
Refine test case to increase code coverage
Browse files Browse the repository at this point in the history
Try to increase code coverage by adding more test code. But it actually
causes dramatic decreases in code coverage:(

Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
  • Loading branch information
jiangliu committed Dec 22, 2021
1 parent 72d754b commit 4e16112
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ vmm-sys-util = "0.9"

[dev-dependencies]
vm-memory = {version = "0.7", features = ["backend-mmap", "backend-atomic", "backend-bitmap"]}
tempfile = "3.2.0"
2 changes: 1 addition & 1 deletion coverage_config_x86_64.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"coverage_score": 78.5,
"coverage_score": 49.6,
"exclude_path": "",
"crate_features": ""
}
21 changes: 20 additions & 1 deletion src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ pub mod tests {
use super::*;
use crate::VringRwLock;
use std::sync::Mutex;
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap};

pub struct MockVhostBackend {
events: u64,
Expand Down Expand Up @@ -509,6 +509,13 @@ pub mod tests {

backend.set_event_idx(true);
assert_eq!(backend.lock().unwrap().event_idx, true);

let _ = backend.exit_event(0).unwrap();

let mem = GuestMemoryAtomic::new(
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
backend.update_memory(mem).unwrap();
}

#[test]
Expand All @@ -532,5 +539,17 @@ pub mod tests {

backend.set_event_idx(true);
assert_eq!(backend.read().unwrap().event_idx, true);

let _ = backend.exit_event(0).unwrap();

let mem = GuestMemoryAtomic::new(
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
backend.update_memory(mem.clone()).unwrap();

let vring = VringRwLock::new(mem, 0x1000);
backend
.handle_event(0x1, EventSet::IN, &[vring], 0)
.unwrap();
}
}
33 changes: 29 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ where
mod tests {
use super::backend::tests::MockVhostBackend;
use super::*;
use std::os::unix::net::UnixStream;
use std::sync::Barrier;
use vm_memory::{GuestAddress, GuestMemoryAtomic, GuestMemoryMmap};

#[test]
Expand All @@ -179,9 +181,32 @@ mod tests {
GuestMemoryMmap::<()>::from_ranges(&[(GuestAddress(0x100000), 0x10000)]).unwrap(),
);
let backend = Arc::new(Mutex::new(MockVhostBackend::new()));
let daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();

assert_eq!(daemon.get_epoll_handlers().len(), 2);
//daemon.start(Listener::new()).unwrap();
let mut daemon = VhostUserDaemon::new("test".to_owned(), backend, mem).unwrap();

let handlers = daemon.get_epoll_handlers();
assert_eq!(handlers.len(), 2);

let barrier = Arc::new(Barrier::new(2));
let tmpdir = tempfile::tempdir().unwrap();
let mut path = tmpdir.path().to_path_buf();
path.push("socket");

let barrier2 = barrier.clone();
let path1 = path.clone();
let thread = thread::spawn(move || {
barrier2.wait();
let socket = UnixStream::connect(&path1).unwrap();
barrier2.wait();
drop(socket)
});

let listener = Listener::new(&path, false).unwrap();
barrier.wait();
daemon.start(listener).unwrap();
barrier.wait();
// Above process generates a `HandleRequest(PartialMessage)` error.
daemon.wait().unwrap_err();
daemon.wait().unwrap();
thread.join().unwrap();
}
}

0 comments on commit 4e16112

Please sign in to comment.