-
Notifications
You must be signed in to change notification settings - Fork 296
/
tests.rs
187 lines (175 loc) · 6.75 KB
/
tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use std::{
sync::mpsc::{self, Receiver, Sender},
thread,
};
use ic_interfaces::execution_environment::{HypervisorError, OutOfInstructionsHandler};
use ic_types::NumInstructions;
use crate::dts::MAX_NUM_SLICES;
use super::{DeterministicTimeSlicingHandler, PausedExecution};
#[test]
fn dts_state_updates() {
let mut state = super::State::new(2500, 1000);
assert_eq!(state.slice_instruction_limit, 1000);
assert_eq!(state.instructions_executed, 0);
assert_eq!(state.total_instructions_left(), 2500);
assert!(!state.is_last_slice());
state.update(0);
assert_eq!(state.slice_instruction_limit, 1000);
assert_eq!(state.instructions_executed, 1000);
assert_eq!(state.total_instructions_left(), 1500);
assert!(!state.is_last_slice());
state.update(0);
assert_eq!(state.slice_instruction_limit, 500);
assert_eq!(state.instructions_executed, 2000);
assert_eq!(state.total_instructions_left(), 500);
assert!(state.is_last_slice());
state.update(-500);
assert_eq!(state.slice_instruction_limit, 0);
assert_eq!(state.instructions_executed, 3000);
assert_eq!(state.total_instructions_left(), -500);
assert!(state.is_last_slice());
}
#[test]
fn dts_state_updates_invalid_instructions() {
let mut state = super::State::new(2500, 2000);
assert_eq!(state.slice_instruction_limit, 2000);
assert_eq!(state.instructions_executed, 0);
assert_eq!(state.total_instructions_left(), 2500);
assert!(!state.is_last_slice());
state.update(4000);
assert_eq!(state.slice_instruction_limit, 2000);
assert_eq!(state.instructions_executed, 0);
assert_eq!(state.total_instructions_left(), 2500);
assert!(!state.is_last_slice());
}
#[test]
fn dts_state_updates_saturating() {
let mut state = super::State::new(2500, 2000);
assert_eq!(state.slice_instruction_limit, 2000);
assert_eq!(state.instructions_executed, 0);
assert_eq!(state.total_instructions_left(), 2500);
assert!(!state.is_last_slice());
state.update(i64::MIN);
assert_eq!(state.slice_instruction_limit, 0);
assert_eq!(state.instructions_executed, i64::MAX);
assert_eq!(state.total_instructions_left(), 2500 - i64::MAX);
assert!(state.is_last_slice());
}
#[test]
fn dts_max_num_slices() {
let mut state = super::State::new(1000000, 100);
let mut iterations = 0;
while !state.is_last_slice() {
state.update(99);
iterations += 1;
}
assert!(iterations <= MAX_NUM_SLICES);
}
#[test]
fn pause_and_resume_works() {
let (tx, rx): (Sender<PausedExecution>, Receiver<PausedExecution>) = mpsc::channel();
let dts = DeterministicTimeSlicingHandler::new(2500, 1000, move |_slice, paused| {
tx.send(paused).unwrap();
});
let control_thread = thread::spawn(move || {
for _ in 0..2 {
let paused_execution = rx.recv().unwrap();
std::thread::sleep(std::time::Duration::from_millis(1));
paused_execution.resume();
}
});
// Slice 1: executes 1000 instructions before calling `out_of_instructions()`.
let next_slice_limit = dts.out_of_instructions(0).unwrap();
assert_eq!(1000, next_slice_limit);
// Slice 2: executes 1000 instructions before calling `out_of_instructions()`.
let next_slice_limit = dts.out_of_instructions(0).unwrap();
assert_eq!(500, next_slice_limit);
// Slice 3: executes 500 instructions before calling `out_of_instructions()`.
let error = dts.out_of_instructions(0);
assert_eq!(error, Err(HypervisorError::InstructionLimitExceeded));
drop(dts);
control_thread.join().unwrap();
}
#[test]
fn early_exit_if_slice_does_not_any_instructions_left() {
let (tx, rx): (Sender<PausedExecution>, Receiver<PausedExecution>) = mpsc::channel();
let dts = DeterministicTimeSlicingHandler::new(10000, 1000, move |_slice, paused| {
tx.send(paused).unwrap();
});
let control_thread = thread::spawn(move || {
let paused_execution = rx.recv().unwrap();
std::thread::sleep(std::time::Duration::from_millis(1));
paused_execution.resume();
});
// Slice 1: executes 1500 instructions before calling `out_of_instructions()`.
let new_slice_limit = dts.out_of_instructions(-500).unwrap();
assert_eq!(500, new_slice_limit);
// Slice 2: executes 1500 instructions before calling `out_of_instructions()`
// and fails because the next slice wouldn't have any slice instructions left.
let error = dts.out_of_instructions(-1000);
assert_eq!(
error,
Err(HypervisorError::SliceOverrun {
instructions: NumInstructions::from(1500),
limit: NumInstructions::from(1000)
})
);
drop(dts);
control_thread.join().unwrap();
}
#[test]
fn invalid_instructions() {
let (tx, rx): (Sender<PausedExecution>, Receiver<PausedExecution>) = mpsc::channel();
let dts = DeterministicTimeSlicingHandler::new(2500, 1000, move |_slice, paused| {
tx.send(paused).unwrap();
});
let control_thread = thread::spawn(move || {
for _ in 0..2 {
let paused_execution = rx.recv().unwrap();
std::thread::sleep(std::time::Duration::from_millis(1));
paused_execution.resume();
}
});
// Slice 1: executes 1000 instructions before calling `out_of_instructions()`.
let new_instructions = dts.out_of_instructions(0).unwrap();
assert_eq!(1000, new_instructions);
// Slice 2: executes 0 instructions before calling `out_of_instructions()`.
let new_instructions = dts.out_of_instructions(i64::MAX).unwrap();
assert_eq!(1000, new_instructions);
// Slice 3: executes more than i64::MAX instructions before calling `out_of_instructions()`.
let error = dts.out_of_instructions(i64::MIN);
assert_eq!(
error,
Err(HypervisorError::SliceOverrun {
instructions: NumInstructions::from(9223372036854775807),
limit: NumInstructions::from(1000)
})
);
drop(dts);
control_thread.join().unwrap();
}
#[test]
fn max_num_slices() {
let (tx, rx): (Sender<PausedExecution>, Receiver<PausedExecution>) = mpsc::channel();
let dts = DeterministicTimeSlicingHandler::new(1000000, 100, move |_slice, paused| {
tx.send(paused).unwrap();
});
let control_thread = thread::spawn(move || {
for _ in 0..MAX_NUM_SLICES - 1 {
let paused_execution = rx.recv().unwrap();
paused_execution.resume();
}
});
let mut result = Ok(0);
for i in 0..MAX_NUM_SLICES {
result = dts.out_of_instructions(99);
if i < MAX_NUM_SLICES - 1 {
assert!(result.is_ok());
} else {
assert!(result.is_err());
}
}
assert_eq!(result, Err(HypervisorError::InstructionLimitExceeded));
drop(dts);
control_thread.join().unwrap();
}