mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 22:16:40 +00:00
Timeout test.
This commit is contained in:
parent
a8f3a5ff47
commit
9ba07c4834
1 changed files with 32 additions and 1 deletions
|
@ -10,8 +10,12 @@ pub struct EmailClient {
|
||||||
|
|
||||||
impl EmailClient {
|
impl EmailClient {
|
||||||
pub fn new(base_url: String, sender: SubscriberEmail, authorization_token: String) -> Self {
|
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 {
|
Self {
|
||||||
http_client: Client::new(),
|
http_client,
|
||||||
base_url,
|
base_url,
|
||||||
sender,
|
sender,
|
||||||
authorization_token,
|
authorization_token,
|
||||||
|
@ -163,4 +167,31 @@ mod tests {
|
||||||
// Assert
|
// Assert
|
||||||
assert_err!(outcome);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue