Skip to content

Commit

Permalink
migrate actix-web-actors
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Dec 15, 2019
1 parent a791aab commit a153374
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 197 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ open-ssl = { version="0.10", package = "openssl", optional = true }
rust-tls = { version = "0.16.0", package = "rustls", optional = true }

[dev-dependencies]
# actix = "0.8.3"
actix = "0.9.0-alpha.1"
rand = "0.7"
env_logger = "0.6"
serde_derive = "1.0"
Expand Down
4 changes: 4 additions & 0 deletions actix-web-actors/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

## [2.0.0-alpha.1] - 2019-12-15

* Migrate to actix-web 2.0.0

## [1.0.4] - 2019-12-07

* Allow comma-separated websocket subprotocols without spaces (#1172)
Expand Down
19 changes: 9 additions & 10 deletions actix-web-actors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "actix-web-actors"
version = "1.0.4"
version = "2.0.0-alpha.1"
authors = ["Nikolay Kim <[email protected]>"]
description = "Actix actors support for actix web framework."
readme = "README.md"
Expand All @@ -9,22 +9,21 @@ homepage = "https://actix.rs"
repository = "https://github.com/actix/actix-web.git"
documentation = "https://docs.rs/actix-web-actors/"
license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
workspace = ".."
edition = "2018"

[lib]
name = "actix_web_actors"
path = "src/lib.rs"

[dependencies]
actix = "0.8.3"
actix-web = "1.0.9"
actix-http = "0.2.11"
actix-codec = "0.1.2"
bytes = "0.4"
futures = "0.1.25"
actix = "0.9.0-alpha.1"
actix-web = "2.0.0-alpha.5"
actix-http = "1.0.0"
actix-codec = "0.2.0"
bytes = "0.5.2"
futures = "0.3.1"
pin-project = "0.4.6"

[dev-dependencies]
actix-rt = "1.0.0"
env_logger = "0.6"
actix-http-test = { version = "0.2.4", features=["ssl"] }
61 changes: 28 additions & 33 deletions actix-web-actors/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::collections::VecDeque;
use std::pin::Pin;
use std::task::{Context, Poll};

use actix::dev::{
AsyncContextParts, ContextFut, ContextParts, Envelope, Mailbox, ToEnvelope,
Expand All @@ -7,10 +9,10 @@ use actix::fut::ActorFuture;
use actix::{
Actor, ActorContext, ActorState, Addr, AsyncContext, Handler, Message, SpawnHandle,
};
use actix_web::error::{Error, ErrorInternalServerError};
use actix_web::error::Error;
use bytes::Bytes;
use futures::sync::oneshot::Sender;
use futures::{Async, Future, Poll, Stream};
use futures::channel::oneshot::Sender;
use futures::{Future, Stream};

/// Execution context for http actors
pub struct HttpContext<A>
Expand Down Expand Up @@ -43,15 +45,15 @@ where
#[inline]
fn spawn<F>(&mut self, fut: F) -> SpawnHandle
where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
F: ActorFuture<Output = (), Actor = A> + 'static,
{
self.inner.spawn(fut)
}

#[inline]
fn wait<F>(&mut self, fut: F)
where
F: ActorFuture<Item = (), Error = (), Actor = A> + 'static,
F: ActorFuture<Output = (), Actor = A> + 'static,
{
self.inner.wait(fut)
}
Expand Down Expand Up @@ -81,7 +83,7 @@ where
{
#[inline]
/// Create a new HTTP Context from a request and an actor
pub fn create(actor: A) -> impl Stream<Item = Bytes, Error = Error> {
pub fn create(actor: A) -> impl Stream<Item = Result<Bytes, Error>> {
let mb = Mailbox::default();
let ctx = HttpContext {
inner: ContextParts::new(mb.sender_producer()),
Expand All @@ -91,7 +93,7 @@ where
}

/// Create a new HTTP Context
pub fn with_factory<F>(f: F) -> impl Stream<Item = Bytes, Error = Error>
pub fn with_factory<F>(f: F) -> impl Stream<Item = Result<Bytes, Error>>
where
F: FnOnce(&mut Self) -> A + 'static,
{
Expand Down Expand Up @@ -160,24 +162,23 @@ impl<A> Stream for HttpContextFut<A>
where
A: Actor<Context = HttpContext<A>>,
{
type Item = Bytes;
type Error = Error;
type Item = Result<Bytes, Error>;

fn poll(&mut self) -> Poll<Option<Bytes>, Error> {
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Self::Item>> {
if self.fut.alive() {
match self.fut.poll() {
Ok(Async::NotReady) | Ok(Async::Ready(())) => (),
Err(_) => return Err(ErrorInternalServerError("error")),
}
let _ = Pin::new(&mut self.fut).poll(cx);
}

// frames
if let Some(data) = self.fut.ctx().stream.pop_front() {
Ok(Async::Ready(data))
Poll::Ready(data.map(|b| Ok(b)))
} else if self.fut.alive() {
Ok(Async::NotReady)
Poll::Pending
} else {
Ok(Async::Ready(None))
Poll::Ready(None)
}
}
}
Expand All @@ -199,9 +200,9 @@ mod tests {

use actix::Actor;
use actix_web::http::StatusCode;
use actix_web::test::{block_on, call_service, init_service, TestRequest};
use actix_web::test::{call_service, init_service, read_body, TestRequest};
use actix_web::{web, App, HttpResponse};
use bytes::{Bytes, BytesMut};
use bytes::Bytes;

use super::*;

Expand All @@ -223,31 +224,25 @@ mod tests {
if self.count > 3 {
ctx.write_eof()
} else {
ctx.write(Bytes::from(format!("LINE-{}", self.count).as_bytes()));
ctx.write(Bytes::from(format!("LINE-{}", self.count)));
ctx.run_later(Duration::from_millis(100), |slf, ctx| slf.write(ctx));
}
}
}

#[test]
fn test_default_resource() {
#[actix_rt::test]
async fn test_default_resource() {
let mut srv =
init_service(App::new().service(web::resource("/test").to(|| {
HttpResponse::Ok().streaming(HttpContext::create(MyActor { count: 0 }))
})));
})))
.await;

let req = TestRequest::with_uri("/test").to_request();
let mut resp = call_service(&mut srv, req);
let resp = call_service(&mut srv, req).await;
assert_eq!(resp.status(), StatusCode::OK);

let body = block_on(resp.take_body().fold(
BytesMut::new(),
move |mut body, chunk| {
body.extend_from_slice(&chunk);
Ok::<_, Error>(body)
},
))
.unwrap();
assert_eq!(body.freeze(), Bytes::from_static(b"LINE-1LINE-2LINE-3"));
let body = read_body(resp).await;
assert_eq!(body, Bytes::from_static(b"LINE-1LINE-2LINE-3"));
}
}
Loading

0 comments on commit a153374

Please sign in to comment.