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.3 #6

Merged
merged 9 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ic-websocket-cdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ic-websocket-cdk"
version = "0.3.2"
version = "0.3.3"
edition.workspace = true
rust-version.workspace = true
repository.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion src/ic-websocket-cdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod errors;
mod state;
mod tests;
mod timers;
mod types;
pub mod types;
mod utils;

use state::*;
Expand Down
16 changes: 7 additions & 9 deletions src/ic-websocket-cdk/src/tests/integration_tests/a_ws_open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ fn test_2_should_open_a_connection() {
let res = call_ws_open(&client_1_key.client_principal, args);
assert_eq!(res, CanisterWsOpenResult::Ok(()));

let CanisterOutputCertifiedMessages { messages, .. } = call_ws_get_messages_with_panic(
GATEWAY_1.deref(),
CanisterWsGetMessagesArguments { nonce: 0 },
);
let CanisterOutputCertifiedMessages { messages, .. } =
call_ws_get_messages_with_panic(GATEWAY_1.deref(), CanisterWsGetMessagesArguments::new(0));

let first_message = &messages[0];
assert_eq!(first_message.client_key, *client_1_key);
Expand Down Expand Up @@ -82,10 +80,10 @@ proptest! {

#[test]
fn test_4_should_open_a_connection_for_the_same_client_with_a_different_nonce(test_client_nonce in any::<u64>().prop_map(|_| generate_random_client_nonce())) {
let client_key = ClientKey {
client_principal: CLIENT_1_KEY.deref().client_principal,
client_nonce: test_client_nonce,
};
let client_key = ClientKey::new(
CLIENT_1_KEY.deref().client_principal,
test_client_nonce
);
let args = CanisterWsOpenArguments {
client_nonce: client_key.client_nonce,
gateway_principal: GATEWAY_1.deref().to_owned(),
Expand All @@ -95,7 +93,7 @@ proptest! {

let CanisterOutputCertifiedMessages { messages, .. } = call_ws_get_messages_with_panic(
GATEWAY_1.deref(),
CanisterWsGetMessagesArguments { nonce: 0 },
CanisterWsGetMessagesArguments::new(0),
);

let service_message_for_client = messages
Expand Down
91 changes: 44 additions & 47 deletions src/ic-websocket-cdk/src/tests/integration_tests/b_ws_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ fn test_1_fails_if_client_is_not_registered() {
let client_2_key = CLIENT_2_KEY.deref();
let res = call_ws_message(
&client_2_key.client_principal,
CanisterWsMessageArguments {
msg: create_websocket_message(client_2_key, 0, None, false),
},
CanisterWsMessageArguments::new(create_websocket_message(client_2_key, 0, None, false)),
);

assert_eq!(
Expand Down Expand Up @@ -69,9 +67,12 @@ fn test_2_fails_if_client_sends_a_message_with_a_different_client_key() {
// first, send a message with a different principal
let res = call_ws_message(
&client_1_key.client_principal,
CanisterWsMessageArguments {
msg: create_websocket_message(&wrong_client_key, 0, None, false),
},
CanisterWsMessageArguments::new(create_websocket_message(
&wrong_client_key,
0,
None,
false,
)),
);
assert_eq!(
res,
Expand All @@ -91,9 +92,12 @@ fn test_2_fails_if_client_sends_a_message_with_a_different_client_key() {
// then, send a message with a different nonce
let res = call_ws_message(
&client_1_key.client_principal,
CanisterWsMessageArguments {
msg: create_websocket_message(&wrong_client_key, 0, None, false),
},
CanisterWsMessageArguments::new(create_websocket_message(
&wrong_client_key,
0,
None,
false,
)),
);
assert_eq!(
res,
Expand All @@ -116,9 +120,7 @@ fn test_3_should_send_a_message_from_a_registered_client() {

let res = call_ws_message(
&client_1_key.client_principal,
CanisterWsMessageArguments {
msg: create_websocket_message(client_1_key, 1, None, false),
},
CanisterWsMessageArguments::new(create_websocket_message(client_1_key, 1, None, false)),
);
assert_eq!(res, CanisterWsMessageResult::Ok(()));
}
Expand All @@ -135,9 +137,12 @@ fn test_4_fails_if_client_sends_a_message_with_a_wrong_sequence_number() {
let expected_sequence_number = 1; // the next valid sequence number
let res = call_ws_message(
&client_1_key.client_principal,
CanisterWsMessageArguments {
msg: create_websocket_message(client_1_key, wrong_sequence_number, None, false),
},
CanisterWsMessageArguments::new(create_websocket_message(
client_1_key,
wrong_sequence_number,
None,
false,
)),
);
assert_eq!(
res,
Expand All @@ -153,7 +158,7 @@ fn test_4_fails_if_client_sends_a_message_with_a_wrong_sequence_number() {
// check if the gateway put the close message in the queue
let msgs = call_ws_get_messages_with_panic(
GATEWAY_1.deref(),
CanisterWsGetMessagesArguments { nonce: 1 }, // skip the first open message
CanisterWsGetMessagesArguments::new(1), // skip the first open message
);
check_canister_message_has_close_reason(
&msgs.messages[0],
Expand All @@ -164,9 +169,7 @@ fn test_4_fails_if_client_sends_a_message_with_a_wrong_sequence_number() {
// so calling the ws_close endpoint should return the ClientKeyNotConnected error
let res = call_ws_close(
GATEWAY_1.deref(),
CanisterWsCloseArguments {
client_key: client_1_key.clone(),
},
CanisterWsCloseArguments::new(client_1_key.clone()),
);
assert_eq!(
res,
Expand All @@ -190,14 +193,12 @@ fn test_5_fails_if_client_sends_a_wrong_service_message() {
// fail with wrong content encoding
let res = call_ws_message(
&client_1_key.client_principal,
CanisterWsMessageArguments {
msg: create_websocket_message(
client_1_key,
1,
Some(encode_one(vec![1, 2, 3]).unwrap()),
true,
),
},
CanisterWsMessageArguments::new(create_websocket_message(
client_1_key,
1,
Some(encode_one(vec![1, 2, 3]).unwrap()),
true,
)),
);
assert!(res
.err()
Expand All @@ -211,16 +212,14 @@ fn test_5_fails_if_client_sends_a_wrong_service_message() {
});
let res = call_ws_message(
&client_1_key.client_principal,
CanisterWsMessageArguments {
msg: create_websocket_message(
client_1_key,
2,
Some(encode_websocket_service_message_content(
&wrong_service_message,
)),
true,
),
},
CanisterWsMessageArguments::new(create_websocket_message(
client_1_key,
2,
Some(encode_websocket_service_message_content(
&wrong_service_message,
)),
true,
)),
);
assert_eq!(
res,
Expand All @@ -242,16 +241,14 @@ fn test_6_should_send_a_service_message_from_a_registered_client() {
});
let res = call_ws_message(
&client_1_key.client_principal,
CanisterWsMessageArguments {
msg: create_websocket_message(
client_1_key,
1,
Some(encode_websocket_service_message_content(
&client_service_message,
)),
true,
),
},
CanisterWsMessageArguments::new(create_websocket_message(
client_1_key,
1,
Some(encode_websocket_service_message_content(
&client_service_message,
)),
true,
)),
);
assert_eq!(res, CanisterWsMessageResult::Ok(()));
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,11 @@ proptest! {

let res = call_ws_get_messages_with_panic(
test_gateway_principal,
CanisterWsGetMessagesArguments { nonce: 0 },
CanisterWsGetMessagesArguments::new(0),
);
assert_eq!(
res,
CanisterOutputCertifiedMessages {
messages: vec![],
cert: vec![],
tree: vec![],
is_end_of_queue: true,
},
CanisterOutputCertifiedMessages::empty()
);
}

Expand All @@ -53,7 +48,7 @@ proptest! {

let CanisterOutputCertifiedMessages { messages, .. } = call_ws_get_messages_with_panic(
GATEWAY_1.deref(),
CanisterWsGetMessagesArguments { nonce: 0 },
CanisterWsGetMessagesArguments::new(0),
);
prop_assert_eq!(messages.len(), 1); // we expect only the service message
}
Expand Down Expand Up @@ -85,7 +80,7 @@ proptest! {
..
} = call_ws_get_messages_with_panic(
GATEWAY_1.deref(),
CanisterWsGetMessagesArguments { nonce: i },
CanisterWsGetMessagesArguments::new(i),
);
prop_assert_eq!(
messages.len() as u64,
Expand All @@ -104,18 +99,11 @@ proptest! {
// try to get more messages than available
let res = call_ws_get_messages_with_panic(
GATEWAY_1.deref(),
CanisterWsGetMessagesArguments {
nonce: messages_count,
},
CanisterWsGetMessagesArguments::new(messages_count),
);
prop_assert_eq!(
res,
CanisterOutputCertifiedMessages {
messages: vec![],
cert: vec![],
tree: vec![],
is_end_of_queue: true,
}
CanisterOutputCertifiedMessages::empty()
);
}

Expand Down Expand Up @@ -146,9 +134,7 @@ proptest! {
..
} = call_ws_get_messages_with_panic(
GATEWAY_1.deref(),
CanisterWsGetMessagesArguments {
nonce: next_polling_nonce,
},
CanisterWsGetMessagesArguments::new(next_polling_nonce),
);
verify_messages(
&messages,
Expand Down Expand Up @@ -196,7 +182,7 @@ proptest! {
..
} = call_ws_get_messages_with_panic(
GATEWAY_1.deref(),
CanisterWsGetMessagesArguments { nonce: 0 },
CanisterWsGetMessagesArguments::new(0),
);

assert_eq!(
Expand Down Expand Up @@ -250,9 +236,7 @@ fn test_6_empty_gateway_can_get_messages_until_next_keep_alive_check() {
// disconnect the client and check that gateway can still receive the messages
call_ws_close_with_panic(
&GATEWAY_1,
CanisterWsCloseArguments {
client_key: client_1_key.clone(),
},
CanisterWsCloseArguments::new(client_1_key.clone()),
);

// check that gateway can still receive the messages
Expand Down Expand Up @@ -296,9 +280,7 @@ fn test_7_empty_gateway_can_get_messages_until_next_keep_alive_check_if_removed_
// disconnect the client and check that gateway can still receive the messages
call_ws_close_with_panic(
&GATEWAY_1,
CanisterWsCloseArguments {
client_key: client_1_key.clone(),
},
CanisterWsCloseArguments::new(client_1_key.clone()),
);

let expected_messages_len = send_messages_count + 1; // +1 for the ack message
Expand Down Expand Up @@ -330,10 +312,8 @@ mod helpers {
use super::*;

pub(super) fn assert_gateway_has_messages(send_messages_count: usize) {
let CanisterOutputCertifiedMessages { messages, .. } = call_ws_get_messages_with_panic(
&GATEWAY_1,
CanisterWsGetMessagesArguments { nonce: 0 },
);
let CanisterOutputCertifiedMessages { messages, .. } =
call_ws_get_messages_with_panic(&GATEWAY_1, CanisterWsGetMessagesArguments::new(0));
assert_eq!(
messages.len(),
// + 1 for the open service message
Expand All @@ -342,10 +322,8 @@ mod helpers {
}

pub(super) fn assert_gateway_has_no_messages() {
let CanisterOutputCertifiedMessages { messages, .. } = call_ws_get_messages_with_panic(
&GATEWAY_1,
CanisterWsGetMessagesArguments { nonce: 0 },
);
let CanisterOutputCertifiedMessages { messages, .. } =
call_ws_get_messages_with_panic(&GATEWAY_1, CanisterWsGetMessagesArguments::new(0));
assert_eq!(messages.len(), 0);
}
}
Loading
Loading