From d74ab12d9e822d38a4a8b7940b5740c3628270aa Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 22 Aug 2019 23:28:17 +0200 Subject: [PATCH] Flaky CommandExecutorFixture tests --- .../Commands/CommandExecutorFixture.cs | 2 +- .../ExceptionVerification.cs | 98 ++++++++++++++----- 2 files changed, 73 insertions(+), 27 deletions(-) diff --git a/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs b/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs index fb79b04eb..45e015950 100644 --- a/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs +++ b/src/NzbDrone.Core.Test/Messaging/Commands/CommandExecutorFixture.cs @@ -145,7 +145,7 @@ namespace NzbDrone.Core.Test.Messaging.Commands VerifyEventPublished(); - ExceptionVerification.ExpectedErrors(1); + ExceptionVerification.WaitForErrors(1, 500); } [Test] diff --git a/src/NzbDrone.Test.Common/ExceptionVerification.cs b/src/NzbDrone.Test.Common/ExceptionVerification.cs index b86220f7c..a60d1297b 100644 --- a/src/NzbDrone.Test.Common/ExceptionVerification.cs +++ b/src/NzbDrone.Test.Common/ExceptionVerification.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using NLog; using NLog.Targets; using NUnit.Framework; @@ -11,17 +12,27 @@ namespace NzbDrone.Test.Common { private static List _logs = new List(); + private static ManualResetEventSlim _waitEvent = new ManualResetEventSlim(); + protected override void Write(LogEventInfo logEvent) { - if (logEvent.Level >= LogLevel.Warn) + lock (_logs) { - _logs.Add(logEvent); + if (logEvent.Level >= LogLevel.Warn) + { + _logs.Add(logEvent); + _waitEvent.Set(); + } } } public static void Reset() { - _logs = new List(); + lock (_logs) + { + _logs.Clear(); + _waitEvent.Reset(); + } } public static void AssertNoUnexpectedLogs() @@ -47,6 +58,29 @@ namespace NzbDrone.Test.Common return errors; } + public static void WaitForErrors(int count, int msec) + { + while (true) + { + lock (_logs) + { + var levelLogs = _logs.Where(l => l.Level == LogLevel.Error).ToList(); + + if (levelLogs.Count >= count) + { + break; + } + + _waitEvent.Reset(); + } + + if (!_waitEvent.Wait(msec)) + break; + } + + Expected(LogLevel.Error, count); + } + public static void ExpectedErrors(int count) { Expected(LogLevel.Error, count); @@ -74,50 +108,62 @@ namespace NzbDrone.Test.Common public static void MarkInconclusive(Type exception) { - var inconclusiveLogs = _logs.Where(l => l.Exception != null && l.Exception.GetType() == exception).ToList(); - - if (inconclusiveLogs.Any()) + lock (_logs) { - inconclusiveLogs.ForEach(c => _logs.Remove(c)); - Assert.Inconclusive(GetLogsString(inconclusiveLogs)); + var inconclusiveLogs = _logs.Where(l => l.Exception != null && l.Exception.GetType() == exception).ToList(); + + if (inconclusiveLogs.Any()) + { + inconclusiveLogs.ForEach(c => _logs.Remove(c)); + Assert.Inconclusive(GetLogsString(inconclusiveLogs)); + } } } public static void MarkInconclusive(string text) { - var inconclusiveLogs = _logs.Where(l => l.FormattedMessage.ToLower().Contains(text.ToLower())).ToList(); - - if (inconclusiveLogs.Any()) + lock (_logs) { - inconclusiveLogs.ForEach(c => _logs.Remove(c)); - Assert.Inconclusive(GetLogsString(inconclusiveLogs)); + var inconclusiveLogs = _logs.Where(l => l.FormattedMessage.ToLower().Contains(text.ToLower())).ToList(); + + if (inconclusiveLogs.Any()) + { + inconclusiveLogs.ForEach(c => _logs.Remove(c)); + Assert.Inconclusive(GetLogsString(inconclusiveLogs)); + } } } private static void Expected(LogLevel level, int count) { - var levelLogs = _logs.Where(l => l.Level == level).ToList(); - - if (levelLogs.Count != count) + lock (_logs) { + var levelLogs = _logs.Where(l => l.Level == level).ToList(); + + if (levelLogs.Count != count) + { - var message = string.Format("{0} {1}(s) were expected but {2} were logged.\n\r{3}", - count, level, levelLogs.Count, GetLogsString(levelLogs)); + var message = string.Format("{0} {1}(s) were expected but {2} were logged.\n\r{3}", + count, level, levelLogs.Count, GetLogsString(levelLogs)); - message = "\n\r****************************************************************************************\n\r" - + message + - "\n\r****************************************************************************************"; + message = "\n\r****************************************************************************************\n\r" + + message + + "\n\r****************************************************************************************"; - Assert.Fail(message); - } + Assert.Fail(message); + } - levelLogs.ForEach(c => _logs.Remove(c)); + levelLogs.ForEach(c => _logs.Remove(c)); + } } private static void Ignore(LogLevel level) { - var levelLogs = _logs.Where(l => l.Level == level).ToList(); - levelLogs.ForEach(c => _logs.Remove(c)); + lock (_logs) + { + var levelLogs = _logs.Where(l => l.Level == level).ToList(); + levelLogs.ForEach(c => _logs.Remove(c)); + } } } } \ No newline at end of file