Add tests for namespace and restructure tests

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/25/head
trivernis 3 years ago
parent 8146fe8446
commit 5b54140011
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,13 +1,11 @@
mod call_counter;
mod protocol;
mod utils;
use crate::utils::start_server_and_client;
use bromine::prelude::*;
use call_counter::*;
use protocol::*;
use std::time::Duration;
use tokio::sync::oneshot::channel;
use utils::call_counter::*;
use utils::get_free_port;
use utils::protocol::*;
/// Simple events are passed from the client to the server and responses
/// are emitted back to the client. Both will have received an event.
@ -25,6 +23,22 @@ async fn it_sends_events() {
assert_eq!(counter.get("pong").await, 1);
}
/// Events sent to a specific namespace are handled by the namespace event handler
#[tokio::test]
async fn it_sends_namespaced_events() {
let port = get_free_port();
let ctx = get_client_with_server(port).await;
ctx.emitter.emit_to("test", "ping", ()).await.unwrap();
ctx.emitter.emit_to("test", "pong", ()).await.unwrap();
// allow the event to be processed
tokio::time::sleep(Duration::from_millis(10)).await;
let counter = get_counter_from_context(&ctx).await;
assert_eq!(counter.get("test:ping").await, 1);
assert_eq!(counter.get("test:pong").await, 1);
}
/// When awaiting the reply to an event the handler for the event doesn't get called.
/// Therefore we expect it to have a call count of 0.
#[tokio::test]
@ -81,28 +95,7 @@ async fn it_receives_error_responses() {
}
async fn get_client_with_server(port: u8) -> Context {
let counters = CallCounter::default();
let (sender, receiver) = channel::<()>();
tokio::task::spawn({
let counters = counters.clone();
async move {
sender.send(()).unwrap();
get_builder(port)
.insert::<CallCounterKey>(counters)
.build_server()
.await
.unwrap()
}
});
receiver.await.unwrap();
get_builder(port)
.insert::<CallCounterKey>(counters)
.build_client()
.await
.unwrap()
start_server_and_client(move || get_builder(port)).await
}
fn get_builder(port: u8) -> IPCBuilder<TestProtocolListener> {
@ -113,6 +106,11 @@ fn get_builder(port: u8) -> IPCBuilder<TestProtocolListener> {
.on("pong", callback!(handle_pong_event))
.on("create_error", callback!(handle_create_error_event))
.on("error", callback!(handle_error_event))
.namespace("test")
.on("ping", callback!(handle_ping_event))
.on("pong", callback!(handle_pong_event))
.on("create_error", callback!(handle_create_error_event))
.build()
}
async fn handle_ping_event(ctx: &Context, event: Event) -> IPCResult<()> {

@ -1,10 +0,0 @@
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)
}

@ -14,10 +14,14 @@ pub async fn get_counter_from_context(ctx: &Context) -> CallCounter {
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;
let key_name = if let Some(namespace) = event.namespace() {
format!("{}:{}", namespace, event.name())
} else {
event.name().to_string()
};
data.get::<CallCounterKey>().unwrap().incr(&key_name).await;
}
pub struct CallCounterKey;

@ -0,0 +1,45 @@
use bromine::context::Context;
use bromine::protocol::AsyncStreamProtocolListener;
use bromine::IPCBuilder;
use call_counter::*;
use lazy_static::lazy_static;
use std::sync::atomic::{AtomicU8, Ordering};
use std::sync::Arc;
use tokio::sync::oneshot::channel;
pub mod call_counter;
pub mod protocol;
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)
}
pub async fn start_server_and_client<
F: Fn() -> IPCBuilder<L> + Send + Sync + 'static,
L: AsyncStreamProtocolListener,
>(
builder_fn: F,
) -> Context {
let counters = CallCounter::default();
let (sender, receiver) = channel::<()>();
let client_builder = builder_fn().insert::<CallCounterKey>(counters.clone());
tokio::task::spawn({
async move {
sender.send(()).unwrap();
builder_fn()
.insert::<CallCounterKey>(counters)
.build_server()
.await
.unwrap()
}
});
receiver.await.unwrap();
let ctx = client_builder.build_client().await.unwrap();
ctx
}
Loading…
Cancel
Save