Skip to content

Commit 8c289ef

Browse files
committed
add test case
1 parent 5a50486 commit 8c289ef

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

src/helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use tonic::Status;
22

33
use crate::pb::RegisterReq;
44

5-
pub fn validate_register_req(req: &RegisterReq) -> Option<Status> {
5+
pub(crate) fn validate_register_req(req: &RegisterReq) -> Option<Status> {
66
if req.tunnel.is_none() {
77
return Some(Status::invalid_argument("tunnel is required"));
88
}

src/server/control_server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type DataStream = Pin<Box<dyn Stream<Item = GrpcResult<TrafficToClient>> + Send>
3838
///
3939
/// We treat the control server is grpc server as well, in the concept,
4040
/// they are same thing.
41-
/// Although the grpc server provides a [`crate::protocol::pb::tunnel_service_server::TunnelService::data`],
41+
/// Although the grpc server provides a [`crate::pb::tunnel_service_server::TunnelService::data`],
4242
/// it's similar to the data server(a little), but in the `data` function body,
4343
/// the most of work is to forward the data from client to data server.
4444
/// We can understand this is a tunnel between the client and the data server.

src/socket.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,28 @@ impl<'a> AsyncWrite for AsyncUdpSocket<'a> {
8787
}
8888

8989
/// Dialer for connecting to a endpoint to get a async reader and a async writer.
90-
///
91-
/// # Examples
92-
///
93-
/// ```
94-
/// use std::net::{IpAddr, Ipv4Addr, SocketAddr};
95-
///
96-
/// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
97-
/// let dialer = Dialer::new(|addr| Box::pin(dial_tcp(addr)), socket);
98-
/// let (r, w) = dialer.dial().await.unwrap();
99-
/// ```
10090
#[derive(Debug)]
10191
pub(crate) struct Dialer {
10292
dial: DialFn,
10393
addr: SocketAddr,
10494
}
10595

10696
impl Dialer {
97+
/// Create a new dialer.
10798
pub(crate) fn new(dial: DialFn, addr: SocketAddr) -> Self {
10899
Self { dial, addr }
109100
}
110101

102+
/// Dial the endpoint.
103+
///
104+
/// # Returns
105+
///
106+
/// A future that resolves to a async reader and a async writer.
111107
pub(crate) fn dial(&self) -> Pin<Box<dyn std::future::Future<Output = DialResult> + Send>> {
112108
(self.dial)(self.addr)
113109
}
114110

111+
/// Expose the address of the dialer.
115112
pub(crate) fn addr(&self) -> SocketAddr {
116113
self.addr
117114
}
@@ -153,3 +150,40 @@ pub(crate) async fn dial_udp(local_endpoint: SocketAddr) -> DialResult {
153150
Box::new(AsyncUdpSocket::new(socket)),
154151
))
155152
}
153+
154+
#[cfg(test)]
155+
mod test {
156+
use super::*;
157+
use std::net::TcpListener as StdTcpListener;
158+
159+
#[tokio::test]
160+
async fn test_tcp_listener_and_dialer() {
161+
let port = free_port().unwrap();
162+
let listener = create_tcp_listener(port).await.unwrap();
163+
164+
let dialer = Dialer::new(
165+
|addr| Box::pin(dial_tcp(addr)),
166+
listener.local_addr().unwrap(),
167+
);
168+
dialer.dial().await.unwrap();
169+
}
170+
171+
#[tokio::test]
172+
async fn test_udp_socket_and_dialer() {
173+
let port = free_port().unwrap();
174+
let socket = create_udp_socket(port).await.unwrap();
175+
176+
let dialer = Dialer::new(
177+
|addr| Box::pin(dial_udp(addr)),
178+
socket.local_addr().unwrap(),
179+
);
180+
dialer.dial().await.unwrap();
181+
}
182+
183+
/// free_port returns a free port number for testing.
184+
fn free_port() -> std::io::Result<u16> {
185+
let listener = StdTcpListener::bind("127.0.0.1:0")?;
186+
let port = listener.local_addr()?.port();
187+
Ok(port)
188+
}
189+
}

tests/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![warn(missing_docs)]
2+
13
mod common;
24

35
use crate::common::free_port;

0 commit comments

Comments
 (0)