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

feat(rpc): Add method removal functionality for RPC transports #10497

Merged
merged 6 commits into from
Aug 24, 2024
Merged
Changes from 1 commit
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: 122 additions & 0 deletions crates/rpc/rpc-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,50 @@ impl TransportRpcModules {
self.merge_ipc(other)?;
Ok(())
}

/// Removes the method with the given name from the configured http methods.
///
/// Returns `true` if the method was found and removed, `false` otherwise.
pub fn remove_http_method(&mut self, method_name: &'static str) -> bool {
mattsse marked this conversation as resolved.
Show resolved Hide resolved
if let Some(http_module) = &mut self.http {
http_module.remove_method(method_name).is_some()
} else {
false
}
}

/// Removes the method with the given name from the configured ws methods.
///
/// Returns `true` if the method was found and removed, `false` otherwise.
pub fn remove_ws_method(&mut self, method_name: &'static str) -> bool {
if let Some(ws_module) = &mut self.ws {
ws_module.remove_method(method_name).is_some()
} else {
false
}
}

/// Removes the method with the given name from the configured ipc methods.
///
/// Returns `true` if the method was found and removed, `false` otherwise.
pub fn remove_ipc_method(&mut self, method_name: &'static str) -> bool {
if let Some(ipc_module) = &mut self.ipc {
ipc_module.remove_method(method_name).is_some()
} else {
false
}
}

/// Removes the method with the given name from all configured transports.
///
/// Returns `true` if the method was found and removed, `false` otherwise.
pub fn remove_method_from_all_transports(&mut self, method_name: &'static str) -> bool {
SkandaBhat marked this conversation as resolved.
Show resolved Hide resolved
let http_removed = self.remove_http_method(method_name);
let ws_removed = self.remove_ws_method(method_name);
let ipc_removed = self.remove_ipc_method(method_name);

http_removed || ws_removed || ipc_removed
}
}

/// A handle to the spawned servers.
Expand Down Expand Up @@ -1967,4 +2011,82 @@ mod tests {
}
)
}

mod remove_methods {
use super::*;

fn create_test_module() -> RpcModule<()> {
let mut module = RpcModule::new(());
module.register_method("anything", |_, _, _| "succeed").unwrap();
module
}

#[test]
fn test_remove_http_method() {
let mut modules =
TransportRpcModules { http: Some(create_test_module()), ..Default::default() };
// Remove a method that exists
assert!(modules.remove_http_method("anything"));

// Remove a method that does not exist
assert!(!modules.remove_http_method("non_existent_method"));

// Verify that the method was removed
assert!(modules.http.as_ref().unwrap().method("anything").is_none());
}

#[test]
fn test_remove_ws_method() {
let mut modules =
TransportRpcModules { ws: Some(create_test_module()), ..Default::default() };

// Remove a method that exists
assert!(modules.remove_ws_method("anything"));

// Remove a method that does not exist
assert!(!modules.remove_ws_method("non_existent_method"));

// Verify that the method was removed
assert!(modules.ws.as_ref().unwrap().method("anything").is_none());
}

#[test]
fn test_remove_ipc_method() {
let mut modules =
TransportRpcModules { ipc: Some(create_test_module()), ..Default::default() };

// Remove a method that exists
assert!(modules.remove_ipc_method("anything"));

// Remove a method that does not exist
assert!(!modules.remove_ipc_method("non_existent_method"));

// Verify that the method was removed
assert!(modules.ipc.as_ref().unwrap().method("anything").is_none());
}

#[test]
fn test_remove_method_from_all_transports() {
let mut modules = TransportRpcModules {
http: Some(create_test_module()),
ws: Some(create_test_module()),
ipc: Some(create_test_module()),
..Default::default()
};

// Remove a method that exists
assert!(modules.remove_method_from_all_transports("anything"));

// Remove a method that was just removed (it does not exist anymore)
assert!(!modules.remove_method_from_all_transports("anything"));

// Remove a method that does not exist
assert!(!modules.remove_method_from_all_transports("non_existent_method"));

// Verify that the method was removed from all transports
assert!(modules.http.as_ref().unwrap().method("anything").is_none());
assert!(modules.ws.as_ref().unwrap().method("anything").is_none());
assert!(modules.ipc.as_ref().unwrap().method("anything").is_none());
}
}
}
Loading