Keep test suite fast by tuning timeout for tests.

This commit is contained in:
Luca Palmieri 2021-09-01 21:36:33 +02:00
parent ed0f2fb593
commit 50a5814eb2
4 changed files with 22 additions and 7 deletions

View file

@ -11,4 +11,5 @@ database:
email_client: email_client:
base_url: "localhost" base_url: "localhost"
sender_email: "test@gmail.com" sender_email: "test@gmail.com"
authorization_token: "my-secret-token" authorization_token: "my-secret-token"
timeout_milliseconds: 10000

View file

@ -54,12 +54,17 @@ pub struct EmailClientSettings {
pub base_url: String, pub base_url: String,
pub sender_email: String, pub sender_email: String,
pub authorization_token: String, pub authorization_token: String,
pub timeout_milliseconds: u64,
} }
impl EmailClientSettings { impl EmailClientSettings {
pub fn sender(&self) -> Result<SubscriberEmail, String> { pub fn sender(&self) -> Result<SubscriberEmail, String> {
SubscriberEmail::parse(self.sender_email.clone()) SubscriberEmail::parse(self.sender_email.clone())
} }
pub fn timeout(&self) -> std::time::Duration {
std::time::Duration::from_millis(self.timeout_milliseconds)
}
} }
pub fn get_configuration() -> Result<Settings, config::ConfigError> { pub fn get_configuration() -> Result<Settings, config::ConfigError> {

View file

@ -9,11 +9,13 @@ pub struct EmailClient {
} }
impl EmailClient { impl EmailClient {
pub fn new(base_url: String, sender: SubscriberEmail, authorization_token: String) -> Self { pub fn new(
let http_client = Client::builder() base_url: String,
.timeout(std::time::Duration::from_secs(10)) sender: SubscriberEmail,
.build() authorization_token: String,
.unwrap(); timeout: std::time::Duration,
) -> Self {
let http_client = Client::builder().timeout(timeout).build().unwrap();
Self { Self {
http_client, http_client,
base_url, base_url,
@ -103,7 +105,12 @@ mod tests {
/// Get a test instance of `EmailClient`. /// Get a test instance of `EmailClient`.
fn email_client(base_url: String) -> EmailClient { fn email_client(base_url: String) -> EmailClient {
EmailClient::new(base_url, email(), Faker.fake()) EmailClient::new(
base_url,
email(),
Faker.fake(),
std::time::Duration::from_millis(200),
)
} }
#[tokio::test] #[tokio::test]

View file

@ -24,10 +24,12 @@ impl Application {
.email_client .email_client
.sender() .sender()
.expect("Invalid sender email address."); .expect("Invalid sender email address.");
let timeout = configuration.email_client.timeout();
let email_client = EmailClient::new( let email_client = EmailClient::new(
configuration.email_client.base_url, configuration.email_client.base_url,
sender_email, sender_email,
configuration.email_client.authorization_token, configuration.email_client.authorization_token,
timeout,
); );
let address = format!( let address = format!(