Skip to content

Commit

Permalink
Improve code-coverage by testing batch of LogEvents (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot committed Aug 26, 2023
1 parent 35690ee commit bf8876b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/NLog.MailKit/MailTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public Layout Body
public Layout<bool> ReplaceNewlineWithBrTagInHtml { get; set; }

/// <summary>
/// Gets or sets a value indicating the SMTP client timeout.
/// Gets or sets a value indicating the SMTP client timeout (in milliseconds)
/// </summary>
/// <remarks>Warning: zero is not infinite waiting</remarks>
public Layout<int> Timeout { get; set; } = 10000;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NLog.Config;
Expand Down Expand Up @@ -65,6 +66,29 @@ public void SendMailWithHeader()
}, 1);
}

[Fact]
public void SendMailBatch()
{
var transactions = SendTest(() =>
{
var mailTarget = CreateNLogConfig();
var loggingConfiguration = new LoggingConfiguration();
loggingConfiguration.AddRuleForAllLevels(new NLog.Targets.Wrappers.BufferingTargetWrapper(mailTarget));
NLog.LogManager.Configuration = loggingConfiguration;
var logger = LogManager.GetLogger("logger1");
logger.Info("hello starting mail!");
}, 1);

var mailMessage = transactions.LastOrDefault()?.Message as SmtpServer.Mail.ITextMessage;
Assert.NotNull(mailMessage);
mailMessage.Content.Position = 0;
var mailBody = new StreamReader(mailMessage.Content).ReadToEnd();
Assert.NotNull(mailBody);
Assert.Contains("hello starting mail!", mailBody);
Assert.Contains("hello first mail!", mailBody);
}

private static IList<IMessageTransaction> SendTest(Action createConfig, int toCount)
{
var countdownEvent = new CountdownEvent(1);
Expand All @@ -75,15 +99,15 @@ private static IList<IMessageTransaction> SendTest(Action createConfig, int toCo
var cancellationToken = new CancellationTokenSource();

Task.Run(async () => await smtpServer.StartAsync(cancellationToken.Token), cancellationToken.Token);
{
createConfig();

var logger = LogManager.GetLogger("logger1");
logger.Info("hello first mail!");
createConfig();

countdownEvent.Wait(TimeSpan.FromSeconds(10));
}
var logger = LogManager.GetLogger("logger1");
logger.Info("hello first mail!");

LogManager.Flush();

countdownEvent.Wait(TimeSpan.FromSeconds(10));
cancellationToken.Cancel(false);

var receivedTransactions = messageStore.ReceivedTransactions;
Expand Down

0 comments on commit bf8876b

Please sign in to comment.