Assert Timings #356
Answered
by
asomers
StefanSchoof
asked this question in
Questions
Assert Timings
#356
-
I have a scenario where the time between calls is something I like to test. I want to send a code over gpio pins and the time how long a high and how long a low signal is the key thing. Can I use mockall for this purpose? If yes, can someone point me to the docs and/or an example. |
Beta Was this translation helpful? Give feedback.
Answered by
asomers
Feb 2, 2022
Replies: 1 comment 3 replies
-
The timing won't be precise, but you should be able to do it by inserting sleeps into the returner. Like this: let mut mock = MockMyThing::default();
mock.expect_go_high()
.returning(|| {
std::thread::sleep(std::time::Duration::from_millis(10));
Ok(())
}) Or are you saying you want to assert that the code under test slept for a certain amount of time? If so, you could try something like this: let mut high_time = None;
let mut low_time = None;
let mut mock = MockMyThing();
mock.expect_go_high()
.returning(|| {
high_time = Some(std::time::Instant::now());
Ok(())
});
mock.expect_go_low()
.returning(|| {
low_time = Some(std::time::Instant::now());
Ok(())
});
code_under_test(mock);
assert_ge(high_time.unwrap() - low_time.unwrap(), std::time::Duration::from_millis(10)); |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
StefanSchoof
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The timing won't be precise, but you should be able to do it by inserting sleeps into the returner. Like this:
Or are you saying you want to assert that the code under test slept for a certain amount of time? If so, you could try something like this: