Timeout test.

This commit is contained in:
LukeMathWalker 2021-01-17 00:26:30 +00:00
parent a8f3a5ff47
commit 9ba07c4834

View file

@ -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);
}
}