|
11 | 11 | // Contributors: |
12 | 12 | // ZettaScale Zenoh Team, <zenoh@zettascale.tech> |
13 | 13 | // |
| 14 | +use std::{fmt::DebugStruct, sync::Arc, time::Duration}; |
| 15 | + |
| 16 | +use async_trait::async_trait; |
| 17 | +use tokio::sync::MutexGuard as AsyncMutexGuard; |
14 | 18 | use zenoh_core::zcondfeat; |
| 19 | +use zenoh_link::Link; |
| 20 | +use zenoh_protocol::{ |
| 21 | + core::{Bound, RegionName, WhatAmI, ZenohIdProto}, |
| 22 | + network::{NetworkBody, NetworkBodyMut, NetworkMessage, NetworkMessageMut}, |
| 23 | + transport::TransportSn, |
| 24 | +}; |
| 25 | +use zenoh_result::ZResult; |
| 26 | + |
| 27 | +use crate::{ |
| 28 | + unicast::{ |
| 29 | + authentication::TransportAuthId, |
| 30 | + link::LinkUnicastWithOpenAck, |
| 31 | + transport_unicast_inner::{AddLinkResult, TransportStatus, TransportUnicastTrait}, |
| 32 | + TransportConfigUnicast, TransportManagerBuilderUnicast, TransportUnicast, |
| 33 | + }, |
| 34 | + TransportManager, TransportPeerEventHandler, |
| 35 | +}; |
| 36 | + |
| 37 | +/// A minimal stub implementation of [`TransportUnicastTrait`] for use in routing tests. |
| 38 | +/// |
| 39 | +/// All methods panic with `unimplemented!()` except `get_zid`, `get_whatami`, and `schedule`. |
| 40 | +/// `schedule` converts each incoming [`NetworkMessageMut`] to an owned [`NetworkMessage`] and |
| 41 | +/// calls the `on_schedule` callback provided at construction time, allowing tests to observe |
| 42 | +/// every message that the routing layer sends to this face without a real network connection. |
| 43 | +pub struct MockTransportUnicastInner { |
| 44 | + zid: ZenohIdProto, |
| 45 | + whatami: WhatAmI, |
| 46 | + on_schedule: Arc<dyn Fn(NetworkMessage) + Send + Sync>, |
| 47 | +} |
| 48 | + |
| 49 | +#[async_trait] |
| 50 | +impl TransportUnicastTrait for MockTransportUnicastInner { |
| 51 | + fn set_callback(&self, _callback: Arc<dyn TransportPeerEventHandler>) {} |
| 52 | + |
| 53 | + async fn get_status(&self) -> AsyncMutexGuard<'_, TransportStatus> { |
| 54 | + unimplemented!("MockTransportUnicastInner::get_status") |
| 55 | + } |
| 56 | + |
| 57 | + fn get_zid(&self) -> ZenohIdProto { |
| 58 | + self.zid |
| 59 | + } |
| 60 | + |
| 61 | + fn get_whatami(&self) -> WhatAmI { |
| 62 | + self.whatami |
| 63 | + } |
| 64 | + |
| 65 | + fn get_callback(&self) -> Option<Arc<dyn TransportPeerEventHandler>> { |
| 66 | + unimplemented!("MockTransportUnicastInner::get_callback") |
| 67 | + } |
| 68 | + |
| 69 | + fn get_links(&self) -> Vec<Link> { |
| 70 | + vec![] |
| 71 | + } |
15 | 72 |
|
16 | | -use crate::{unicast::TransportManagerBuilderUnicast, TransportManager}; |
| 73 | + fn get_auth_ids(&self) -> TransportAuthId { |
| 74 | + unimplemented!("MockTransportUnicastInner::get_auth_ids") |
| 75 | + } |
| 76 | + |
| 77 | + #[cfg(feature = "shared-memory")] |
| 78 | + fn is_shm(&self) -> bool { |
| 79 | + false |
| 80 | + } |
| 81 | + |
| 82 | + fn is_qos(&self) -> bool { |
| 83 | + false |
| 84 | + } |
| 85 | + |
| 86 | + fn region_name(&self) -> Option<RegionName> { |
| 87 | + None |
| 88 | + } |
| 89 | + |
| 90 | + fn get_bound(&self) -> Option<Bound> { |
| 91 | + None |
| 92 | + } |
| 93 | + |
| 94 | + fn get_config(&self) -> &TransportConfigUnicast { |
| 95 | + unimplemented!("MockTransportUnicastInner::get_config") |
| 96 | + } |
| 97 | + |
| 98 | + #[cfg(feature = "stats")] |
| 99 | + fn stats(&self) -> zenoh_stats::TransportStats { |
| 100 | + unimplemented!("MockTransportUnicastInner::stats") |
| 101 | + } |
| 102 | + |
| 103 | + async fn add_link( |
| 104 | + &self, |
| 105 | + _link: LinkUnicastWithOpenAck, |
| 106 | + _other_initial_sn: TransportSn, |
| 107 | + _other_lease: Duration, |
| 108 | + ) -> AddLinkResult { |
| 109 | + unimplemented!("MockTransportUnicastInner::add_link") |
| 110 | + } |
| 111 | + |
| 112 | + fn schedule(&self, msg: NetworkMessageMut) -> ZResult<bool> { |
| 113 | + let body = match msg.body { |
| 114 | + NetworkBodyMut::Push(p) => NetworkBody::Push(p.clone()), |
| 115 | + NetworkBodyMut::Request(r) => NetworkBody::Request(r.clone()), |
| 116 | + NetworkBodyMut::Response(r) => NetworkBody::Response(r.clone()), |
| 117 | + NetworkBodyMut::ResponseFinal(r) => NetworkBody::ResponseFinal(r.clone()), |
| 118 | + NetworkBodyMut::Interest(i) => NetworkBody::Interest(i.clone()), |
| 119 | + NetworkBodyMut::Declare(d) => NetworkBody::Declare(d.clone()), |
| 120 | + NetworkBodyMut::OAM(o) => NetworkBody::OAM(o.clone()), |
| 121 | + }; |
| 122 | + (self.on_schedule)(NetworkMessage { |
| 123 | + body, |
| 124 | + reliability: msg.reliability, |
| 125 | + }); |
| 126 | + Ok(true) |
| 127 | + } |
| 128 | + |
| 129 | + async fn close(&self, _reason: u8) -> ZResult<()> { |
| 130 | + Ok(()) |
| 131 | + } |
| 132 | + |
| 133 | + fn add_debug_fields<'a, 'b: 'a, 'c>( |
| 134 | + &self, |
| 135 | + s: &'c mut DebugStruct<'a, 'b>, |
| 136 | + ) -> &'c mut DebugStruct<'a, 'b> { |
| 137 | + s |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +/// Creates a [`TransportUnicast`] backed by a [`MockTransportUnicastInner`]. |
| 142 | +/// |
| 143 | +/// Every message that the routing layer sends to this face is converted to an owned |
| 144 | +/// [`NetworkMessage`] and forwarded to `on_schedule`. The caller can use this callback to |
| 145 | +/// record or assert on outgoing messages without a real network connection. |
| 146 | +/// |
| 147 | +/// The caller must keep the returned `Arc<MockTransportUnicastInner>` alive for the duration of |
| 148 | +/// any operation that uses the `TransportUnicast`, because `TransportUnicast` only holds a `Weak` |
| 149 | +/// reference. |
| 150 | +pub fn mock_transport_unicast( |
| 151 | + zid: ZenohIdProto, |
| 152 | + whatami: WhatAmI, |
| 153 | + on_schedule: Arc<dyn Fn(NetworkMessage) + Send + Sync>, |
| 154 | +) -> (TransportUnicast, Arc<MockTransportUnicastInner>) { |
| 155 | + let inner = Arc::new(MockTransportUnicastInner { |
| 156 | + zid, |
| 157 | + whatami, |
| 158 | + on_schedule, |
| 159 | + }); |
| 160 | + let erased: Arc<dyn TransportUnicastTrait> = inner.clone(); |
| 161 | + let transport = TransportUnicast::from(&erased); |
| 162 | + (transport, inner) |
| 163 | +} |
17 | 164 |
|
18 | 165 | pub fn make_transport_manager_builder( |
19 | 166 | #[cfg(feature = "transport_multilink")] max_links: usize, |
|
0 commit comments