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

Test: WebSocket close behavior. #3093

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions actix-web-actors/tests/test_ws.rs
Expand Up @@ -52,6 +52,10 @@ async fn common_test_code(mut srv: actix_test::TestServer, frame_size: usize) {
.unwrap();
let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));

let nothing = actix_rt::time::timeout(std::time::Duration::from_secs(1), framed.next()).await;
assert_eq!(true, nothing.is_ok());
assert_eq!(true, nothing.unwrap().is_none());
}

#[actix_rt::test]
Expand Down
96 changes: 96 additions & 0 deletions actix-web-actors/tests/test_ws_close.rs
@@ -0,0 +1,96 @@
use actix::prelude::*;
use actix_web::{web, App, HttpRequest};
use actix_web_actors::ws;
use futures_util::{SinkExt as _, StreamExt as _};
use tokio::sync::mpsc::Sender;

struct Ws {
finished: Sender<()>,
}

impl Actor for Ws {
type Context = ws::WebsocketContext<Self>;
}

impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for Ws {
fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
match msg {
Ok(ws::Message::Close(reason)) => ctx.close(reason),
_ => ctx.close(Some(ws::CloseCode::Normal.into())),
}
}

fn finished(&mut self, _ctx: &mut Self::Context) {
_ = self.finished.try_send(()).unwrap();

Check failure on line 24 in actix-web-actors/tests/test_ws_close.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] reported by reviewdog 🐶 error: this let-binding has unit value --> actix-web-actors/tests/test_ws_close.rs:24:9 | 24 | _ = self.finished.try_send(()).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `self.finished.try_send(()).unwrap();` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value = note: `-D clippy::let-unit-value` implied by `-D warnings` Raw Output: actix-web-actors/tests/test_ws_close.rs:24:9:e:error: this let-binding has unit value --> actix-web-actors/tests/test_ws_close.rs:24:9 | 24 | _ = self.finished.try_send(()).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `self.finished.try_send(()).unwrap();` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value = note: `-D clippy::let-unit-value` implied by `-D warnings` __END__
}
}

#[actix_rt::test]
async fn close_initiated_by_client() {
let (tx, mut finished) = tokio::sync::mpsc::channel(1);
let mut srv = actix_test::start(move || {
let tx = tx.clone();
App::new().service(web::resource("{anything:.*}").to(
move |req: HttpRequest, stream: web::Payload| {
let tx: Sender<()> = tx.clone();
async move { ws::WsResponseBuilder::new(Ws { finished: tx }, &req, stream).start() }
},
))
});

let mut framed = srv.ws().await.unwrap();

framed
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
.await
.unwrap();
let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));

let nothing = actix_rt::time::timeout(std::time::Duration::from_secs(1), framed.next()).await;
assert_eq!(true, nothing.is_ok());

Check failure on line 51 in actix-web-actors/tests/test_ws_close.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] reported by reviewdog 🐶 error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:51:5 | 51 | assert_eq!(true, nothing.is_ok()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison = note: `-D clippy::bool-assert-comparison` implied by `-D warnings` help: replace it with `assert!(..)` | 51 - assert_eq!(true, nothing.is_ok()); 51 + assert!(nothing.is_ok()); | Raw Output: actix-web-actors/tests/test_ws_close.rs:51:5:e:error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:51:5 | 51 | assert_eq!(true, nothing.is_ok()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison = note: `-D clippy::bool-assert-comparison` implied by `-D warnings` help: replace it with `assert!(..)` | 51 - assert_eq!(true, nothing.is_ok()); 51 + assert!(nothing.is_ok()); | __END__
assert_eq!(true, nothing.unwrap().is_none());

Check failure on line 52 in actix-web-actors/tests/test_ws_close.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] reported by reviewdog 🐶 error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:52:5 | 52 | assert_eq!(true, nothing.unwrap().is_none()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 52 - assert_eq!(true, nothing.unwrap().is_none()); 52 + assert!(nothing.unwrap().is_none()); | Raw Output: actix-web-actors/tests/test_ws_close.rs:52:5:e:error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:52:5 | 52 | assert_eq!(true, nothing.unwrap().is_none()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 52 - assert_eq!(true, nothing.unwrap().is_none()); 52 + assert!(nothing.unwrap().is_none()); | __END__

let finished =
actix_rt::time::timeout(std::time::Duration::from_secs(1), finished.recv()).await;
assert_eq!(true, finished.is_ok());

Check failure on line 56 in actix-web-actors/tests/test_ws_close.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] reported by reviewdog 🐶 error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:56:5 | 56 | assert_eq!(true, finished.is_ok()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 56 - assert_eq!(true, finished.is_ok()); 56 + assert!(finished.is_ok()); | Raw Output: actix-web-actors/tests/test_ws_close.rs:56:5:e:error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:56:5 | 56 | assert_eq!(true, finished.is_ok()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 56 - assert_eq!(true, finished.is_ok()); 56 + assert!(finished.is_ok()); | __END__
assert_eq!(Some(()), finished.unwrap());
}

#[actix_rt::test]
async fn close_initiated_by_server() {
let (tx, mut finished) = tokio::sync::mpsc::channel(1);
let mut srv = actix_test::start(move || {
let tx = tx.clone();
App::new().service(web::resource("{anything:.*}").to(
move |req: HttpRequest, stream: web::Payload| {
let tx: Sender<()> = tx.clone();
async move { ws::WsResponseBuilder::new(Ws { finished: tx }, &req, stream).start() }
},
))
});

let mut framed = srv.ws().await.unwrap();

framed
.send(ws::Message::Text("I'll initiate close by server".into()))
.await
.unwrap();

let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));

framed
.send(ws::Message::Close(Some(ws::CloseCode::Normal.into())))
.await
.unwrap();

let nothing = actix_rt::time::timeout(std::time::Duration::from_secs(1), framed.next()).await;
assert_eq!(true, nothing.is_ok());

Check failure on line 89 in actix-web-actors/tests/test_ws_close.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] reported by reviewdog 🐶 error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:89:5 | 89 | assert_eq!(true, nothing.is_ok()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 89 - assert_eq!(true, nothing.is_ok()); 89 + assert!(nothing.is_ok()); | Raw Output: actix-web-actors/tests/test_ws_close.rs:89:5:e:error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:89:5 | 89 | assert_eq!(true, nothing.is_ok()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 89 - assert_eq!(true, nothing.is_ok()); 89 + assert!(nothing.is_ok()); | __END__
assert_eq!(true, nothing.unwrap().is_none());

Check failure on line 90 in actix-web-actors/tests/test_ws_close.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] reported by reviewdog 🐶 error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:90:5 | 90 | assert_eq!(true, nothing.unwrap().is_none()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 90 - assert_eq!(true, nothing.unwrap().is_none()); 90 + assert!(nothing.unwrap().is_none()); | Raw Output: actix-web-actors/tests/test_ws_close.rs:90:5:e:error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:90:5 | 90 | assert_eq!(true, nothing.unwrap().is_none()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 90 - assert_eq!(true, nothing.unwrap().is_none()); 90 + assert!(nothing.unwrap().is_none()); | __END__

let finished =
actix_rt::time::timeout(std::time::Duration::from_secs(1), finished.recv()).await;
assert_eq!(true, finished.is_ok());

Check failure on line 94 in actix-web-actors/tests/test_ws_close.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] reported by reviewdog 🐶 error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:94:5 | 94 | assert_eq!(true, finished.is_ok()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 94 - assert_eq!(true, finished.is_ok()); 94 + assert!(finished.is_ok()); | Raw Output: actix-web-actors/tests/test_ws_close.rs:94:5:e:error: used `assert_eq!` with a literal bool --> actix-web-actors/tests/test_ws_close.rs:94:5 | 94 | assert_eq!(true, finished.is_ok()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison help: replace it with `assert!(..)` | 94 - assert_eq!(true, finished.is_ok()); 94 + assert!(finished.is_ok()); | __END__
assert_eq!(Some(()), finished.unwrap());
}
4 changes: 4 additions & 0 deletions awc/tests/test_ws.rs
Expand Up @@ -65,4 +65,8 @@ async fn test_simple() {

let item = framed.next().await.unwrap().unwrap();
assert_eq!(item, ws::Frame::Close(Some(ws::CloseCode::Normal.into())));

let nothing = actix_rt::time::timeout(std::time::Duration::from_secs(1), framed.next()).await;
assert_eq!(true, nothing.is_ok());
assert_eq!(true, nothing.unwrap().is_none());
}