-
Notifications
You must be signed in to change notification settings - Fork 176
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
Allow awaiting on server handles #550
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,16 +27,17 @@ | |
#![cfg(test)] | ||
|
||
use std::net::SocketAddr; | ||
use std::time::Duration; | ||
|
||
use crate::types::error::{CallError, Error}; | ||
use crate::{server::StopHandle, HttpServerBuilder, RpcModule}; | ||
use crate::{server::ServerHandle, HttpServerBuilder, RpcModule}; | ||
|
||
use jsonrpsee_test_utils::helpers::*; | ||
use jsonrpsee_test_utils::mocks::{Id, StatusCode, TestContext}; | ||
use jsonrpsee_test_utils::TimeoutFutureExt; | ||
use serde_json::Value as JsonValue; | ||
|
||
async fn server() -> (SocketAddr, StopHandle) { | ||
async fn server() -> (SocketAddr, ServerHandle) { | ||
let server = HttpServerBuilder::default().build("127.0.0.1:0".parse().unwrap()).unwrap(); | ||
let ctx = TestContext; | ||
let mut module = RpcModule::new(ctx); | ||
|
@@ -78,8 +79,8 @@ async fn server() -> (SocketAddr, StopHandle) { | |
}) | ||
.unwrap(); | ||
|
||
let stop_handle = server.start(module).unwrap(); | ||
(addr, stop_handle) | ||
let server_handle = server.start(module).unwrap(); | ||
(addr, server_handle) | ||
} | ||
|
||
#[tokio::test] | ||
|
@@ -380,6 +381,27 @@ async fn can_register_modules() { | |
#[tokio::test] | ||
async fn stop_works() { | ||
let _ = env_logger::try_init(); | ||
let (_addr, stop_handle) = server().with_default_timeout().await.unwrap(); | ||
assert!(matches!(stop_handle.stop().unwrap().await, Ok(_))); | ||
let (_addr, server_handle) = server().with_default_timeout().await.unwrap(); | ||
assert!(matches!(server_handle.stop().unwrap().await, Ok(_))); | ||
} | ||
|
||
#[tokio::test] | ||
async fn run_forever() { | ||
const TIMEOUT: Duration = Duration::from_millis(200); | ||
|
||
let _ = env_logger::try_init(); | ||
let (_addr, server_handle) = server().with_default_timeout().await.unwrap(); | ||
|
||
assert!(matches!(server_handle.with_timeout(TIMEOUT).await, Err(_timeout_err))); | ||
|
||
let (_addr, server_handle) = server().await; | ||
server_handle.handle.as_ref().unwrap().abort(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, you did this instead of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stop is a little bit different, here we want to emulate the actual server behavior -- if a task is cancelled or panics, the future handle resolves too. |
||
|
||
// Cancelled task is still considered to be finished without errors. | ||
// A subject to change. | ||
server_handle.with_timeout(TIMEOUT).await.unwrap(); | ||
|
||
let (_addr, mut server_handle) = server().with_default_timeout().await.unwrap(); | ||
server_handle.handle.take(); | ||
server_handle.with_timeout(TIMEOUT).await.unwrap(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
pub(crate)
only needed for tests or why?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used for tests