summaryrefslogtreecommitdiffstats
path: root/src/search/interrupter.rs
blob: 8778dbe14581affe6e5a1184f102ee5a4889eb42 (plain)
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
use std::{
	ops::Add,
	time::{Duration, Instant},
};

pub(crate) struct Interrupter {
	finish: Instant,
}

impl Interrupter {
	pub(crate) fn new(duration: Duration) -> Self {
		Self {
			finish: Instant::now().add(duration),
		}
	}

	pub(crate) fn should_continue(&self) -> bool {
		Instant::now() < self.finish
	}
}

#[cfg(test)]
mod test {
	use std::ops::Sub;

	use super::*;

	#[test]
	fn should_continue_before_finish() {
		let interrupter = Interrupter::new(Duration::from_secs(60));
		assert!(interrupter.should_continue());
	}

	#[test]
	fn should_continue_after_finish() {
		let interrupter = Interrupter {
			finish: Instant::now().sub(Duration::from_secs(60)),
		};
		assert!(!interrupter.should_continue());
	}
}