1
0
mirror of https://github.com/actix/actix-extras.git synced 2025-06-25 09:59:21 +02:00

actix 0.5.5, ws test

This commit is contained in:
Nikolay Kim
2018-03-19 13:12:36 -07:00
parent e7ec0f9fd7
commit 35ee5d36d8
5 changed files with 61 additions and 11 deletions

View File

@ -91,3 +91,56 @@ fn test_large_bin() {
assert_eq!(item, Some(ws::Message::Binary(Binary::from(data.clone()))));
}
}
struct Ws2 {
count: usize
}
impl Actor for Ws2 {
type Context = ws::WebsocketContext<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
self.send(ctx);
}
}
impl Ws2 {
fn send(&mut self, ctx: &mut ws::WebsocketContext<Self>) {
ctx.text("0".repeat(65_536));
ctx.drain().and_then(|_, act, ctx| {
act.count += 1;
if act.count != 100 {
act.send(ctx);
}
actix::fut::ok(())
}).wait(ctx);
}
}
impl StreamHandler<ws::Message, ws::ProtocolError> for Ws2 {
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
match msg {
ws::Message::Ping(msg) => ctx.pong(&msg),
ws::Message::Text(text) => ctx.text(text),
ws::Message::Binary(bin) => ctx.binary(bin),
ws::Message::Close(reason) => ctx.close(reason, ""),
_ => (),
}
}
}
#[test]
fn test_server_send_text() {
let data = Some(ws::Message::Text("0".repeat(65_536)));
let mut srv = test::TestServer::new(
|app| app.handler(|req| ws::start(req, Ws2{count:0})));
let (mut reader, _writer) = srv.ws().unwrap();
for _ in 0..100 {
let (item, r) = srv.execute(reader.into_future()).unwrap();
reader = r;
assert_eq!(item, data);
}
}