Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: layer6_testing::Testing #33

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2 changes: 1 addition & 1 deletion ohkami/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ rt_async-std = ["dep:async-std"]
test = ["tokio?/macros", "async-std?/attributes"]

##### DEBUG #####
# default = ["rt_tokio", "serde/derive"]
# default = ["rt_tokio", "test", "serde/derive"]
64 changes: 32 additions & 32 deletions ohkami/src/layer3_fang_handler/handler/into_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ const _: (/* only Context */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |_, c, _|
Handler::new(move |_, c, _|
Box::pin({
let res = self(c);
async {res.await}
})
))
)
}
}
};
Expand All @@ -37,7 +37,7 @@ const _: (/* PathParam */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |_, c, params|
Handler::new(move |_, c, params|
match <$param_type as PathParam>::parse(unsafe {params.assume_init_first().as_bytes()}) {
Ok(p1) => Box::pin({
let res = self(c, p1);
Expand All @@ -48,7 +48,7 @@ const _: (/* PathParam */) = {
async {res}
}),
}
))
)
}
}
)*};
Expand All @@ -62,7 +62,7 @@ const _: (/* PathParam */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |_, c, params|
Handler::new(move |_, c, params|
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
match <P1 as PathParam>::parse(unsafe {params.assume_init_first().as_bytes()}) {
Expand All @@ -75,7 +75,7 @@ const _: (/* PathParam */) = {
async {res}
}),
}
))
)
}
}

Expand All @@ -85,7 +85,7 @@ const _: (/* PathParam */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |_, c, params| {
Handler::new(move |_, c, params| {
let (p1_range, p2_range) = params.assume_init_extract();
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed twice before this code
Expand All @@ -105,7 +105,7 @@ const _: (/* PathParam */) = {
async {res}
}),
}
}))
})
}
}
};
Expand All @@ -117,7 +117,7 @@ const _: (/* FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, _|
Handler::new(move |req, c, _|
match Item1::parse(&req) {
Ok(item1) => Box::pin({
let res = self(c, item1);
Expand All @@ -128,7 +128,7 @@ const _: (/* FromRequest items */) = {
async {res}
})
}
))
)
}
}

Expand All @@ -138,7 +138,7 @@ const _: (/* FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, _|
Handler::new(move |req, c, _|
match Item1::parse(&req) {
Ok(item1) => match Item2::parse(&req) {
Ok(item2) => Box::pin({
Expand All @@ -155,7 +155,7 @@ const _: (/* FromRequest items */) = {
async {res}
})
}
))
)
}
}

Expand All @@ -165,7 +165,7 @@ const _: (/* FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, _|
Handler::new(move |req, c, _|
match Item1::parse(&req) {
Ok(item1) => match Item2::parse(&req) {
Ok(item2) => match Item3::parse(&req) {
Expand All @@ -188,7 +188,7 @@ const _: (/* FromRequest items */) = {
async {res}
})
}
))
)
}
}
};
Expand All @@ -202,7 +202,7 @@ const _: (/* single PathParam and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params|
Handler::new(move |req, c, params|
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
match <$param_type as PathParam>::parse(unsafe {params.assume_init_first().as_bytes()}) {
Expand All @@ -221,7 +221,7 @@ const _: (/* single PathParam and FromRequest items */) = {
async {res}
}),
}
))
)
}
}

Expand All @@ -231,7 +231,7 @@ const _: (/* single PathParam and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params|
Handler::new(move |req, c, params|
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
match <$param_type as PathParam>::parse(unsafe {params.assume_init_first().as_bytes()}) {
Expand All @@ -256,7 +256,7 @@ const _: (/* single PathParam and FromRequest items */) = {
async {res}
}),
}
))
)
}
}

Expand All @@ -266,7 +266,7 @@ const _: (/* single PathParam and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params|
Handler::new(move |req, c, params|
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
match <$param_type as PathParam>::parse(unsafe {params.assume_init_first().as_bytes()}) {
Expand Down Expand Up @@ -297,7 +297,7 @@ const _: (/* single PathParam and FromRequest items */) = {
async {res}
}),
}
))
)
}
}
)*};
Expand All @@ -313,7 +313,7 @@ const _: (/* one PathParam and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params|
Handler::new(move |req, c, params|
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
match P1::parse(unsafe {params.assume_init_first().as_bytes()}) {
Expand All @@ -332,7 +332,7 @@ const _: (/* one PathParam and FromRequest items */) = {
async {res}
}),
}
))
)
}
}

Expand All @@ -342,7 +342,7 @@ const _: (/* one PathParam and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params|
Handler::new(move |req, c, params|
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
match P1::parse(unsafe {params.assume_init_first().as_bytes()}) {
Expand All @@ -367,7 +367,7 @@ const _: (/* one PathParam and FromRequest items */) = {
async {res}
}),
}
))
)
}
}

Expand All @@ -377,7 +377,7 @@ const _: (/* one PathParam and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params|
Handler::new(move |req, c, params|
// SAFETY: Due to the architecture of `Router`,
// `params` has already `append`ed once before this code
match P1::parse(unsafe {params.assume_init_first().as_bytes()}) {
Expand Down Expand Up @@ -408,7 +408,7 @@ const _: (/* one PathParam and FromRequest items */) = {
async {res}
}),
}
))
)
}
}
};
Expand All @@ -420,7 +420,7 @@ const _: (/* two PathParams and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params| {
Handler::new(move |req, c, params| {
let (p1_range, p2_range) = params.assume_init_extract();

// SAFETY: Due to the architecture of `Router`,
Expand All @@ -447,7 +447,7 @@ const _: (/* two PathParams and FromRequest items */) = {
async {res}
}),
}
}))
})
}
}

Expand All @@ -457,7 +457,7 @@ const _: (/* two PathParams and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params| {
Handler::new(move |req, c, params| {
let (p1_range, p2_range) = params.assume_init_extract();

// SAFETY: Due to the architecture of `Router`,
Expand Down Expand Up @@ -490,7 +490,7 @@ const _: (/* two PathParams and FromRequest items */) = {
async {res}
}),
}
}))
})
}
}

Expand All @@ -500,7 +500,7 @@ const _: (/* two PathParams and FromRequest items */) = {
Fut: Future<Output = Response> + Send + Sync + 'static,
{
fn into_handler(self) -> Handler {
Handler(Box::new(move |req, c, params| {
Handler::new(move |req, c, params| {
let (p1_range, p2_range) = params.assume_init_extract();

// SAFETY: Due to the architecture of `Router`,
Expand Down Expand Up @@ -539,7 +539,7 @@ const _: (/* two PathParams and FromRequest items */) = {
async {res}
}),
}
}))
})
}
}
};
39 changes: 37 additions & 2 deletions ohkami/src/layer3_fang_handler/handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
mod handlers; pub use handlers::{Handlers, ByAnother, Route};
mod handlers; pub use handlers::{Handlers, ByAnother, Route};
mod into_handler; pub use into_handler::{IntoHandler};

use std::{pin::Pin, future::Future};
#[cfg(test)] use std::sync::Arc;

use std::{
pin::Pin,
future::Future,
};
use crate::{
Context, Request,
layer0_lib::{List, Slice},
Expand All @@ -12,6 +17,7 @@ pub(crate) const PATH_PARAMS_LIMIT: usize = 2;
pub(crate) type PathParams = List<Slice, PATH_PARAMS_LIMIT>;


#[cfg(not(test))]
pub struct Handler(
pub(crate) Box<dyn
Fn(&mut Request, Context, PathParams) -> Pin<
Expand All @@ -22,3 +28,32 @@ pub struct Handler(
> + Send + Sync + 'static
>
);

#[cfg(test)]
#[derive(Clone)]
pub struct Handler(
pub(crate) Arc<dyn
Fn(&mut Request, Context, PathParams) -> Pin<
Box<dyn
Future<Output = Response>
+ Send + 'static
>
> + Send + Sync + 'static
>
);


impl Handler {
fn new(proc: (impl
Fn(&mut Request, Context, PathParams) -> Pin<
Box<dyn
Future<Output = Response>
+ Send + 'static
>
> + Send + Sync + 'static
)
) -> Self {
#[cfg(not(test))] {Self(Box::new(proc))}
#[cfg(test)] {Self(Arc::new(proc))}
}
}
2 changes: 2 additions & 0 deletions ohkami/src/layer4_router/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const _: () = {


/*===== defs =====*/
#[cfg_attr(test, derive(Clone))]
pub struct TrieRouter {
pub(super/* for test */) GET: Node,
pub(super/* for test */) PUT: Node,
Expand All @@ -26,6 +27,7 @@ pub struct TrieRouter {
pub(super) OPTIONSfangs: Vec<Fang>,
}

#[cfg_attr(test, derive(Clone))]
pub(super/* for test */) struct Node {
/// Why Option: root node doesn't have pattern
pub(super/* for test */) pattern: Option<Pattern>,
Expand Down
34 changes: 34 additions & 0 deletions ohkami/src/layer6_testing/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::{pin::Pin, future::Future};
use crate::{Response, Request};

#[cfg(test)] use crate::{Ohkami, Context};

pub trait Testing {
fn oneshot<'test>(&self, req: &'test mut Request) -> TestResponse<'test>;
}

pub struct TestResponse<'test>(
Box<dyn Future<Output = Response> + 'test>
); impl<'test> Future for TestResponse<'test> {
type Output = Response;
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> {
unsafe {self.map_unchecked_mut(|tr| tr.0.as_mut())}
.poll(cx)
}
}

#[cfg(test)]
impl Testing for Ohkami {
fn oneshot<'test>(&self, req: &'test mut Request) -> TestResponse<'test> {
let router = {
let mut router = self.routes.clone();
for (methods, fang) in &self.fangs {
router = router.apply_fang(methods, fang.clone())
}
router.into_radix()
};

let res = async move {router.handle(Context::new(), req).await};
TestResponse(Box::new(res))
}
}
Loading
Loading