From 9ba07c48341bb23b2539b29a7a90840e649c6cb2 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Sun, 17 Jan 2021 00:26:30 +0000 Subject: [PATCH] Timeout test. --- src/email_client.rs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/email_client.rs b/src/email_client.rs index 881524b..90c9f1c 100644 --- a/src/email_client.rs +++ b/src/email_client.rs @@ -10,8 +10,12 @@ 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(); Self { - http_client: Client::new(), + http_client, base_url, sender, authorization_token, @@ -163,4 +167,31 @@ mod tests { // Assert assert_err!(outcome); } + + #[tokio::test(threaded_scheduler)] + async fn send_email_times_out_if_the_server_takes_too_long() { + // Arrange + let mock_server = MockServer::start().await; + let sender = SubscriberEmail::parse(SafeEmail().fake()).unwrap(); + let email_client = EmailClient::new(mock_server.uri(), sender, Faker.fake()); + + let subscriber_email = SubscriberEmail::parse(SafeEmail().fake()).unwrap(); + let subject: String = Sentence(1..2).fake(); + let content: String = Paragraph(1..10).fake(); + + let response = ResponseTemplate::new(200).set_delay(std::time::Duration::from_secs(180)); + Mock::given(any()) + .respond_with(response) + .expect(1) + .mount(&mock_server) + .await; + + // Act + let outcome = email_client + .send_email(subscriber_email, &subject, &content, &content) + .await; + + // Assert + assert_err!(outcome); + } }