mirror of https://github.com/Trivernis/bromine.git
Add event_ids to events
Signed-off-by: trivernis <trivernis@protonmail.com>pull/2/head
parent
8ab420e797
commit
8e483b97cb
@ -1,3 +1,19 @@
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
pub mod error_event;
|
||||
pub mod event;
|
||||
pub mod event_handler;
|
||||
|
||||
/// Generates a new event id
|
||||
pub(crate) fn generate_event_id() -> u64 {
|
||||
lazy_static::lazy_static! {
|
||||
static ref COUNTER: Arc<AtomicU64> = Arc::new(AtomicU64::new(0));
|
||||
}
|
||||
let epoch_elapsed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
|
||||
|
||||
(epoch_elapsed.as_secs() % u16::MAX as u64) << 48
|
||||
| (epoch_elapsed.subsec_millis() as u64) << 32
|
||||
| COUNTER.fetch_add(1, Ordering::SeqCst)
|
||||
}
|
||||
|
@ -0,0 +1,12 @@
|
||||
use crate::events::generate_event_id;
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[test]
|
||||
fn event_ids_work() {
|
||||
let mut ids = HashSet::new();
|
||||
|
||||
// simple collision test
|
||||
for _ in 0..100000 {
|
||||
assert!(ids.insert(generate_event_id()))
|
||||
}
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
mod ipc_tests;
|
||||
mod utils;
|
||||
mod event_tests;
|
||||
|
Loading…
Reference in New Issue