Skip to main content

libcgroups/
test_manager.rs

1use std::cell::RefCell;
2use std::convert::Infallible;
3
4use nix::unistd::Pid;
5
6use crate::common::{CgroupManager, ControllerOpt, FreezerState};
7use crate::stats::Stats;
8
9#[derive(Debug)]
10pub struct TestManager {
11    add_task_args: RefCell<Vec<Pid>>,
12    pub apply_called: RefCell<bool>,
13}
14
15impl Default for TestManager {
16    fn default() -> Self {
17        Self {
18            add_task_args: RefCell::new(vec![]),
19            apply_called: RefCell::new(false),
20        }
21    }
22}
23
24impl CgroupManager for TestManager {
25    type Error = Infallible;
26
27    fn add_task(&self, pid: Pid) -> Result<(), Infallible> {
28        self.add_task_args.borrow_mut().push(pid);
29        Ok(())
30    }
31
32    // NOTE: The argument cannot be stored due to lifetime.
33    fn apply(&self, _controller_opt: &ControllerOpt) -> Result<(), Infallible> {
34        *self.apply_called.borrow_mut() = true;
35        Ok(())
36    }
37
38    fn remove(&self) -> Result<(), Infallible> {
39        unimplemented!()
40    }
41
42    fn freeze(&self, _state: FreezerState) -> Result<(), Infallible> {
43        unimplemented!()
44    }
45
46    fn stats(&self) -> Result<Stats, Infallible> {
47        unimplemented!()
48    }
49
50    fn get_all_pids(&self) -> Result<Vec<Pid>, Infallible> {
51        unimplemented!()
52    }
53}
54
55impl TestManager {
56    pub fn get_add_task_args(&self) -> Vec<Pid> {
57        self.add_task_args.borrow_mut().clone()
58    }
59
60    pub fn apply_called(&self) -> bool {
61        *self.apply_called.borrow_mut()
62    }
63}