-
-
Notifications
You must be signed in to change notification settings - Fork 296
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: Use diffrent ports for tests #1093
base: master
Are you sure you want to change the base?
Changes from 12 commits
2918232
db412d2
a3de641
97806e4
dc7e7fb
f1246fa
f015238
392c113
4084902
319b23f
cd7d90f
32e0f8f
73c8084
ccdea40
043e8c3
2eca6e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,46 @@ | ||
use std::net::SocketAddr; | ||
|
||
use axum_test::{TestServer, TestServerConfig}; | ||
use tokio::net::TcpListener; | ||
|
||
use crate::{ | ||
app::{AppContext, Hooks}, | ||
boot::{self, BootResult}, | ||
config::Server, | ||
environment::Environment, | ||
Result, | ||
}; | ||
|
||
/// The port on which the test server will run. | ||
pub const TEST_PORT_SERVER: i32 = 5555; | ||
|
||
/// The hostname to which the test server binds. | ||
pub const TEST_BINDING_SERVER: &str = "localhost"; | ||
|
||
/// Constructs and returns the base URL used for the test server. | ||
#[allow(dead_code)] | ||
pub fn get_base_url() -> String { | ||
format!("http://{TEST_BINDING_SERVER}:{TEST_PORT_SERVER}/") | ||
} | ||
|
||
/// Constructs and returns the base URL used for the test server. | ||
pub fn get_base_url_port(port: i32) -> String { | ||
format!("http://{TEST_BINDING_SERVER}:{port}/") | ||
} | ||
|
||
/// Returns a unique port number. Usually increments by 1 starting from 59126 | ||
pub async fn get_available_port() -> i32 { | ||
let addr = format!("{}:0", TEST_BINDING_SERVER); | ||
let listener = TcpListener::bind(addr) | ||
.await | ||
.expect("Failed to bind to address"); | ||
let port = listener | ||
.local_addr() | ||
.expect("Failed to get local address") | ||
.port() as i32; | ||
port | ||
} | ||
|
||
/// Bootstraps test application with test environment hard coded. | ||
/// | ||
/// # Errors | ||
|
@@ -37,6 +69,40 @@ pub async fn boot_test<H: Hooks>() -> Result<BootResult> { | |
H::boot(boot::StartMode::ServerOnly, &Environment::Test, config).await | ||
} | ||
|
||
/// Bootstraps test application with test environment hard coded, | ||
/// and with a unique port. | ||
/// | ||
/// # Errors | ||
/// when could not bootstrap the test environment | ||
/// | ||
/// # Example | ||
/// | ||
/// The provided example demonstrates how to boot the test case with the | ||
/// application context, and a with a unique port. | ||
/// | ||
/// ```rust,ignore | ||
/// use myapp::app::App; | ||
/// use loco_rs::testing::prelude::*; | ||
/// use migration::Migrator; | ||
/// | ||
/// #[tokio::test] | ||
/// async fn test_create_user() { | ||
/// let port = get_available_port().await; | ||
/// let boot = boot_test_unique_port::<App, Migrator>(Some(port)).await; | ||
/// | ||
/// /// ..... | ||
/// assert!(false) | ||
/// } | ||
pub async fn boot_test_unique_port<H: Hooks>(port: Option<i32>) -> Result<BootResult> { | ||
let mut config = H::load_config(&Environment::Test).await?; | ||
config.server = Server { | ||
port: port.unwrap_or(TEST_PORT_SERVER), | ||
binding: TEST_BINDING_SERVER.to_string(), | ||
..config.server | ||
}; | ||
Comment on lines
+105
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is working correctly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this function used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the user would call boot_test_unique_port in their tests? |
||
H::boot(boot::StartMode::ServerOnly, &Environment::Test, config).await | ||
} | ||
|
||
#[allow(clippy::future_not_send)] | ||
/// Initiates a test request with a provided callback. | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this function still relevant? Also, what about the TEST_PORT_SERVER constant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not the relevant, get_base_url and TEST_PORT_SERVER could be remove if you want