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

release/0.3.2 #5

Merged
merged 18 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
39 changes: 0 additions & 39 deletions src/ic-websocket-cdk/src/tests/unit_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,45 +106,6 @@ fn test_ws_handlers_are_called() {
assert!(CUSTOM_STATE.with(|h| h.borrow().is_on_close_called));
}

#[test]
fn test_ws_handlers_panic_is_handled() {
let h = WsHandlers {
on_open: Some(|_| {
panic!("on_open_panic");
}),
on_message: Some(|_| {
panic!("on_close_panic");
}),
on_close: Some(|_| {
panic!("on_close_panic");
}),
};

set_params(WsInitParams::new(h));

let handlers = get_handlers_from_params();

let res = panic::catch_unwind(|| {
handlers.call_on_open(OnOpenCallbackArgs {
client_principal: common::generate_random_principal(),
});
});
assert!(res.is_ok());
let res = panic::catch_unwind(|| {
handlers.call_on_message(OnMessageCallbackArgs {
client_principal: common::generate_random_principal(),
message: vec![],
});
});
assert!(res.is_ok());
let res = panic::catch_unwind(|| {
handlers.call_on_close(OnCloseCallbackArgs {
client_principal: common::generate_random_principal(),
});
});
assert!(res.is_ok());
}

#[test]
fn test_ws_init_params() {
let handlers = WsHandlers::default();
Expand Down
38 changes: 12 additions & 26 deletions src/ic-websocket-cdk/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{collections::VecDeque, fmt, panic, time::Duration};
use std::{collections::VecDeque, fmt, time::Duration};

use candid::{decode_one, CandidType, Principal};
use serde::{Deserialize, Serialize};
use serde_cbor::Serializer;

use crate::{
custom_print, custom_trap, errors::WsError, utils::get_current_time,
DEFAULT_CLIENT_KEEP_ALIVE_TIMEOUT_MS, DEFAULT_MAX_NUMBER_OF_RETURNED_MESSAGES,
DEFAULT_SEND_ACK_INTERVAL_MS, INITIAL_OUTGOING_MESSAGE_NONCE,
custom_trap, errors::WsError, utils::get_current_time, DEFAULT_CLIENT_KEEP_ALIVE_TIMEOUT_MS,
DEFAULT_MAX_NUMBER_OF_RETURNED_MESSAGES, DEFAULT_SEND_ACK_INTERVAL_MS,
INITIAL_OUTGOING_MESSAGE_NONCE,
};

pub type ClientPrincipal = Principal;
Expand Down Expand Up @@ -173,7 +173,7 @@ impl RegisteredGateway {

/// Decrements the connected clients count by 1, returning the new value.
pub(crate) fn decrement_clients_count(&mut self) -> u64 {
self.connected_clients_count -= 1;
ilbertt marked this conversation as resolved.
Show resolved Hide resolved
self.connected_clients_count = self.connected_clients_count.saturating_sub(1);
self.connected_clients_count
}

Expand Down Expand Up @@ -340,37 +340,23 @@ pub struct WsHandlers {
impl WsHandlers {
pub(crate) fn call_on_open(&self, args: OnOpenCallbackArgs) {
if let Some(on_open) = self.on_open {
let res = panic::catch_unwind(|| {
on_open(args);
});

if let Err(e) = res {
custom_print!("Error calling on_open handler: {:?}", e);
}
// we don't have to recover from errors here,
// we just let the canister trap
on_open(args);
}
}

pub(crate) fn call_on_message(&self, args: OnMessageCallbackArgs) {
if let Some(on_message) = self.on_message {
let res = panic::catch_unwind(|| {
on_message(args);
});

if let Err(e) = res {
custom_print!("Error calling on_message handler: {:?}", e);
}
// see call_on_open
on_message(args);
}
}

pub(crate) fn call_on_close(&self, args: OnCloseCallbackArgs) {
if let Some(on_close) = self.on_close {
let res = panic::catch_unwind(|| {
on_close(args);
});

if let Err(e) = res {
custom_print!("Error calling on_close handler: {:?}", e);
}
// see call_on_open
on_close(args);
}
}
}
Expand Down