-
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
rpc module: report error on invalid subscription #561
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,12 +27,16 @@ | |
#![cfg(test)] | ||
|
||
use crate::types::error::{CallError, Error}; | ||
use crate::types::v2::{self, Response, RpcError}; | ||
use crate::types::DeserializeOwned; | ||
use crate::{future::ServerHandle, RpcModule, WsServerBuilder}; | ||
use anyhow::anyhow; | ||
use futures_util::future::join; | ||
use jsonrpsee_test_utils::helpers::*; | ||
use jsonrpsee_test_utils::mocks::{Id, TestContext, WebSocketTestClient, WebSocketTestError}; | ||
use jsonrpsee_test_utils::TimeoutFutureExt; | ||
use jsonrpsee_types::to_json_raw_value; | ||
use jsonrpsee_types::v2::error::invalid_subscription_err; | ||
use serde_json::Value as JsonValue; | ||
use std::{fmt, net::SocketAddr, time::Duration}; | ||
use tracing_subscriber::{EnvFilter, FmtSubscriber}; | ||
|
@@ -41,6 +45,11 @@ fn init_logger() { | |
let _ = FmtSubscriber::builder().with_env_filter(EnvFilter::from_default_env()).try_init(); | ||
} | ||
|
||
fn deser_call<T: DeserializeOwned>(raw: String) -> T { | ||
let out: Response<T> = serde_json::from_str(&raw).unwrap(); | ||
out.result | ||
} | ||
|
||
/// Applications can/should provide their own error. | ||
#[derive(Debug)] | ||
struct MyAppError; | ||
|
@@ -107,6 +116,15 @@ async fn server_with_handles() -> (SocketAddr, ServerHandle) { | |
Ok("Yawn!") | ||
}) | ||
.unwrap(); | ||
module | ||
.register_subscription("subscribe_hello", "unsubscribe_hello", |_, sink, _| { | ||
std::thread::spawn(move || loop { | ||
let _ = sink; | ||
std::thread::sleep(std::time::Duration::from_secs(30)); | ||
}); | ||
Ok(()) | ||
}) | ||
.unwrap(); | ||
|
||
let addr = server.local_addr().unwrap(); | ||
|
||
|
@@ -569,3 +587,38 @@ async fn run_forever() { | |
// Send the shutdown request from one handle and await the server on the second one. | ||
join(server_handle.clone().stop().unwrap(), server_handle).with_timeout(TIMEOUT).await.unwrap(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn unsubscribe_twice_should_indicate_error() { | ||
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. ideally these tests should be in the RPC module but I have to refactor the |
||
init_logger(); | ||
let addr = server().await; | ||
let mut client = WebSocketTestClient::new(addr).with_default_timeout().await.unwrap().unwrap(); | ||
|
||
let sub_call = call("subscribe_hello", Vec::<()>::new(), Id::Num(0)); | ||
let sub_id: u64 = deser_call(client.send_request_text(sub_call).await.unwrap()); | ||
|
||
let unsub_call = call("unsubscribe_hello", vec![sub_id], Id::Num(1)); | ||
let unsub_1: String = deser_call(client.send_request_text(unsub_call).await.unwrap()); | ||
assert_eq!(&unsub_1, "Unsubscribed"); | ||
|
||
let unsub_call = call("unsubscribe_hello", vec![sub_id], Id::Num(2)); | ||
let unsub_2 = client.send_request_text(unsub_call).await.unwrap(); | ||
let unsub_2_err: RpcError = serde_json::from_str(&unsub_2).unwrap(); | ||
let sub_id = to_json_raw_value(&sub_id).unwrap(); | ||
|
||
let err = Some(to_json_raw_value(&format!("Invalid subscription ID={}", sub_id)).unwrap()); | ||
assert_eq!(unsub_2_err, RpcError::new(invalid_subscription_err(err.as_deref()), v2::Id::Number(2))); | ||
} | ||
|
||
#[tokio::test] | ||
async fn unsubscribe_wrong_sub_id_type() { | ||
init_logger(); | ||
let addr = server().await; | ||
let mut client = WebSocketTestClient::new(addr).with_default_timeout().await.unwrap().unwrap(); | ||
|
||
let unsub = | ||
client.send_request_text(call("unsubscribe_hello", vec!["string_is_not_supported"], Id::Num(0))).await.unwrap(); | ||
let unsub_2_err: RpcError = serde_json::from_str(&unsub).unwrap(); | ||
let err = Some(to_json_raw_value(&"Invalid subscription ID type, must be integer").unwrap()); | ||
assert_eq!(unsub_2_err, RpcError::new(invalid_subscription_err(err.as_deref()), v2::Id::Number(0))); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
unrelated bug I found
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.
Oops. No test for this eh?
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.
Indeed