2019-07-03 09:02:03 +02:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use actix_codec::BytesCodec;
|
|
|
|
use actix_server_config::Io;
|
2019-11-14 13:38:24 +01:00
|
|
|
use actix_service::{apply_fn_factory, service_fn, Service};
|
2019-11-25 16:49:11 +01:00
|
|
|
use actix_testing::TestServer;
|
2019-11-14 13:38:24 +01:00
|
|
|
use futures::future::ok;
|
|
|
|
use tokio_net::tcp::TcpStream;
|
|
|
|
use tokio_timer::delay_for;
|
2019-07-03 09:02:03 +02:00
|
|
|
|
|
|
|
use actix_ioframe::{Builder, Connect};
|
|
|
|
|
|
|
|
struct State;
|
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_disconnect() -> std::io::Result<()> {
|
2019-07-03 09:02:03 +02:00
|
|
|
let disconnect = Arc::new(AtomicBool::new(false));
|
|
|
|
let disconnect1 = disconnect.clone();
|
|
|
|
|
2019-09-25 05:51:28 +02:00
|
|
|
let srv = TestServer::with(move || {
|
2019-07-03 09:02:03 +02:00
|
|
|
let disconnect1 = disconnect1.clone();
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
apply_fn_factory(
|
2019-07-03 09:02:03 +02:00
|
|
|
Builder::new()
|
2019-11-14 13:38:24 +01:00
|
|
|
.factory(service_fn(|conn: Connect<_>| {
|
|
|
|
ok(conn.codec(BytesCodec).state(State))
|
|
|
|
}))
|
2019-07-03 09:02:03 +02:00
|
|
|
.disconnect(move |_, _| {
|
|
|
|
disconnect1.store(true, Ordering::Relaxed);
|
|
|
|
})
|
2019-11-14 13:38:24 +01:00
|
|
|
.finish(service_fn(|_t| ok(None))),
|
2019-07-03 09:02:03 +02:00
|
|
|
|io: Io<TcpStream>, srv| srv.call(io.into_parts().0),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut client = Builder::new()
|
|
|
|
.service(|conn: Connect<_>| {
|
|
|
|
let conn = conn.codec(BytesCodec).state(State);
|
|
|
|
conn.sink().close();
|
2019-11-14 13:38:24 +01:00
|
|
|
ok(conn)
|
2019-07-03 09:02:03 +02:00
|
|
|
})
|
2019-11-14 13:38:24 +01:00
|
|
|
.finish(service_fn(|_t| ok(None)));
|
2019-07-03 09:02:03 +02:00
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
let conn = actix_connect::default_connector()
|
|
|
|
.call(actix_connect::Connect::with(String::new(), srv.addr()))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2019-07-03 09:02:03 +02:00
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
client.call(conn.into_parts().0).await.unwrap();
|
|
|
|
let _ = delay_for(Duration::from_millis(100)).await;
|
2019-07-03 09:02:03 +02:00
|
|
|
assert!(disconnect.load(Ordering::Relaxed));
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|