Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Septias committed May 20, 2024
1 parent d3e65e3 commit 0d296e3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
52 changes: 52 additions & 0 deletions src/peer_channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ pub async fn send_webxdc_realtime_data(ctx: &Context, msg_id: MsgId, data: Vec<u

/// Leave the gossip of the webxdc with given [MsgId].
pub async fn leave_webxdc_realtime(ctx: &Context, msg_id: MsgId) -> Result<()> {
if !ctx.get_config_bool(Config::EnableRealtime).await? {
return Ok(());
}

let iroh = ctx.get_or_try_init_peer_channel().await?;
iroh.leave_realtime(get_iroh_topic_for_msg(ctx, msg_id).await?)
.await?;
Expand Down Expand Up @@ -611,6 +615,18 @@ mod tests {
let alice = &mut tcm.alice().await;
let bob = &mut tcm.bob().await;

bob.ctx
.set_config_bool(Config::EnableRealtime, true)
.await
.unwrap();

alice
.ctx
.set_config_bool(Config::EnableRealtime, true)
.await
.unwrap();

assert!(alice.get_config_bool(Config::EnableRealtime).await.unwrap());
// Alice sends webxdc to bob
let alice_chat = alice.create_chat(bob).await;
let mut instance = Message::new(Viewtype::File);
Expand Down Expand Up @@ -746,6 +762,17 @@ mod tests {
let alice = &mut tcm.alice().await;
let bob = &mut tcm.bob().await;

bob.ctx
.set_config_bool(Config::EnableRealtime, true)
.await
.unwrap();

alice
.ctx
.set_config_bool(Config::EnableRealtime, true)
.await
.unwrap();

// Alice sends webxdc to bob
let alice_chat = alice.create_chat(bob).await;
let mut instance = Message::new(Viewtype::File);
Expand Down Expand Up @@ -808,4 +835,29 @@ mod tests {
}
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_peer_channels_disable() {
let mut tcm = TestContextManager::new();
let alice = &mut tcm.alice().await;

// creates iroh endpoint as side effect
send_webxdc_realtime_advertisement(alice, MsgId::new(1))
.await
.unwrap();

assert!(alice.ctx.iroh.get().is_none());

// creates iroh endpoint as side effect
send_webxdc_realtime_data(alice, MsgId::new(1), vec![])
.await
.unwrap();

assert!(alice.ctx.iroh.get().is_none());

// creates iroh endpoint as side effect
leave_webxdc_realtime(alice, MsgId::new(1)).await.unwrap();

assert!(alice.ctx.iroh.get().is_none())
}
}
4 changes: 2 additions & 2 deletions src/receive_imf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,9 +1427,9 @@ async fn add_parts(
if let Some(node_addr) = mime_parser.get_header(HeaderDef::IrohNodeAddr) {
match serde_json::from_str::<NodeAddr>(node_addr).context("Failed to parse node address") {
Ok(node_addr) => {
info!(context, "Adding iroh peer with address {node_addr:?}.");
let instance_id = parent.context("Failed to get parent message")?.id;
let node_id = node_addr.node_id;
info!(context, "Adding iroh peer with address pubkey {node_id}",);
let instance_id = parent.context("Failed to get parent message")?.id;
let relay_server = node_addr.relay_url().map(|relay| relay.as_str());
let topic = get_iroh_topic_for_msg(context, instance_id).await?;
iroh_add_peer_for_topic(context, instance_id, topic, node_id, relay_server).await?;
Expand Down

0 comments on commit 0d296e3

Please sign in to comment.