Skip to content

Commit 7b03bf7

Browse files
authored
E2E RPC tests (#769)
1 parent 9424a19 commit 7b03bf7

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

livekit/tests/rpc_test.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright 2025 LiveKit, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#[cfg(feature = "__lk-e2e-test")]
16+
use {
17+
anyhow::{Context, Result},
18+
common::test_rooms,
19+
livekit::prelude::PerformRpcData,
20+
std::time::Duration,
21+
};
22+
23+
mod common;
24+
25+
#[cfg(feature = "__lk-e2e-test")]
26+
#[test_log::test(tokio::test)]
27+
pub async fn test_rpc_invocation() -> Result<()> {
28+
let mut rooms = test_rooms(2).await?;
29+
let (caller_room, _) = rooms.pop().unwrap();
30+
let (callee_room, _) = rooms.pop().unwrap();
31+
let callee_identity = callee_room.local_participant().identity();
32+
33+
const METHOD_NAME: &str = "test-method";
34+
const PAYLOAD: &str = "test-payload";
35+
36+
callee_room.local_participant().register_rpc_method(METHOD_NAME.to_string(), |data| {
37+
// Echo caller payload as return value
38+
Box::pin(async move { Ok(data.payload.to_string()) })
39+
});
40+
41+
let perform_data = PerformRpcData {
42+
method: METHOD_NAME.to_string(),
43+
destination_identity: callee_identity.to_string(),
44+
payload: PAYLOAD.to_string(),
45+
response_timeout: Duration::from_millis(500),
46+
..Default::default()
47+
};
48+
let return_payload = caller_room
49+
.local_participant()
50+
.perform_rpc(perform_data)
51+
.await
52+
.context("Invocation failed")?;
53+
assert_eq!(return_payload, PAYLOAD, "Unexpected return value");
54+
Ok(())
55+
}
56+
57+
#[cfg(feature = "__lk-e2e-test")]
58+
#[test_log::test(tokio::test)]
59+
pub async fn test_rpc_unregistered() -> Result<()> {
60+
let mut rooms = test_rooms(2).await?;
61+
let (caller_room, _) = rooms.pop().unwrap();
62+
let (callee_room, _) = rooms.pop().unwrap();
63+
let callee_identity = callee_room.local_participant().identity();
64+
65+
const METHOD_NAME: &str = "unregistered-method";
66+
const PAYLOAD: &str = "test-payload";
67+
68+
let perform_data = PerformRpcData {
69+
method: METHOD_NAME.to_string(),
70+
destination_identity: callee_identity.to_string(),
71+
payload: PAYLOAD.to_string(),
72+
response_timeout: Duration::from_millis(500),
73+
..Default::default()
74+
};
75+
let result = caller_room.local_participant().perform_rpc(perform_data).await;
76+
assert!(result.is_err(), "Expected error");
77+
Ok(())
78+
}
79+
80+
#[cfg(feature = "__lk-e2e-test")]
81+
#[test_log::test(tokio::test)]
82+
pub async fn test_rpc_unknown_destination() -> Result<()> {
83+
let mut rooms = test_rooms(1).await?;
84+
let (caller_room, _) = rooms.pop().unwrap();
85+
86+
let perform_data = PerformRpcData {
87+
method: "unregistered-method".to_string(),
88+
destination_identity: "unknown-participant".to_string(),
89+
payload: "test-payload".to_string(),
90+
response_timeout: Duration::from_millis(500),
91+
..Default::default()
92+
};
93+
let result = caller_room.local_participant().perform_rpc(perform_data).await;
94+
assert!(result.is_err(), "Expected error");
95+
Ok(())
96+
}

0 commit comments

Comments
 (0)