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

fix: unify x_lambda::ohkami_service_* tests into single one #351

Merged
merged 3 commits into from
Feb 6, 2025
Merged
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
122 changes: 44 additions & 78 deletions ohkami/src/x_lambda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,6 @@ mod ws {

#[cfg(feature="nightly"/* `noop_waker` is stabilized in 1.85.0 and then remove this cfg */)]
#[cfg(test)]
/// FIXME
/// this is flaky; sometimes cause a timing conflict around `static ROUTER` (in ohkami/mod.rs, used for Service impl)
mod tests {
use super::*;
use crate::{Ohkami, Route, Method};
Expand Down Expand Up @@ -557,19 +555,20 @@ mod tests {
}

#[test]
fn ohkami_service_call_once() {
fn ohkami_service_call() {
tokio::runtime::Runtime::new().unwrap().block_on(async {
{
let mut o = Ohkami::new((
"/hello".GET(|| async {"Hello, Service!"}),
));

let _ = <Ohkami as lambda_runtime::Service<
lambda_runtime::LambdaEvent<
crate::x_lambda::LambdaHTTPRequest
>
>>::poll_ready(&mut o, &mut Context::from_waker(Waker::noop()));

let mut o = Ohkami::new((
"/hello".GET(|| async {"Hello, Service!"}),
));

/* poll_ready first */
let _ = <Ohkami as lambda_runtime::Service<
lambda_runtime::LambdaEvent<
crate::x_lambda::LambdaHTTPRequest
>
>>::poll_ready(&mut o, &mut Context::from_waker(Waker::noop()));

{/* 404 */
let res = <Ohkami as lambda_runtime::Service<
lambda_runtime::LambdaEvent<
crate::x_lambda::LambdaHTTPRequest
Expand All @@ -579,33 +578,51 @@ mod tests {
"/",
None
)).await.unwrap();

let lambda_runtime::FunctionResponse::BufferedResponse(res) = res else {
panic!("Unexpected `StreamingResponse`")
};

assert_eq!(res, LambdaResponse {
statusCode: 404,
headers: crate::response::ResponseHeaders::from_iter([
("Date", ohkami_lib::imf_fixdate(crate::util::unix_timestamp())),
("Content-Length", "0".into()),
// ("Content-Type", "text/plain; charset=UTF-8".into()),
]),
cookies: None,
body: None,
isBase64Encoded: None,
body: None,//Some("Hello, Service!".into()),
isBase64Encoded: None,//Some(false),
});
}
{
let mut o = Ohkami::new((
"/hello".GET(|| async {"Hello, Service!"}),
));

let _ = <Ohkami as lambda_runtime::Service<
{/* OK */
let res = <Ohkami as lambda_runtime::Service<
lambda_runtime::LambdaEvent<
crate::x_lambda::LambdaHTTPRequest
>
>>::poll_ready(&mut o, &mut Context::from_waker(Waker::noop()));

>>::call(&mut o, new_req(
Method::GET,
"/hello",
None
)).await.unwrap();

let lambda_runtime::FunctionResponse::BufferedResponse(res) = res else {
panic!("Unexpected `StreamingResponse`")
};

assert_eq!(res, LambdaResponse {
statusCode: 200,
headers: crate::response::ResponseHeaders::from_iter([
("Date", ohkami_lib::imf_fixdate(crate::util::unix_timestamp())),
("Content-Length", "15".into()),
("Content-Type", "text/plain; charset=UTF-8".into()),
]),
cookies: None,
body: Some("Hello, Service!".into()),
isBase64Encoded: Some(false),
});
}
{/* OK twice */
let res = <Ohkami as lambda_runtime::Service<
lambda_runtime::LambdaEvent<
crate::x_lambda::LambdaHTTPRequest
Expand All @@ -619,7 +636,7 @@ mod tests {
let lambda_runtime::FunctionResponse::BufferedResponse(res) = res else {
panic!("Unexpected `StreamingResponse`")
};

assert_eq!(res, LambdaResponse {
statusCode: 200,
headers: crate::response::ResponseHeaders::from_iter([
Expand All @@ -634,55 +651,4 @@ mod tests {
}
});
}

#[test]
fn ohkami_service_call_multiple_times() {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let mut o = Ohkami::new((
"/hello".GET(|| async {"Hello, Service!"}),
));

let _ = <Ohkami as lambda_runtime::Service<
lambda_runtime::LambdaEvent<
crate::x_lambda::LambdaHTTPRequest
>
>>::poll_ready(&mut o, &mut Context::from_waker(Waker::noop()));

let res = <Ohkami as lambda_runtime::Service<
lambda_runtime::LambdaEvent<
crate::x_lambda::LambdaHTTPRequest
>
>>::call(&mut o, new_req(
Method::GET,
"/hello",
None
)).await.unwrap();

let lambda_runtime::FunctionResponse::BufferedResponse(res) = res else {
panic!("Unexpected `StreamingResponse`")
};

assert_eq!(res, LambdaResponse {
statusCode: 200,
headers: crate::response::ResponseHeaders::from_iter([
("Date", ohkami_lib::imf_fixdate(crate::util::unix_timestamp())),
("Content-Length", "15".into()),
("Content-Type", "text/plain; charset=UTF-8".into()),
]),
cookies: None,
body: Some("Hello, Service!".into()),
isBase64Encoded: Some(false),
});

let _ = <Ohkami as lambda_runtime::Service<
lambda_runtime::LambdaEvent<
crate::x_lambda::LambdaHTTPRequest
>
>>::call(&mut o, new_req(
Method::GET,
"/hello",
None
)); /* panics as second call */
});
}
}