Tests cleanups

This commit is contained in:
Nikolay Kim 2022-02-02 08:53:58 +06:00
parent f3c7c6d365
commit 6b35f10c2f
12 changed files with 100 additions and 58 deletions

View file

@ -3654,12 +3654,6 @@ impl PartialEq<BytesMut> for [u8] {
}
}
impl PartialOrd<BytesMut> for [u8] {
fn partial_cmp(&self, other: &BytesMut) -> Option<cmp::Ordering> {
other.partial_cmp(self)
}
}
impl PartialEq<str> for BytesMut {
fn eq(&self, other: &str) -> bool {
&**self == other.as_bytes()
@ -3897,12 +3891,6 @@ impl PartialEq<[u8]> for BytesVec {
}
}
impl PartialOrd<[u8]> for BytesVec {
fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> {
(**self).partial_cmp(other)
}
}
impl PartialEq<BytesVec> for [u8] {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self

View file

@ -457,6 +457,10 @@ fn fns_defined_for_bytes() {
assert_eq!(bytes, &"hello world"[..]);
assert_eq!(bytes, BytesVec::copy_from_slice(b"hello world"));
assert_eq!(bytes, BytesMut::copy_from_slice(b"hello world"));
assert_eq!("hello world", bytes);
assert_eq!("hello world".as_bytes().to_vec(), bytes);
assert_eq!("hello world".to_string(), bytes);
assert_eq!(&"hello world"[..], bytes);
// Iterator
let v: Vec<u8> = (&bytes).iter().cloned().collect();
@ -468,7 +472,7 @@ fn fns_defined_for_bytes() {
let v: Vec<u8> = bytes.clone().into_iter().collect();
assert_eq!(&v[..], bytes);
let v: Vec<u8> = bytes.as_ref().into_iter().cloned().collect();
let v: Vec<u8> = (&bytes).into_iter().cloned().collect();
assert_eq!(&v[..], bytes);
let b2: Bytes = v.iter().collect();
@ -496,6 +500,10 @@ fn fns_defined_for_bytes_mut() {
assert_eq!(bytes, &"hello world"[..]);
assert_eq!(bytes, Bytes::copy_from_slice(b"hello world"));
assert_eq!(bytes, BytesVec::copy_from_slice(b"hello world"));
assert_eq!("hello world", bytes);
assert_eq!("hello world".as_bytes().to_vec(), bytes);
assert_eq!("hello world".to_string(), bytes);
assert_eq!(&"hello world"[..], bytes);
// Iterator
let v: Vec<u8> = (&bytes).iter().cloned().collect();
@ -508,6 +516,10 @@ fn fns_defined_for_bytes_mut() {
assert_eq!(&v[..], bytes);
let v: Vec<u8> = bytes.clone().into_iter().collect();
assert_eq!(&v[..], bytes);
let v: Vec<u8> = (&bytes).into_iter().cloned().collect();
assert_eq!(&v[..], bytes);
let mut bytes = BytesMut::copy_from_slice(b"hello world");
assert_eq!(&v[..], bytes);
@ -550,6 +562,10 @@ fn fns_defined_for_bytes_vec() {
assert_eq!(bytes, &"hello world"[..]);
assert_eq!(bytes, Bytes::copy_from_slice(b"hello world"));
assert_eq!(bytes, BytesMut::copy_from_slice(b"hello world"));
assert_eq!("hello world", bytes);
assert_eq!("hello world".as_bytes().to_vec(), bytes);
assert_eq!("hello world".to_string(), bytes);
assert_eq!(&"hello world"[..], bytes);
// Iterator
let v: Vec<u8> = (&bytes).iter().cloned().collect();
@ -561,7 +577,7 @@ fn fns_defined_for_bytes_vec() {
let v: Vec<u8> = bytes.iter().cloned().collect();
assert_eq!(&v[..], bytes);
let v: Vec<u8> = bytes.as_ref().into_iter().cloned().collect();
let v: Vec<u8> = (&bytes).into_iter().cloned().collect();
assert_eq!(&v[..], bytes);
let v: Vec<u8> = bytes.into_iter().collect();
@ -590,6 +606,10 @@ fn fns_defined_for_bytes_vec() {
let b = BytesMut::default();
assert!(b.is_empty());
let mut bytes = BytesVec::copy_from_slice(b"hello world");
unsafe { bytes.set_len(1) };
assert_eq!(bytes, "h");
}
#[test]

View file

@ -811,7 +811,7 @@ impl<F> FilterItem<F> {
}
}
/// Get sealed, panic if it is not sealed
/// Get sealed, panic if it is already sealed
fn get_sealed(&mut self) -> Sealed {
if self.data[KIND_IDX] & KIND_SEALED != 0 {
self.data[KIND_IDX] &= KIND_UNMASK;

View file

@ -356,6 +356,7 @@ mod tests {
Arbiter::set_item("test");
assert!(Arbiter::get_item::<&'static str, _, _>(|s| *s == "test"));
assert!(Arbiter::get_mut_item::<&'static str, _, _>(|s| *s == "test"));
assert!(Arbiter::contains_item::<&'static str>());
assert!(format!("{:?}", Arbiter::current()).contains("Arbiter"));
}
}

View file

@ -19,5 +19,5 @@ path = "src/lib.rs"
pin-project-lite = "0.2.6"
[dev-dependencies]
ntex = "0.5.0"
ntex = { version = "0.5", features = ["tokio"] }
ntex-util = "0.1.5"

View file

@ -493,6 +493,10 @@ mod tests {
.on_shutdown(|| {
*shutdown.borrow_mut() = true;
})
.clone()
.new_service(())
.await
.unwrap()
.clone();
let res = srv.call(()).await;

View file

@ -320,5 +320,11 @@ mod tests {
let (tx, rx) = channel::<()>();
rx.close();
assert!(tx.is_closed());
let (tx, rx) = channel::<()>();
drop(tx);
assert!(rx.is_closed());
let _tx = rx.sender();
assert!(!rx.is_closed());
}
}

View file

@ -3,13 +3,15 @@ use slab::Slab;
use std::{future::Future, pin::Pin, task::Context, task::Poll};
use super::{cell::Cell, Canceled};
use crate::{future::poll_fn, task::LocalWaker};
use crate::task::LocalWaker;
/// Creates a new futures-aware, pool of one-shot's.
pub fn new<T>() -> Pool<T> {
Pool(Cell::new(Slab::new()))
}
#[doc(hidden)]
/// Futures-aware, pool of one-shot's.
pub type OneshotsPool<T> = Pool<T>;
/// Futures-aware, pool of one-shot's.
@ -152,11 +154,6 @@ impl<T> Drop for Sender<T> {
}
impl<T> Receiver<T> {
/// Wait until the oneshot is ready and return value
pub async fn recv(&self) -> Result<T, Canceled> {
poll_fn(|cx| self.poll_recv(cx)).await
}
/// Polls the oneshot to determine if value is ready
pub fn poll_recv(&self, cx: &mut Context<'_>) -> Poll<Result<T, Canceled>> {
let inner = get_inner(&self.inner, self.token);
@ -207,6 +204,8 @@ mod tests {
let (tx, rx) = p.channel();
tx.send("test").unwrap();
assert_eq!(rx.await.unwrap(), "test");
assert!(format!("{}", Canceled).contains("canceled"));
assert!(format!("{:?}", Canceled).contains("Canceled"));
let p2 = p.clone();
let (tx, rx) = p2.channel();
@ -241,5 +240,10 @@ mod tests {
lazy(|cx| Pin::new(&mut tx).poll_canceled(cx)).await,
Poll::Ready(())
);
let p = Pool::default();
let (tx, rx) = p.channel();
tx.send("test").unwrap();
assert_eq!(rx.await.unwrap(), "test");
}
}

View file

@ -121,12 +121,6 @@ where
self
}
#[deprecated]
#[doc(hidden)]
pub fn data<U: 'static>(self, data: U) -> Self {
self.state(data)
}
/// Set application state factory. This function is
/// similar to `.state()` but it accepts state factory. State object get
/// constructed asynchronously during application initialization.
@ -658,10 +652,12 @@ mod tests {
#[crate::rt_test]
async fn test_default_resource() {
let srv = init_service(
App::new().service(web::resource("/test").to(|| async { HttpResponse::Ok() })),
)
.await;
let srv = App::new()
.service(web::resource("/test").to(|| async { HttpResponse::Ok() }))
.finish()
.new_service(())
.await
.unwrap();
let req = TestRequest::with_uri("/test").to_request();
let resp = srv.call(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
@ -670,21 +666,22 @@ mod tests {
let resp = srv.call(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let srv = init_service(
App::new()
.service(web::resource("/test").to(|| async { HttpResponse::Ok() }))
.service(
web::resource("/test2")
.default_service(|r: WebRequest<DefaultError>| async move {
Ok(r.into_response(HttpResponse::Created()))
})
.route(web::get().to(|| async { HttpResponse::Ok() })),
)
.default_service(|r: WebRequest<DefaultError>| async move {
Ok(r.into_response(HttpResponse::MethodNotAllowed()))
}),
)
.await;
let srv = App::new()
.service(web::resource("/test").to(|| async { HttpResponse::Ok() }))
.service(
web::resource("/test2")
.default_service(|r: WebRequest<DefaultError>| async move {
Ok(r.into_response(HttpResponse::Created()))
})
.route(web::get().to(|| async { HttpResponse::Ok() })),
)
.default_service(|r: WebRequest<DefaultError>| async move {
Ok(r.into_response(HttpResponse::MethodNotAllowed()))
})
.with_config(Default::default())
.new_service(())
.await
.unwrap();
let req = TestRequest::with_uri("/blah").to_request();
let resp = srv.call(req).await.unwrap();

View file

@ -669,13 +669,17 @@ mod tests {
#[crate::rt_test]
async fn test_scope() {
let srv = init_service(
App::new().service(
web::scope("/app")
.service(web::resource("/path1").to(|| async { HttpResponse::Ok() })),
),
)
.await;
let srv =
init_service(
App::new()
.service(web::scope("/app").service(
web::resource("/path1").to(|| async { HttpResponse::Ok() }),
))
.service(web::scope("/app2").case_insensitive_routing().service(
web::resource("/path1").to(|| async { HttpResponse::Ok() }),
)),
)
.await;
let req = TestRequest::with_uri("/app/path1").to_request();
let resp = srv.call(req).await.unwrap();
@ -684,6 +688,14 @@ mod tests {
let req = TestRequest::with_uri("/app/path10").to_request();
let resp = srv.call(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
let req = TestRequest::with_uri("/app2/path1").to_request();
let resp = srv.call(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let req = TestRequest::with_uri("/app2/Path1").to_request();
let resp = srv.call(req).await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
#[crate::rt_test]

View file

@ -441,14 +441,22 @@ mod tests {
.set_payload(Bytes::from_static(b"hello=world"))
.to_http_parts();
let mut s = from_request::<Payload>(&req, &mut pl)
.await
.unwrap()
.into_inner();
let mut s = from_request::<Payload>(&req, &mut pl).await.unwrap();
let b = stream_recv(&mut s).await.unwrap().unwrap();
assert_eq!(b, Bytes::from_static(b"hello=world"));
}
#[crate::rt_test]
async fn test_payload_recv() {
let (req, mut pl) = TestRequest::with_header(header::CONTENT_LENGTH, "11")
.set_payload(Bytes::from_static(b"hello=world"))
.to_http_parts();
let mut s = from_request::<Payload>(&req, &mut pl).await.unwrap();
let b = s.recv().await.unwrap().unwrap();
assert_eq!(b, Bytes::from_static(b"hello=world"));
}
#[crate::rt_test]
async fn test_bytes() {
let (req, mut pl) = TestRequest::with_header(header::CONTENT_LENGTH, "11")

View file

@ -169,6 +169,8 @@ async fn test_rustls_string() {
}))
.and_then(rustls::Acceptor::new(tls_acceptor()))
.and_then(fn_service(|io: Io<_>| async move {
assert!(io.query::<PeerCert>().as_ref().is_none());
assert!(io.query::<PeerCertChain>().as_ref().is_none());
io.send(Bytes::from_static(b"test"), &BytesCodec)
.await
.unwrap();