replaced server address with a constant

This commit is contained in:
Nickiel12 2021-12-17 12:55:00 -08:00
parent 6203112195
commit 28847873f6
2 changed files with 9 additions and 7 deletions

View file

@ -4,14 +4,16 @@ mod modules;
use std::net::{TcpListener, TcpStream, Shutdown};
const SERVER_ADDRESS: &str = "localhost:5000";
fn main() {
let listener = make_listener(String::from("localhost:5000"));
let listener = make_listener(SERVER_ADDRESS);
drop(listener);
}
fn make_listener(address: String) -> TcpListener {
fn make_listener(address: &str) -> TcpListener {
TcpListener::bind(address).unwrap()
}

View file

@ -1,4 +1,4 @@
use crate::make_listener;
use crate::{make_listener, SERVER_ADDRESS};
#[test]
@ -9,16 +9,16 @@ fn it_works() {
#[test]
fn can_make_socket_listener(){
let listener = make_listener(String::from("localhost:5000"));
let listener = make_listener(SERVER_ADDRESS);
drop(listener);
}
#[test]
fn create_and_connect_to_listener() {
let listener = make_listener(String::from("localhost:5000"));
let listener = make_listener(SERVER_ADDRESS);
let join_handle = std::thread::spawn(move || {
let _outgoing = std::net::TcpStream::connect("localhost:5000").unwrap();
let _outgoing = std::net::TcpStream::connect(SERVER_ADDRESS).unwrap();
});
join_handle.join().unwrap();
drop(listener);
@ -27,6 +27,6 @@ fn create_and_connect_to_listener() {
#[test]
#[should_panic]
fn panic_no_listener() {
let _outgoing = std::net::TcpStream::connect("localhost:5000").unwrap();
let _outgoing = std::net::TcpStream::connect(SERVER_ADDRESS).unwrap();
}