Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for Session::send_to #16

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ impl Session {
});
}

// TODO: write some tests
/// Both sends a packet to the Session's dest.
async fn send_to(&mut self, buf: &[u8]) -> Result<usize> {
debug!(
Expand All @@ -277,13 +276,18 @@ impl Session {

#[cfg(test)]
mod tests {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4};

use crate::config::{Config, ConnectionConfig, Local};
use crate::server::Server;
use crate::logger;
use crate::server::{Packet, Server, Session};
use std::str::from_utf8;
use tokio::net::UdpSocket;
use tokio::sync::mpsc::channel;
use tokio::sync::oneshot;

#[tokio::test]
async fn bind() {
async fn server_bind() {
let config = Config {
local: Local { port: 12345 },
connections: ConnectionConfig::Receiver {
Expand All @@ -296,4 +300,30 @@ mod tests {
let expected = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 12345);
assert_eq!(expected, addr)
}

#[tokio::test]
async fn session_send_to() {
let log = logger();

let addr = SocketAddrV4::new(Ipv4Addr::new(0, 0, 0, 0), 0);
let socket = UdpSocket::bind(addr).await.unwrap();
let local_addr = socket.local_addr().unwrap();
let (mut recv, _) = socket.split();
let (sender, _) = channel::<Packet>(1);

let (done, wait) = oneshot::channel::<()>();

tokio::spawn(async move {
let mut buf = vec![0; 1024];
let size = recv.recv(&mut buf).await.unwrap();
assert_eq!("hello", from_utf8(&buf[..size]).unwrap());
done.send(()).unwrap();
});

let mut session = Session::new(&log, local_addr, local_addr, sender)
.await
.unwrap();
session.send_to("hello".as_bytes()).await.unwrap();
wait.await.unwrap();
}
}