mirror of https://github.com/Trivernis/bromine.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.9 KiB
Rust
102 lines
2.9 KiB
Rust
3 years ago
|
use crate::utils::call_counter::increment_counter_for_event;
|
||
|
use crate::utils::protocol::TestProtocolListener;
|
||
|
use crate::utils::{get_free_port, start_server_and_client};
|
||
|
use bromine::prelude::encrypted::EncryptedListener;
|
||
|
use bromine::prelude::*;
|
||
|
use bromine::IPCBuilder;
|
||
|
use byteorder::{BigEndian, ReadBytesExt};
|
||
|
use bytes::{BufMut, Bytes, BytesMut};
|
||
|
use futures::StreamExt;
|
||
|
use rand_core::RngCore;
|
||
|
use std::io::Read;
|
||
|
use std::time::Duration;
|
||
|
|
||
|
mod utils;
|
||
|
|
||
|
#[tokio::test]
|
||
|
async fn it_sends_and_receives() {
|
||
|
let ctx = get_client_with_server().await;
|
||
|
let mut rng = rand::thread_rng();
|
||
|
let mut buffer = vec![0u8; 140];
|
||
|
rng.fill_bytes(&mut buffer);
|
||
|
|
||
|
let mut stream = ctx
|
||
|
.emit("bytes", BytePayload::new(buffer.clone()))
|
||
|
.stream_replies()
|
||
|
.await
|
||
|
.unwrap();
|
||
|
let mut count = 0;
|
||
|
|
||
|
while let Some(Ok(response)) = stream.next().await {
|
||
|
let bytes = response.payload::<BytePayload>().unwrap();
|
||
|
assert_eq!(bytes.into_inner(), buffer);
|
||
|
count += 1;
|
||
|
}
|
||
|
assert_eq!(count, 100)
|
||
|
}
|
||
|
|
||
|
#[tokio::test]
|
||
|
async fn it_sends_and_receives_strings() {
|
||
|
let ctx = get_client_with_server().await;
|
||
|
let response = ctx
|
||
|
.emit("string", StringPayload(String::from("Hello World")))
|
||
|
.await_reply()
|
||
|
.await
|
||
|
.unwrap();
|
||
|
let response_string = response.payload::<StringPayload>().unwrap().0;
|
||
|
|
||
|
assert_eq!(&response_string, "Hello World")
|
||
|
}
|
||
|
|
||
|
async fn get_client_with_server() -> Context {
|
||
|
let port = get_free_port();
|
||
|
|
||
|
start_server_and_client(move || get_builder(port)).await
|
||
|
}
|
||
|
|
||
|
fn get_builder(port: u8) -> IPCBuilder<EncryptedListener<TestProtocolListener>> {
|
||
|
IPCBuilder::new()
|
||
|
.address(port)
|
||
|
.on("bytes", callback!(handle_bytes))
|
||
|
.on("string", callback!(handle_string))
|
||
|
.timeout(Duration::from_millis(100))
|
||
|
}
|
||
|
|
||
|
async fn handle_bytes(ctx: &Context, event: Event) -> IPCResult<Response> {
|
||
|
increment_counter_for_event(ctx, &event).await;
|
||
|
let bytes = event.payload::<BytePayload>()?.into_bytes();
|
||
|
|
||
|
for _ in 0u8..99 {
|
||
|
ctx.emit("bytes", BytePayload::from(bytes.clone())).await?;
|
||
|
}
|
||
|
|
||
|
ctx.response(BytePayload::from(bytes))
|
||
|
}
|
||
|
|
||
|
async fn handle_string(ctx: &Context, event: Event) -> IPCResult<Response> {
|
||
|
ctx.response(event.payload::<StringPayload>()?)
|
||
|
}
|
||
|
|
||
|
pub struct StringPayload(String);
|
||
|
|
||
|
impl IntoPayload for StringPayload {
|
||
|
fn into_payload(self, _: &Context) -> IPCResult<Bytes> {
|
||
|
let mut buf = BytesMut::with_capacity(self.0.len() + 4);
|
||
|
buf.put_u32(self.0.len() as u32);
|
||
|
buf.put(Bytes::from(self.0));
|
||
|
|
||
|
Ok(buf.freeze())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl FromPayload for StringPayload {
|
||
|
fn from_payload<R: Read>(mut reader: R) -> IPCResult<Self> {
|
||
|
let len = reader.read_u32::<BigEndian>()?;
|
||
|
let mut buf = vec![0u8; len as usize];
|
||
|
reader.read_exact(&mut buf)?;
|
||
|
let string = String::from_utf8(buf).map_err(|_| IPCError::from("not a string"))?;
|
||
|
|
||
|
Ok(StringPayload(string))
|
||
|
}
|
||
|
}
|