Skip to content

Commit

Permalink
Test new ffi callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Oct 2, 2023
1 parent 951ad3b commit 449f003
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
3 changes: 2 additions & 1 deletion base_layer/contacts/src/contacts_service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ where T: ContactsBackend + 'static
}

async fn handle_confirmation(&mut self, dispatch: MessageDispatch) -> Result<(), ContactsServiceError> {
let (message_id, delivery, read) = match dispatch {
let (message_id, delivery, read) = match dispatch.clone() {
MessageDispatch::DeliveryConfirmation(c) => (c.message_id, Some(c.timestamp), None),
MessageDispatch::ReadConfirmation(c) => (c.message_id, None, Some(c.timestamp)),
_ => {
Expand All @@ -600,6 +600,7 @@ where T: ContactsBackend + 'static

trace!(target: LOG_TARGET, "Handling confirmation with details: message_id: {:?}, delivery: {:?}, read: {:?}", message_id, delivery, read);
self.db.confirm_message(message_id, delivery, read)?;
let _msg = self.message_publisher.send(Arc::new(dispatch));

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions integration_tests/src/chat_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub fn test_config(address: Multiaddr) -> ApplicationConfig {
chat_client_config.p2p.transport.tcp.listener_address = address.clone();
chat_client_config.p2p.public_addresses = MultiaddrList::from(vec![address]);
chat_client_config.log_path = Some(PathBuf::from("log/chat_client/chat_client.log"));
chat_client_config.log_verbosity = Some(11);

ApplicationConfig {
chat_client: chat_client_config,
Expand Down
16 changes: 16 additions & 0 deletions integration_tests/src/chat_ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ extern "C" fn callback_message_received(_state: *mut c_void) {
*callback.message_received.lock().unwrap() += 1;
}

extern "C" fn callback_delivery_confirmation_received(_state: *mut c_void) {
let callback = ChatCallback::instance();
*callback.delivery_confirmation_received.lock().unwrap() += 1;
}

extern "C" fn callback_read_confirmation_received(_state: *mut c_void) {
let callback = ChatCallback::instance();
*callback.read_confirmation_received.lock().unwrap() += 1;
}

#[cfg_attr(windows, link(name = "minotari_chat_ffi.dll"))]
#[cfg_attr(not(windows), link(name = "minotari_chat_ffi"))]
extern "C" {
Expand All @@ -66,6 +76,8 @@ extern "C" {
error_out: *const c_int,
callback_contact_status_change: unsafe extern "C" fn(*mut c_void),
callback_message_received: unsafe extern "C" fn(*mut c_void),
callback_delivery_confirmation_received: unsafe extern "C" fn(*mut c_void),
callback_read_confirmation_received: unsafe extern "C" fn(*mut c_void),
) -> *mut ClientFFI;
pub fn create_chat_message(receiver: *mut c_void, message: *const c_char, error_out: *const c_int) -> *mut c_void;
pub fn send_chat_message(client: *mut ClientFFI, message: *mut c_void, error_out: *const c_int);
Expand Down Expand Up @@ -252,6 +264,8 @@ pub async fn spawn_ffi_chat_client(name: &str, seed_peers: Vec<Peer>, base_dir:
error_out,
callback_contact_status_change,
callback_message_received,
callback_delivery_confirmation_received,
callback_read_confirmation_received,
);
}

Expand All @@ -268,6 +282,8 @@ static START: Once = Once::new();
pub struct ChatCallback {
pub contact_status_change: Mutex<u64>,
pub message_received: Mutex<u64>,
pub delivery_confirmation_received: Mutex<u64>,
pub read_confirmation_received: Mutex<u64>,
}

impl ChatCallback {
Expand Down
19 changes: 19 additions & 0 deletions integration_tests/tests/features/ChatFFI.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@ Feature: Chat FFI messaging
Then there will be a MessageReceived callback of at least 1
Then CHAT_B will have 1 message with CHAT_A

Scenario: Callback for delivery confirmation received
Given I have a seed node SEED_A
When I have a chat FFI client CHAT_A connected to seed node SEED_A
When I have a chat FFI client CHAT_B connected to seed node SEED_A
When I use CHAT_A to send a message 'Hey there' to CHAT_B
Then there will be a DeliveryConfirmationReceived callback of at least 1
Then CHAT_B will have 1 message with CHAT_A
Then CHAT_A and CHAT_B will have a message 'Hey there' with matching delivery timestamps

Scenario: Callback for read confirmation received
Given I have a seed node SEED_A
When I have a chat FFI client CHAT_A connected to seed node SEED_A
When I have a chat FFI client CHAT_B connected to seed node SEED_A
When I use CHAT_A to send a message 'Hey there' to CHAT_B
Then CHAT_B will have 1 message with CHAT_A
When CHAT_B sends a read receipt to CHAT_A for message 'Hey there'
Then there will be a ReadConfirmationReceived callback of at least 1
Then CHAT_A and CHAT_B will have a message 'Hey there' with matching read timestamps

# Also flaky on CI. Seems liveness has issues on CI
@broken
Scenario: Callback for status change is received
Expand Down
38 changes: 38 additions & 0 deletions integration_tests/tests/steps/chat_ffi_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,44 @@ async fn message_reveived_callback(_world: &mut TariWorld, callback_count: usize
);
}

#[then(expr = "there will be a DeliveryConfirmationReceived callback of at least {int}")]
async fn delivery_confirmation_reveived_callback(_world: &mut TariWorld, callback_count: usize) {
let mut count = 0;
for _ in 0..(TWO_MINUTES_WITH_HALF_SECOND_SLEEP) {
count = *ChatCallback::instance().delivery_confirmation_received.lock().unwrap();

if count >= callback_count as u64 {
return;
}

tokio::time::sleep(Duration::from_millis(HALF_SECOND)).await;
}

panic!(
"contact status update never received. Callbacks expected: {}, Callbacks received: {:?}",
callback_count, count
);
}

#[then(expr = "there will be a ReadConfirmationReceived callback of at least {int}")]
async fn read_confirmation_received_callback(_world: &mut TariWorld, callback_count: usize) {
let mut count = 0;
for _ in 0..(TWO_MINUTES_WITH_HALF_SECOND_SLEEP) {
count = *ChatCallback::instance().read_confirmation_received.lock().unwrap();

if count >= callback_count as u64 {
return;
}

tokio::time::sleep(Duration::from_millis(HALF_SECOND)).await;
}

panic!(
"contact status update never received. Callbacks expected: {}, Callbacks received: {:?}",
callback_count, count
);
}

#[then(expr = "I can shutdown {word} without a problem")]
async fn can_shutdown(world: &mut TariWorld, name: String) {
let mut client = world.chat_clients.remove(&name).unwrap();
Expand Down

0 comments on commit 449f003

Please sign in to comment.