mirror of https://github.com/Trivernis/bromine.git
Add more tests and descriptions for tests
Signed-off-by: trivernis <trivernis@protonmail.com>pull/25/head
parent
cda472d3a9
commit
8146fe8446
@ -0,0 +1,57 @@
|
||||
use bromine::context::Context;
|
||||
use bromine::event::Event;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
use typemap_rev::TypeMapKey;
|
||||
|
||||
pub async fn get_counter_from_context(ctx: &Context) -> CallCounter {
|
||||
let data = ctx.data.read().await;
|
||||
|
||||
data.get::<CallCounterKey>().unwrap().clone()
|
||||
}
|
||||
|
||||
pub async fn increment_counter_for_event(ctx: &Context, event: &Event) {
|
||||
let data = ctx.data.read().await;
|
||||
data.get::<CallCounterKey>()
|
||||
.unwrap()
|
||||
.incr(event.name())
|
||||
.await;
|
||||
}
|
||||
|
||||
pub struct CallCounterKey;
|
||||
|
||||
impl TypeMapKey for CallCounterKey {
|
||||
type Value = CallCounter;
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Debug)]
|
||||
pub struct CallCounter {
|
||||
inner: Arc<RwLock<HashMap<String, AtomicUsize>>>,
|
||||
}
|
||||
|
||||
impl CallCounter {
|
||||
pub async fn incr(&self, name: &str) {
|
||||
{
|
||||
let calls = self.inner.read().await;
|
||||
if let Some(call) = calls.get(name) {
|
||||
call.fetch_add(1, Ordering::Relaxed);
|
||||
return;
|
||||
}
|
||||
}
|
||||
{
|
||||
let mut calls = self.inner.write().await;
|
||||
calls.insert(name.to_string(), AtomicUsize::new(1));
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get(&self, name: &str) -> usize {
|
||||
let calls = self.inner.read().await;
|
||||
|
||||
calls
|
||||
.get(name)
|
||||
.map(|n| n.load(Ordering::SeqCst))
|
||||
.unwrap_or(0)
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::atomic::{AtomicU8, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn get_free_port() -> u8 {
|
||||
lazy_static! {
|
||||
static ref PORT_COUNTER: Arc<AtomicU8> = Arc::new(AtomicU8::new(0));
|
||||
}
|
||||
PORT_COUNTER.fetch_add(1, Ordering::Relaxed)
|
||||
}
|
Loading…
Reference in New Issue