From 2ffda29f9bf56cb357cca7480cd29e6e743e59d3 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 12 Jun 2019 16:15:06 +0600 Subject: [PATCH] Allow to test an app that uses async actors #897 --- CHANGES.md | 2 ++ Cargo.toml | 3 ++- MIGRATION.md | 2 +- src/test.rs | 46 ++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 231cb133f..14400add5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,8 @@ * Move identity middleware to `actix-identity` crate. +* Allow to test an app that uses async actors #897 + ## [1.0.0] - 2019-06-05 diff --git a/Cargo.toml b/Cargo.toml index 08eb7cd9f..871ed7451 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -103,7 +103,8 @@ rustls = { version = "0.15", optional = true } [dev-dependencies] actix-http = { version = "0.2.3", features=["ssl", "brotli", "flate2-zlib"] } actix-http-test = { version = "0.2.0", features=["ssl"] } -actix-files = { version = "0.1.1" } +actix-files = "0.1.1" +actix = { version = "0.8.3" } rand = "0.6" env_logger = "0.6" serde_derive = "1.0" diff --git a/MIGRATION.md b/MIGRATION.md index 8b5d7dd49..a2591a1d5 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -15,7 +15,7 @@ ``` -## 1.0 +## 1.0.0 * Resource registration. 1.0 version uses generalized resource registration via `.service()` method. diff --git a/src/test.rs b/src/test.rs index 89c1a1268..5ab417bd6 100644 --- a/src/test.rs +++ b/src/test.rs @@ -7,7 +7,7 @@ use actix_http::http::{HttpTryFrom, Method, StatusCode, Uri, Version}; use actix_http::test::TestRequest as HttpTestRequest; use actix_http::{cookie::Cookie, Extensions, Request}; use actix_router::{Path, ResourceDef, Url}; -use actix_rt::Runtime; +use actix_rt::{System, SystemRunner}; use actix_server_config::ServerConfig; use actix_service::{IntoNewService, IntoService, NewService, Service}; use bytes::{Bytes, BytesMut}; @@ -29,14 +29,14 @@ use crate::{Error, HttpRequest, HttpResponse}; thread_local! { static RT: RefCell = { - RefCell::new(Inner(Some(Runtime::new().unwrap()))) + RefCell::new(Inner(Some(System::builder().build()))) }; } -struct Inner(Option); +struct Inner(Option); impl Inner { - fn get_mut(&mut self) -> &mut Runtime { + fn get_mut(&mut self) -> &mut SystemRunner { self.0.as_mut().unwrap() } } @@ -714,4 +714,42 @@ mod tests { let res = block_fn(|| app.call(req)).unwrap(); assert!(res.status().is_success()); } + + #[test] + fn test_actor() { + use actix::Actor; + + struct MyActor; + + struct Num(usize); + impl actix::Message for Num { + type Result = usize; + } + impl actix::Actor for MyActor { + type Context = actix::Context; + } + impl actix::Handler for MyActor { + type Result = usize; + fn handle(&mut self, msg: Num, _: &mut Self::Context) -> Self::Result { + msg.0 + } + } + + let addr = run_on(|| MyActor.start()); + let mut app = init_service(App::new().service( + web::resource("/index.html").to_async(move || { + addr.send(Num(1)).from_err().and_then(|res| { + if res == 1 { + HttpResponse::Ok() + } else { + HttpResponse::BadRequest() + } + }) + }), + )); + + let req = TestRequest::post().uri("/index.html").to_request(); + let res = block_fn(|| app.call(req)).unwrap(); + assert!(res.status().is_success()); + } }