webrtc_connection/utils/
test_helpers.rs1use cs_utils::random_str;
2use tokio::try_join;
3use tokio::io::duplex;
4use webrtc::{peer_connection::configuration::RTCConfiguration, ice_transport::ice_server::RTCIceServer};
5use connection_utils::{Channel, Connected};
6
7use crate::rtc_connection::RtcConnection;
8
9type TConnection = Box<dyn Connected>;
10type TChannel = Box<dyn Channel>;
11
12pub async fn create_connection_pair() -> (TConnection, TConnection) {
14 let (stream1, stream2 ) = duplex(4096);
15
16 let rtc_config1 = RTCConfiguration {
17 ice_servers: vec![RTCIceServer {
18 urls: vec!["stun:stun.l.google.com:19302".to_owned()],
19 ..Default::default()
20 }],
21 ..Default::default()
22 };
23
24 let rtc_config2 = RTCConfiguration {
25 ice_servers: vec![RTCIceServer {
26 urls: vec!["stun:stun.l.google.com:19302".to_owned()],
27 ..Default::default()
28 }],
29 ..Default::default()
30 };
31
32 let (client_connection, server_connection) = try_join!(
33 tokio::spawn(async move {
34 let connection = RtcConnection::new(
35 Box::new(stream1),
36 rtc_config1,
37 )
38 .await
39 .expect("Cannot create RTC connection.");
40
41 return connection
42 .connect()
43 .await
44 .expect("Failed to connect.");
45 }),
46 tokio::spawn(async move {
47 let connection = RtcConnection::new(
48 Box::new(stream2),
49 rtc_config2,
50 )
51 .await
52 .expect("Cannot create RTC connection.");
53
54 return connection
55 .listen()
56 .await
57 .expect("Failed to connect.");
58 }),
59 ).unwrap();
60
61 return (client_connection, server_connection);
62}
63
64pub async fn create_channel_pair(
66 mut client_connection: TConnection,
67 mut server_connection: TConnection,
68) -> ((TConnection, TChannel), (TConnection, TChannel)) {
69 let data_channel_label = format!("channel:{}", random_str(8));
71 let (client_channel, server_channel) = try_join!(
72 tokio::spawn(async move {
73 let data_channel = client_connection
74 .channel(data_channel_label)
75 .await
76 .expect("Cannot create data channel.");
77
78 return (client_connection, data_channel);
79 }),
80 tokio::spawn(async move {
81 let mut on_remote_channel = server_connection.on_remote_channel()
82 .expect("Cannot get \"on_remote_channel\" stream.");
83
84 while let Some(channel) = on_remote_channel.recv().await {
85 server_connection.off_remote_channel(on_remote_channel).unwrap();
86
87 return (server_connection, channel);
88 }
89
90 panic!("Failed to receive a remote data channel.");
91 }),
92 ).unwrap();
93
94 return (client_channel, server_channel);
95}
96
97pub async fn create_channels(
99 channels_count: usize,
100) -> Vec<(TChannel, TChannel)> {
101 let (
103 mut client_connection,
104 mut server_connection,
105 ) = create_connection_pair().await;
106
107 let channels = {
108 let mut result = vec![];
109
110 for _ in 0..channels_count {
111 let (
112 (client_connection2, client_channel),
113 (server_connection2, server_channel)) = create_channel_pair(
114 client_connection,
115 server_connection,
116 ).await;
117
118 client_connection = client_connection2;
119 server_connection = server_connection2;
120
121 result.push((client_channel, server_channel));
122 }
123
124 result
125 };
126
127 return channels;
128}