1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-06-25 06:39:22 +02:00

migrate to tokio

This commit is contained in:
Nikolay Kim
2018-05-24 21:03:16 -07:00
parent 565bcfb561
commit 690169db89
22 changed files with 162 additions and 162 deletions

View File

@ -4,21 +4,20 @@ extern crate bytes;
extern crate futures;
extern crate h2;
extern crate http;
extern crate tokio_core;
extern crate tokio_timer;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
use std::io;
use std::time::Duration;
use std::time::{Duration, Instant};
use actix::*;
use actix_web::*;
use bytes::Bytes;
use futures::Future;
use http::StatusCode;
use serde_json::Value;
use tokio_core::reactor::Timeout;
use tokio_timer::Delay;
#[derive(Deserialize)]
struct PParam {
@ -75,8 +74,7 @@ fn test_async_extractor_async() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route().with(|data: Json<Value>| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(move |_| Ok(format!("{}", data.0)))
.responder()
})
@ -171,8 +169,7 @@ fn test_path_and_query_extractor2_async() {
app.resource("/{username}/index.html", |r| {
r.route()
.with3(|p: Path<PParam>, _: Query<PParam>, data: Json<Value>| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
})
@ -201,8 +198,7 @@ fn test_path_and_query_extractor3_async() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route().with2(|p: Path<PParam>, data: Json<Value>| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
})
@ -227,8 +223,7 @@ fn test_path_and_query_extractor4_async() {
let mut srv = test::TestServer::new(|app| {
app.resource("/{username}/index.html", |r| {
r.route().with2(|data: Json<Value>, p: Path<PParam>| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
})
@ -254,8 +249,7 @@ fn test_path_and_query_extractor2_async2() {
app.resource("/{username}/index.html", |r| {
r.route()
.with3(|p: Path<PParam>, data: Json<Value>, _: Query<PParam>| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
})
@ -294,8 +288,7 @@ fn test_path_and_query_extractor2_async3() {
app.resource("/{username}/index.html", |r| {
r.route()
.with3(|data: Json<Value>, p: Path<PParam>, _: Query<PParam>| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", p.username, data.0))
})
@ -334,8 +327,7 @@ fn test_path_and_query_extractor2_async4() {
app.resource("/{username}/index.html", |r| {
r.route()
.with(|data: (Json<Value>, Path<PParam>, Query<PParam>)| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(move |_| {
Ok(format!("Welcome {} - {}!", data.1.username, (data.0).0))
})
@ -443,8 +435,8 @@ fn test_nested_scope_and_path_extractor() {
fn test_impl_trait(
data: (Json<Value>, Path<PParam>, Query<PParam>),
) -> impl Future<Item = String, Error = io::Error> {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.map_err(|_| io::Error::new(io::ErrorKind::Other, "timeout"))
.and_then(move |_| Ok(format!("Welcome {} - {}!", data.1.username, (data.0).0)))
}
@ -452,8 +444,8 @@ fn test_impl_trait(
fn test_impl_trait_err(
_data: (Json<Value>, Path<PParam>, Query<PParam>),
) -> impl Future<Item = String, Error = io::Error> {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.map_err(|_| io::Error::new(io::ErrorKind::Other, "timeout"))
.and_then(move |_| Err(io::Error::new(io::ErrorKind::Other, "other")))
}

View File

@ -1,17 +1,16 @@
extern crate actix;
extern crate actix_web;
extern crate futures;
extern crate tokio_core;
extern crate tokio_timer;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use std::time::{Duration, Instant};
use actix::*;
use actix_web::*;
use futures::{future, Future};
use tokio_core::reactor::Timeout;
use tokio_timer::Delay;
struct MiddlewareTest {
start: Arc<AtomicUsize>,
@ -245,8 +244,7 @@ fn test_middleware_async_handler() {
})
.resource("/", |r| {
r.route().a(|_| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(|_| Ok(HttpResponse::Ok()))
})
})
@ -281,8 +279,7 @@ fn test_resource_middleware_async_handler() {
App::new().resource("/test", |r| {
r.middleware(mw);
r.route().a(|_| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(|_| Ok(HttpResponse::Ok()))
})
})
@ -317,8 +314,7 @@ fn test_scope_middleware_async_handler() {
})
.resource("/test", |r| {
r.route().a(|_| {
Timeout::new(Duration::from_millis(10), &Arbiter::handle())
.unwrap()
Delay::new(Instant::now() + Duration::from_millis(10))
.and_then(|_| Ok(HttpResponse::Ok()))
})
})
@ -436,7 +432,7 @@ struct MiddlewareAsyncTest {
impl<S> middleware::Middleware<S> for MiddlewareAsyncTest {
fn start(&self, _: &mut HttpRequest<S>) -> Result<middleware::Started> {
let to = Timeout::new(Duration::from_millis(10), &Arbiter::handle()).unwrap();
let to = Delay::new(Instant::now() + Duration::from_millis(10));
let start = Arc::clone(&self.start);
Ok(middleware::Started::Future(Box::new(
@ -450,7 +446,7 @@ impl<S> middleware::Middleware<S> for MiddlewareAsyncTest {
fn response(
&self, _: &mut HttpRequest<S>, resp: HttpResponse,
) -> Result<middleware::Response> {
let to = Timeout::new(Duration::from_millis(10), &Arbiter::handle()).unwrap();
let to = Delay::new(Instant::now() + Duration::from_millis(10));
let response = Arc::clone(&self.response);
Ok(middleware::Response::Future(Box::new(
@ -462,7 +458,7 @@ impl<S> middleware::Middleware<S> for MiddlewareAsyncTest {
}
fn finish(&self, _: &mut HttpRequest<S>, _: &HttpResponse) -> middleware::Finished {
let to = Timeout::new(Duration::from_millis(10), &Arbiter::handle()).unwrap();
let to = Delay::new(Instant::now() + Duration::from_millis(10));
let finish = Arc::clone(&self.finish);
middleware::Finished::Future(Box::new(to.from_err().and_then(move |_| {

View File

@ -6,7 +6,9 @@ extern crate futures;
extern crate h2;
extern crate http as modhttp;
extern crate rand;
extern crate tokio_core;
extern crate tokio;
extern crate tokio_reactor;
extern crate tokio_tcp;
#[cfg(feature = "brotli")]
extern crate brotli2;
@ -25,8 +27,9 @@ use rand::Rng;
use std::io::{Read, Write};
use std::sync::{mpsc, Arc};
use std::{net, thread, time};
use tokio_core::net::TcpStream;
use tokio_core::reactor::Core;
use tokio::executor::current_thread;
use tokio::runtime::current_thread::Runtime;
use tokio_tcp::TcpStream;
use actix::System;
use actix_web::*;
@ -790,9 +793,8 @@ fn test_h2() {
let srv = test::TestServer::new(|app| app.handler(|_| HttpResponse::Ok().body(STR)));
let addr = srv.addr();
let mut core = Core::new().unwrap();
let handle = core.handle();
let tcp = TcpStream::connect(&addr, &handle);
let mut core = Runtime::new().unwrap();
let tcp = TcpStream::connect(&addr);
let tcp = tcp
.then(|res| h2client::handshake(res.unwrap()))
@ -806,7 +808,7 @@ fn test_h2() {
let (response, _) = client.send_request(request, false).unwrap();
// Spawn a task to run the conn...
handle.spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
current_thread::spawn(h2.map_err(|e| println!("GOT ERR={:?}", e)));
response.and_then(|response| {
assert_eq!(response.status(), http::StatusCode::OK);
@ -819,7 +821,7 @@ fn test_h2() {
})
})
});
let _res = core.run(tcp);
let _res = core.block_on(tcp);
// assert_eq!(_res.unwrap(), Bytes::from_static(STR.as_ref()));
}