Skip to content

Commit

Permalink
test: add test to cover the convertion between CurpError and tonic::S…
Browse files Browse the repository at this point in the history
…tatus

Signed-off-by: Phoeniix Zhao <[email protected]>
  • Loading branch information
Phoenix500526 committed Oct 20, 2023
1 parent 6cbd057 commit daec800
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion curp/src/rpc/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl ConnectApi for Connect<ProtocolClient<Channel>> {
client.wait_synced(req).await
}

/// Send `FetchLeaderRequest`
/// Send `FetchClusterRequest`
async fn fetch_cluster(
&self,
request: FetchClusterRequest,
Expand Down
55 changes: 55 additions & 0 deletions curp/src/server/curp_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,8 @@ impl<C: Command, RC: RoleChange> Debug for CurpNode<C, RC> {

#[cfg(test)]
mod tests {
use std::io::{Error, ErrorKind};

use curp_test_utils::{mock_role_change, sleep_secs, test_cmd::TestCommand};
use tokio::sync::oneshot;
use tracing_test::traced_test;
Expand All @@ -959,6 +961,15 @@ mod tests {
rpc::connect::MockInnerConnectApi, server::cmd_worker::MockCEEventTxApi, ConfChange,
};

fn get_error_label(status: &Status) -> &str {
let metadata = status.metadata();
metadata
.get("error-label")
.expect("error-label should not be None in CurpError")
.to_str()
.expect("error-label must be construct by ascii char")
}

#[traced_test]
#[tokio::test]
async fn sync_task_will_send_hb() {
Expand Down Expand Up @@ -1087,4 +1098,48 @@ mod tests {
assert!(curp.is_leader());
curp.shutdown_trigger().self_shutdown_and_wait().await;
}

#[test]
fn curp_error_convert_to_tonic_status_should_success() {
let encode_decode = CurpError::EncodeDecode("CurpError::EncodeDecode".to_owned());
let status: Status = encode_decode.into();
assert_eq!("encode-decode", get_error_label(&status));

let internal = CurpError::Internal("CurpError::Internal".to_owned());
let status: Status = internal.into();
assert_eq!("internal", get_error_label(&status));

let transport = CurpError::Transport("CurpError::Transport".to_owned());
let status: Status = transport.into();
assert_eq!("transport", get_error_label(&status));

let shutdown = CurpError::ShuttingDown;
let status: Status = shutdown.into();
assert_eq!("shutting-down", get_error_label(&status));

let redirect_1 = CurpError::Redirect(Some(1), 2);
let status: Status = redirect_1.into();
assert_eq!("redirect", get_error_label(&status));
let (leader_id, term): (Option<u64>, u64) = serde_json::from_slice(status.details())
.expect(" deserialize (leader_id, term) from status' detail should always success");
assert_eq!(leader_id, Some(1));
assert_eq!(term, 2);

let redirect_2 = CurpError::Redirect(None, 2);
let status: Status = redirect_2.into();
assert_eq!("redirect", get_error_label(&status));
let (leader_id, term): (Option<u64>, u64) = serde_json::from_slice(status.details())
.expect(" deserialize (leader_id, term) from status' detail should always success");
assert_eq!(leader_id, None);
assert_eq!(term, 2);

let io = CurpError::IO(Error::new(ErrorKind::Other, "oh no!"));
let status: Status = io.into();
assert_eq!("io", get_error_label(&status));

let bincode_err = Box::new(bincode::ErrorKind::Custom("StorageError".to_owned()));
let storage = CurpError::Storage(bincode_err.into());
let status: Status = storage.into();
assert_eq!("storage", get_error_label(&status));
}
}

0 comments on commit daec800

Please sign in to comment.