Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute websocket test assertions #394

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions crates/net/src/eventsource/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,38 +236,33 @@ impl PinnedDrop for EventSourceSubscription {
mod tests {
use super::*;
use futures::StreamExt;
use wasm_bindgen_futures::spawn_local;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn eventsource_works() {
async fn eventsource_works() {
let sse_echo_server_url =
option_env!("SSE_ECHO_SERVER_URL").expect("Did you set SSE_ECHO_SERVER_URL?");

let mut es = EventSource::new(sse_echo_server_url).unwrap();
let mut servers = es.subscribe("server").unwrap();
let mut requests = es.subscribe("request").unwrap();

spawn_local(async move {
assert_eq!(servers.next().await.unwrap().unwrap().0, "server");
assert_eq!(requests.next().await.unwrap().unwrap().0, "request");
});
assert_eq!(servers.next().await.unwrap().unwrap().0, "server");
assert_eq!(requests.next().await.unwrap().unwrap().0, "request");
}

#[wasm_bindgen_test]
fn eventsource_connect_failure_works() {
async fn eventsource_connect_failure_works() {
let mut es = EventSource::new("rubbish").unwrap();
let mut servers = es.subscribe("server").unwrap();

spawn_local(async move {
// we should expect an immediate failure
// we should expect an immediate failure

assert_eq!(
servers.next().await,
Some(Err(EventSourceError::ConnectionError))
);
})
assert_eq!(
servers.next().await,
Some(Err(EventSourceError::ConnectionError))
);
}
}
49 changes: 22 additions & 27 deletions crates/net/src/websocket/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,43 +354,38 @@ impl PinnedDrop for WebSocket {
mod tests {
use super::*;
use futures::{SinkExt, StreamExt};
use wasm_bindgen_futures::spawn_local;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn websocket_works() {
async fn websocket_works() {
let ws_echo_server_url =
option_env!("WS_ECHO_SERVER_URL").expect("Did you set WS_ECHO_SERVER_URL?");

let ws = WebSocket::open(ws_echo_server_url).unwrap();
let (mut sender, mut receiver) = ws.split();

spawn_local(async move {
sender
.send(Message::Text(String::from("test 1")))
.await
.unwrap();
sender
.send(Message::Text(String::from("test 2")))
.await
.unwrap();
});

spawn_local(async move {
// ignore first message
// the echo-server uses it to send it's info in the first message
let _ = receiver.next().await;

assert_eq!(
receiver.next().await.unwrap().unwrap(),
Message::Text("test 1".to_string())
);
assert_eq!(
receiver.next().await.unwrap().unwrap(),
Message::Text("test 2".to_string())
);
});
sender
.send(Message::Text(String::from("test 1")))
.await
.unwrap();
sender
.send(Message::Text(String::from("test 2")))
.await
.unwrap();

// ignore first message
// the echo-server uses it to send it's info in the first message
let _ = receiver.next().await;

assert_eq!(
receiver.next().await.unwrap().unwrap(),
Message::Text("test 1".to_string())
);
assert_eq!(
receiver.next().await.unwrap().unwrap(),
Message::Text("test 2".to_string())
);
}
}
21 changes: 8 additions & 13 deletions crates/net/src/websocket/io_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ fn ws_result_to_io_result(res: Result<(), WebSocketError>) -> io::Result<()> {
mod tests {
use super::*;
use futures::{AsyncReadExt, AsyncWriteExt, StreamExt};
use wasm_bindgen_futures::spawn_local;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);
Expand All @@ -128,18 +127,14 @@ mod tests {

let (mut reader, mut writer) = AsyncReadExt::split(ws);

spawn_local(async move {
writer.write_all(b"test 1").await.unwrap();
writer.write_all(b"test 2").await.unwrap();
});

spawn_local(async move {
let mut buf = [0u8; 6];
reader.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"test 1");
reader.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"test 2");
});
writer.write_all(b"test 1").await.unwrap();
writer.write_all(b"test 2").await.unwrap();

let mut buf = [0u8; 6];
reader.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"test 1");
reader.read_exact(&mut buf).await.unwrap();
assert_eq!(&buf, b"test 2");
}

#[wasm_bindgen_test]
Expand Down