Update utils tests

This commit is contained in:
Nikolay Kim 2022-01-31 16:58:56 +06:00
parent 767dd91b7e
commit 16fcd6dc58
12 changed files with 194 additions and 37 deletions

View file

@ -1,6 +1,6 @@
# Changes
## [0.1.11] (2022-01-31)
## [0.1.12] (2022-01-31)
* Fix conversion from BytesVec to BytesMut and back (BytesVec::with_bytes_mut())

View file

@ -72,32 +72,6 @@ impl<A, B> Either<A, B> {
}
}
impl<A, B, T> Either<(T, A), (T, B)> {
#[inline]
/// Factor out a homogeneous type from an either of pairs.
///
/// Here, the homogeneous type is the first element of the pairs.
pub fn factor_first(self) -> (T, Either<A, B>) {
match self {
Either::Left((x, a)) => (x, Either::Left(a)),
Either::Right((x, b)) => (x, Either::Right(b)),
}
}
}
impl<A, B, T> Either<(A, T), (B, T)> {
#[inline]
/// Factor out a homogeneous type from an either of pairs.
///
/// Here, the homogeneous type is the second element of the pairs.
pub fn factor_second(self) -> (Either<A, B>, T) {
match self {
Either::Left((a, x)) => (Either::Left(a), x),
Either::Right((b, x)) => (Either::Right(b), x),
}
}
}
impl<T> Either<T, T> {
#[inline]
/// Extract the value of an either over two equivalent types.
@ -145,3 +119,37 @@ where
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn either() {
let mut e = Either::<(), ()>::Left(());
assert_eq!(e.is_left(), true);
assert_eq!(e.is_right(), false);
assert!(e.left().is_some());
assert!(e.right().is_none());
e.as_ref();
e.as_mut();
let e = Either::<(), ()>::Right(());
assert_eq!(e.is_left(), false);
assert_eq!(e.is_right(), true);
assert!(e.left().is_none());
assert!(e.right().is_some());
assert_eq!(Either::<(), ()>::Left(()).into_inner(), ());
assert_eq!(Either::<(), ()>::Right(()).into_inner(), ());
assert_eq!(
format!("{}", Either::<_, &'static str>::Left("test")),
"test"
);
assert_eq!(
format!("{}", Either::<&'static str, _>::Right("test")),
"test"
);
}
}

View file

@ -99,3 +99,19 @@ where
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{future::Ready, time};
#[ntex_macros::rt_test2]
async fn join_tests() {
let res = join(Ready::<_, ()>::Ok("test"), time::sleep(time::Millis(50))).await;
assert_eq!(res, (Ok("test"), ()));
let res =
join_all([time::sleep(time::Millis(50)), time::sleep(time::Millis(60))]).await;
assert_eq!(res, vec![(), ()]);
}
}

View file

@ -41,3 +41,20 @@ where
Poll::Pending
}
}
#[cfg(test)]
mod tests {
use futures_util::future::pending;
use super::*;
use crate::{future::Ready, time};
#[ntex_macros::rt_test2]
async fn select_tests() {
let res = select(Ready::<_, ()>::Ok("test"), pending::<()>()).await;
assert_eq!(res, Either::Left(Ok("test")));
let res = select(pending::<()>(), time::sleep(time::Millis(50))).await;
assert_eq!(res, Either::Right(()));
}
}

View file

@ -192,3 +192,57 @@ impl From<Seconds> for std::time::Duration {
std::time::Duration::from_secs(d.0 as u64)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Duration;
#[test]
fn time_types() {
let m = Millis::default();
assert_eq!(m.0, 0);
let m = Millis(10) + Millis(20);
assert_eq!(m.0, 30);
let m = Millis(10) + Millis(u32::MAX);
assert_eq!(m.0, u32::MAX);
let m = Millis(10) + Seconds(1);
assert_eq!(m.0, 1010);
let m = Millis(u32::MAX) + Seconds(1);
assert_eq!(m.0, u32::MAX);
let m = Millis(10) + Duration::from_millis(100);
assert_eq!(m.0, 110);
let m = Duration::from_millis(100) + Millis(10);
assert_eq!(m, Duration::from_millis(110));
let m = Millis::from(Seconds(1));
assert_eq!(m.0, 1000);
let m = Millis::from(Duration::from_secs(1));
assert_eq!(m.0, 1000);
let s = Seconds::new(10);
assert_eq!(s.0, 10);
let s = Seconds::checked_new(u16::MAX as usize + 10);
assert_eq!(s.0, u16::MAX);
assert!(Seconds::ZERO.is_zero());
assert!(!Seconds::ZERO.non_zero());
let s = Seconds::new(10);
assert_eq!(s.seconds(), 10);
let s = Seconds::default();
assert_eq!(s.0, 0);
let s = Seconds::new(10) + Seconds::new(10);
assert_eq!(s.seconds(), 20);
}
}

View file

@ -38,3 +38,18 @@ impl Clone for ConnectError {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn connect_error_clone() {
let _ =
ConnectError::Resolver(io::Error::new(io::ErrorKind::Other, "test")).clone();
let _ = ConnectError::NoRecords.clone();
let _ = ConnectError::InvalidInput.clone();
let _ = ConnectError::Unresolved.clone();
let _ = ConnectError::Io(io::Error::new(io::ErrorKind::Other, "test")).clone();
}
}

View file

@ -128,7 +128,7 @@ mod tests {
});
let ssl = SslConnector::builder(SslMethod::tls()).unwrap();
let factory = Connector::new(ssl.build()).clone();
let factory = Connector::new(ssl.build()).clone().memory_pool(PoolId::P5);
let srv = factory.new_service(()).await.unwrap();
let result = srv

View file

@ -92,7 +92,7 @@ impl<T: Address> Resolver<T> {
impl<T> Default for Resolver<T> {
fn default() -> Resolver<T> {
Resolver(marker::PhantomData)
Resolver::new()
}
}
@ -138,7 +138,7 @@ mod tests {
#[crate::rt_test]
async fn resolver() {
let resolver = Resolver::new();
let resolver = Resolver::default().clone();
assert!(format!("{:?}", resolver).contains("Resolver"));
let srv = resolver.new_service(()).await.unwrap();
assert!(lazy(|cx| srv.poll_ready(cx)).await.is_ready());

View file

@ -121,10 +121,12 @@ impl<T: Address> Service<Connect<T>> for Connector<T> {
#[cfg(test)]
mod tests {
use std::sync::Arc;
use tls_rustls::{OwnedTrustAnchor, RootCertStore};
use super::*;
use crate::service::{Service, ServiceFactory};
use crate::util::lazy;
#[crate::rt_test]
async fn test_rustls_connect() {
@ -146,9 +148,12 @@ mod tests {
.with_safe_defaults()
.with_root_certificates(cert_store)
.with_no_client_auth();
let factory = Connector::new(config).clone();
let _ = Connector::<&'static str>::new(config.clone()).clone();
let factory = Connector::from(Arc::new(config)).memory_pool(PoolId::P5);
let srv = factory.new_service(()).await.unwrap();
// always ready
assert!(lazy(|cx| srv.poll_ready(cx)).await.is_ready());
let result = srv
.call(Connect::new("www.rust-lang.org").set_addr(Some(server.addr())))
.await;

View file

@ -240,7 +240,7 @@ mod tests {
crate::service::fn_service(|_| async { Ok::<_, ()>(()) })
});
let srv = Connector::default();
let srv = Connector::default().memory_pool(PoolId::P5);
let result = srv.connect("").await;
assert!(result.is_err());
let result = srv.connect("localhost:99999").await;

View file

@ -35,3 +35,27 @@ fn port(scheme: Option<&str>) -> Option<u16> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn port_tests() {
for (s, p) in [
("http", 80),
("https", 443),
("ws", 80),
("wss", 443),
("amqp", 5672),
("amqps", 5671),
("sb", 5671),
("mqtt", 1883),
("mqtts", 8883),
] {
assert_eq!(port(Some(s)), Some(p))
}
assert_eq!(port(Some("unknowns")), None);
assert_eq!(port(None), None);
}
}

View file

@ -3,9 +3,8 @@ use std::{io, sync::Arc};
use ntex::codec::BytesCodec;
use ntex::connect::Connect;
use ntex::io::{types::PeerAddr, Io};
use ntex::server::test_server;
use ntex::service::{fn_service, pipeline_factory, Service, ServiceFactory};
use ntex::{time, util::Bytes};
use ntex::{server::test_server, util::Bytes};
#[cfg(feature = "openssl")]
fn ssl_acceptor() -> tls_openssl::ssl::SslAcceptor {
@ -72,7 +71,7 @@ mod danger {
#[ntex::test]
async fn test_openssl_string() {
use ntex::server::openssl;
use ntex_tls::openssl::PeerCert;
use ntex_tls::{openssl::PeerCert, types::HttpProtocol};
use tls_openssl::{
ssl::{SslConnector, SslMethod, SslVerifyMode},
x509::X509,
@ -89,7 +88,7 @@ async fn test_openssl_string() {
io.send(Bytes::from_static(b"test"), &BytesCodec)
.await
.unwrap();
time::sleep(time::Millis(100)).await;
assert_eq!(io.recv(&BytesCodec).await.unwrap().unwrap(), "test");
Ok::<_, Box<dyn std::error::Error>>(())
}))
});
@ -101,6 +100,10 @@ async fn test_openssl_string() {
let addr = format!("127.0.0.1:{}", srv.addr().port());
let io = conn.call(addr.into()).await.unwrap();
assert_eq!(io.query::<PeerAddr>().get().unwrap(), srv.addr().into());
assert_eq!(
io.query::<HttpProtocol>().get().unwrap(),
HttpProtocol::Http1
);
let cert = X509::from_pem(include_bytes!("cert.pem")).unwrap();
assert_eq!(
io.query::<PeerCert>().as_ref().unwrap().0.to_der().unwrap(),
@ -139,6 +142,10 @@ async fn test_openssl_read_before_error() {
let io = conn.call(addr.into()).await.unwrap();
let item = io.recv(&BytesCodec).await.unwrap().unwrap();
assert_eq!(item, Bytes::from_static(b"test"));
io.send(Bytes::from_static(b"test"), &BytesCodec)
.await
.unwrap();
assert!(io.recv(&BytesCodec).await.unwrap().is_none());
}
@ -146,7 +153,7 @@ async fn test_openssl_read_before_error() {
#[ntex::test]
async fn test_rustls_string() {
use ntex::server::rustls;
use ntex_tls::rustls::PeerCert;
use ntex_tls::{rustls::PeerCert, rustls::PeerCertChain, types::HttpProtocol};
use rustls_pemfile::certs;
use std::fs::File;
use std::io::BufReader;
@ -163,6 +170,7 @@ async fn test_rustls_string() {
io.send(Bytes::from_static(b"test"), &BytesCodec)
.await
.unwrap();
assert_eq!(io.recv(&BytesCodec).await.unwrap().unwrap(), "test");
Ok::<_, std::io::Error>(())
}))
});
@ -176,6 +184,10 @@ async fn test_rustls_string() {
let addr = format!("localhost:{}", srv.addr().port());
let io = conn.call(addr.into()).await.unwrap();
assert_eq!(io.query::<PeerAddr>().get().unwrap(), srv.addr().into());
assert_eq!(
io.query::<HttpProtocol>().get().unwrap(),
HttpProtocol::Http1
);
let cert_file = &mut BufReader::new(File::open("tests/cert.pem").unwrap());
let cert_chain: Vec<Certificate> = certs(cert_file)
.unwrap()
@ -186,8 +198,14 @@ async fn test_rustls_string() {
io.query::<PeerCert>().as_ref().unwrap().0,
*cert_chain.first().unwrap()
);
assert_eq!(io.query::<PeerCertChain>().as_ref().unwrap().0, cert_chain);
let item = io.recv(&BytesCodec).await.unwrap().unwrap();
assert_eq!(item, Bytes::from_static(b"test"));
io.send(Bytes::from_static(b"test"), &BytesCodec)
.await
.unwrap();
assert!(io.recv(&BytesCodec).await.unwrap().is_none());
}
#[ntex::test]