Skip to content

Commit

Permalink
test: Send Papercut smtp message non blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
HofmeisterAn committed Sep 19, 2024
1 parent b426d49 commit 1375e9b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tests/Testcontainers.Papercut.Tests/PapercutContainerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@ public async Task ReceivesSentMessage()

Message[] messages;

using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(30));

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(_papercutContainer.GetBaseAddress());

using var smtpClient = new SmtpClient(_papercutContainer.Hostname, _papercutContainer.SmtpPort);

// When
smtpClient.Send("from@example.com", "to@example.com", subject, "A test message");
smtpClient.SendAsync("from@example.com", "to@example.com", subject, "A test message", null);

do
{
var messagesJson = await httpClient.GetStringAsync("/api/messages")
var messagesJson = await httpClient.GetStringAsync("/api/messages", timeoutCts.Token)
.ConfigureAwait(true);

var jsonDocument = JsonDocument.Parse(messagesJson);
messages = jsonDocument.RootElement.GetProperty("messages").Deserialize<Message[]>();
}
while (messages.Length == 0);
while (messages.Length == 0 && !timeoutCts.IsCancellationRequested);

// Then
Assert.NotEmpty(messages);
Expand Down
1 change: 1 addition & 0 deletions tests/Testcontainers.Papercut.Tests/Usings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
global using System.Net.Mail;
global using System.Text.Json;
global using System.Text.Json.Serialization;
global using System.Threading;
global using System.Threading.Tasks;
global using DotNet.Testcontainers.Commons;
global using Xunit;

0 comments on commit 1375e9b

Please sign in to comment.