Skip to content

Commit

Permalink
ShvBroker methods subscribe/unsubscribe tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanda Vacek committed Jan 1, 2024
1 parent d3229a5 commit 1f41522
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/broker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ pub(crate) async fn broker_loop(events: Receiver<ClientEvent>, access: AccessCon
if node.is_request_granted(&rpcmsg2) {
node.process_request(&rpcmsg2)
} else {
let err = RpcError::new(RpcErrorCode::PermissionDenied, &format!("Request to call {}:{} is not granted.", rpcmsg2.shv_path().unwrap_or_default(), rpcmsg2.method().unwrap_or_default()));
let err = RpcError::new(RpcErrorCode::PermissionDenied, &format!("Method doesn't exist or request to call {}:{} is not granted.", shv_path, rpcmsg2.method().unwrap_or_default()));
Command::Error(err)
}
} else {
Expand Down Expand Up @@ -560,6 +560,9 @@ pub async fn accept_loop(config: BrokerConfig, access: AccessControl) -> crate::

#[cfg(test)]
mod tests {
use crate::broker::node::METH_SUBSCRIBE;
use crate::broker::node::METH_UNSUBSCRIBE;
use crate::Map;
use super::*;

#[async_std::test]
Expand Down Expand Up @@ -613,6 +616,23 @@ mod tests {
assert_eq!(m.get("userName").unwrap(), &RpcValue::from("admin"));
assert_eq!(m.get("subscriptions").unwrap(), &RpcValue::from(List::new()));

// subscriptions
let subs_param = Map::from([("paths".to_string(), RpcValue::from("shv/**"))]);
{
call(".app/broker/currentClient", METH_SUBSCRIBE, Some(RpcValue::from(subs_param.clone())), &call_ctx).await;
let resp = call(".app/broker/currentClient", "info", None, &call_ctx).await;
let subs = resp.as_map().get("subscriptions").unwrap();
let subs_list = subs.as_list();
assert_eq!(subs_list.len(), 1);
}
{
call(".app/broker/currentClient", METH_UNSUBSCRIBE, Some(RpcValue::from(subs_param.clone())), &call_ctx).await;
let resp = call(".app/broker/currentClient", "info", None, &call_ctx).await;
let subs = resp.as_map().get("subscriptions").unwrap();
let subs_list = subs.as_list();
assert_eq!(subs_list.len(), 0);
}

broker_task.cancel().await;
}
}
4 changes: 2 additions & 2 deletions src/broker/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ const APP_BROKER_CURRENT_CLIENT_METHODS: [MetaMethod; 3] = [
MetaMethod { name: METH_UNSUBSCRIBE, flags: Flag::None as u32, access: Access::Read, param: "SubscribeParams", result: "void", description: "" },
];
const METH_INFO: &str = "info";
const METH_SUBSCRIBE: &str = "subscribe";
const METH_UNSUBSCRIBE: &str = "unsubscribe";
pub const METH_SUBSCRIBE: &str = "subscribe";
pub const METH_UNSUBSCRIBE: &str = "unsubscribe";

pub(crate) struct AppBrokerCurrentClientNode {}
impl ShvNode<BrokerCommand> for AppBrokerCurrentClientNode {
Expand Down
2 changes: 2 additions & 0 deletions src/rpcmessage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,13 @@ impl TryFrom<i32> for RpcErrorCode {
x if x == RpcErrorCode::MethodCallTimeout as i32 => Ok(RpcErrorCode::MethodCallTimeout),
x if x == RpcErrorCode::MethodCallCancelled as i32 => Ok(RpcErrorCode::MethodCallCancelled),
x if x == RpcErrorCode::MethodCallException as i32 => Ok(RpcErrorCode::MethodCallException),
x if x == RpcErrorCode::PermissionDenied as i32 => Ok(RpcErrorCode::PermissionDenied),
x if x == RpcErrorCode::Unknown as i32 => Ok(RpcErrorCode::Unknown),
_ => Err(()),
}
}
}

pub struct RpcError {
pub code: RpcErrorCode,
pub message: String,
Expand Down

0 comments on commit 1f41522

Please sign in to comment.