Skip to content

Commit c1d7c15

Browse files
authored
RichTextBoxTarget - Batch BeginInvoke to avoid mesage pump starvation (unit test) (#201)
1 parent f04597b commit c1d7c15

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

NLog.Windows.Forms.Tests/RichTextBoxTargetTests.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,49 @@ public void MaxLinesTest()
377377
}
378378
}
379379

380+
[Fact]
381+
[LogManagerReset]
382+
public void BatchWriteTest()
383+
{
384+
try
385+
{
386+
RichTextBoxTarget target = new RichTextBoxTarget()
387+
{
388+
ControlName = "Control1",
389+
Layout = "${message}",
390+
ToolWindow = false,
391+
AutoScroll = true,
392+
};
393+
394+
Assert.Equal(0, target.MaxLines);
395+
target.MaxLines = 7;
396+
397+
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
398+
399+
var form = target.TargetForm;
400+
NLog.LogManager.Setup().LoadConfiguration(cfg => cfg.ForLogger().WriteTo(target).WithBuffering());
401+
ThreadPool.QueueUserWorkItem(_ =>
402+
{
403+
for (int i = 0; i < 100; ++i)
404+
{
405+
logger.Info("Test {0}", i);
406+
}
407+
NLog.LogManager.Flush();
408+
manualResetEvent.Set();
409+
});
410+
manualResetEvent.WaitOne(10000);
411+
412+
Application.DoEvents();
413+
string expectedText = "Test 93\nTest 94\nTest 95\nTest 96\nTest 97\nTest 98\nTest 99\n";
414+
415+
Assert.Equal(expectedText, target.TargetRichTextBox.Text);
416+
}
417+
finally
418+
{
419+
LogManager.Configuration = null;
420+
}
421+
}
422+
380423
[Fact]
381424
[LogManagerReset]
382425
public void ColoringRuleDefaults()

0 commit comments

Comments
 (0)