use std::collections::VecDeque;
use std::sync::Mutex;
use windows::core::HRESULT;
use windows::Win32::Foundation::{ERROR_ACCESS_DENIED, HANDLE};
use super::Kernel;
pub(crate) struct FakeKernel {
terminates: Mutex<VecDeque<Result<(), HRESULT>>>,
}
fn access_denied() -> HRESULT {
HRESULT::from_win32(ERROR_ACCESS_DENIED.0)
}
pub(crate) fn refusing_the_next_terminate() -> FakeKernel {
FakeKernel::new().on_terminate(Err(access_denied()))
}
impl FakeKernel {
pub(crate) fn new() -> Self {
FakeKernel {
terminates: Mutex::new(VecDeque::new()),
}
}
pub(crate) fn on_terminate(mut self, answer: Result<(), HRESULT>) -> Self {
self.terminates
.get_mut()
.expect("no thread holds the rows")
.push_back(answer);
self
}
pub(crate) fn all_rows_taken(&self) -> bool {
self.terminates
.lock()
.expect("no thread panics holding the rows")
.is_empty()
}
}
impl Kernel for FakeKernel {
fn terminate(&self, _handle: HANDLE, _exit_code: u32) -> windows::core::Result<()> {
let row = self
.terminates
.lock()
.expect("no thread panics holding the rows")
.pop_front();
match row.expect("the fake kernel has no row left for terminate") {
Ok(()) => Ok(()),
Err(code) => Err(windows::core::Error::from_hresult(code)),
}
}
}