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:
base_url: "localhost"
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 sender_email: String,
pub authorization_token: String,
pub timeout_milliseconds: u64,
}
impl EmailClientSettings {
pub fn sender(&self) -> Result<SubscriberEmail, String> {
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> {

View file

@ -9,11 +9,13 @@ pub struct EmailClient {
}
impl EmailClient {
pub fn new(base_url: String, sender: SubscriberEmail, authorization_token: String) -> Self {
let http_client = Client::builder()
.timeout(std::time::Duration::from_secs(10))
.build()
.unwrap();
pub fn new(
base_url: String,
sender: SubscriberEmail,
authorization_token: String,
timeout: std::time::Duration,
) -> Self {
let http_client = Client::builder().timeout(timeout).build().unwrap();
Self {
http_client,
base_url,
@ -103,7 +105,12 @@ mod tests {
/// Get a test instance of `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]

View file

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