|
| 1 | +package io.quarkus.mailer.runtime; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 5 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 6 | + |
| 7 | +import java.time.Duration; |
| 8 | + |
| 9 | +import org.junit.jupiter.api.AfterEach; |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import io.quarkus.mailer.Mail; |
| 14 | +import io.quarkus.mailer.Mailer; |
| 15 | +import io.quarkus.mailer.reactive.ReactiveMailer; |
| 16 | +import io.smallrye.mutiny.Uni; |
| 17 | +import io.vertx.mutiny.core.Vertx; |
| 18 | + |
| 19 | +class BlockingMailerImplTest { |
| 20 | + |
| 21 | + private Vertx vertx; |
| 22 | + |
| 23 | + @BeforeEach |
| 24 | + void setup() { |
| 25 | + vertx = Vertx.vertx(); |
| 26 | + } |
| 27 | + |
| 28 | + @AfterEach |
| 29 | + void tearDown() { |
| 30 | + if (vertx != null) { |
| 31 | + vertx.close().await().indefinitely(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + void testTimeoutThrowsException() { |
| 37 | + ReactiveMailer slowMailer = new ReactiveMailer() { |
| 38 | + @Override |
| 39 | + public Uni<Void> send(Mail... mails) { |
| 40 | + return Uni.createFrom().emitter(emitter -> { |
| 41 | + // Never complete - simulates hanging connection |
| 42 | + }); |
| 43 | + } |
| 44 | + }; |
| 45 | + Mailer blockingMailer = new BlockingMailerImpl(slowMailer, Duration.ofSeconds(1)); |
| 46 | + Mail mail = Mail. withText( "[email protected]", "Subject", "Body"); |
| 47 | + |
| 48 | + assertThatThrownBy(() -> blockingMailer.send(mail)) |
| 49 | + .isInstanceOf(io.smallrye.mutiny.TimeoutException.class); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void testTimeoutZeroWaitsIndefinitely() throws InterruptedException { |
| 54 | + ReactiveMailer delayedMailer = new ReactiveMailer() { |
| 55 | + @Override |
| 56 | + public Uni<Void> send(Mail... mails) { |
| 57 | + return Uni.createFrom().voidItem() |
| 58 | + .onItem().delayIt().by(Duration.ofMillis(500)); |
| 59 | + } |
| 60 | + }; |
| 61 | + Mailer blockingMailer = new BlockingMailerImpl(delayedMailer, Duration.ZERO); |
| 62 | + Mail mail = Mail. withText( "[email protected]", "Subject", "Body"); |
| 63 | + |
| 64 | + // Should complete successfully even though it takes 500ms |
| 65 | + // because zero timeout means wait indefinitely |
| 66 | + long startTime = System.currentTimeMillis(); |
| 67 | + blockingMailer.send(mail); |
| 68 | + long duration = System.currentTimeMillis() - startTime; |
| 69 | + |
| 70 | + assertThat(duration).isGreaterThanOrEqualTo(500); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void testTimeoutNullWaitsIndefinitely() { |
| 75 | + ReactiveMailer delayedMailer = new ReactiveMailer() { |
| 76 | + @Override |
| 77 | + public Uni<Void> send(Mail... mails) { |
| 78 | + return Uni.createFrom().voidItem() |
| 79 | + .onItem().delayIt().by(Duration.ofMillis(500)); |
| 80 | + } |
| 81 | + }; |
| 82 | + Mailer blockingMailer = new BlockingMailerImpl(delayedMailer, null); |
| 83 | + Mail mail = Mail. withText( "[email protected]", "Subject", "Body"); |
| 84 | + |
| 85 | + // Should complete successfully even though it takes 500ms |
| 86 | + // because null timeout means wait indefinitely |
| 87 | + long startTime = System.currentTimeMillis(); |
| 88 | + blockingMailer.send(mail); |
| 89 | + long duration = System.currentTimeMillis() - startTime; |
| 90 | + |
| 91 | + assertThat(duration).isGreaterThanOrEqualTo(500); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void testSuccessfulSendWithinTimeout() { |
| 96 | + ReactiveMailer fastMailer = new ReactiveMailer() { |
| 97 | + @Override |
| 98 | + public Uni<Void> send(Mail... mails) { |
| 99 | + return Uni.createFrom().voidItem(); |
| 100 | + } |
| 101 | + }; |
| 102 | + Mailer blockingMailer = new BlockingMailerImpl(fastMailer, Duration.ofSeconds(10)); |
| 103 | + Mail mail = Mail. withText( "[email protected]", "Subject", "Body"); |
| 104 | + |
| 105 | + long startTime = System.currentTimeMillis(); |
| 106 | + assertThatCode(() -> blockingMailer.send(mail)).doesNotThrowAnyException(); |
| 107 | + long duration = System.currentTimeMillis() - startTime; |
| 108 | + assertThat(duration).isLessThan(1000); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + void testExceptionPropagation() { |
| 113 | + ReactiveMailer failingMailer = new ReactiveMailer() { |
| 114 | + @Override |
| 115 | + public Uni<Void> send(Mail... mails) { |
| 116 | + return Uni.createFrom().failure(new RuntimeException("SMTP error")); |
| 117 | + } |
| 118 | + }; |
| 119 | + Mailer blockingMailer = new BlockingMailerImpl(failingMailer, Duration.ofSeconds(10)); |
| 120 | + Mail mail = Mail. withText( "[email protected]", "Subject", "Body"); |
| 121 | + |
| 122 | + assertThatThrownBy(() -> blockingMailer.send(mail)) |
| 123 | + .isInstanceOf(RuntimeException.class) |
| 124 | + .hasMessage("SMTP error"); |
| 125 | + } |
| 126 | +} |
0 commit comments