diff --git a/NzbDrone.Core.Test/Framework/AutoMoq/TestBaseTests.cs b/NzbDrone.Core.Test/Framework/AutoMoq/TestBaseTests.cs new file mode 100644 index 000000000..94d67cda0 --- /dev/null +++ b/NzbDrone.Core.Test/Framework/AutoMoq/TestBaseTests.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using NLog; +using NUnit.Framework; + +namespace NzbDrone.Core.Test.Framework.AutoMoq +{ + [TestFixture] + class TestBaseTests : TestBase + { + private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); + + [Test] + public void Test_should_pass_when_no_exceptions_are_logged() + { + Logger.Info("Everything is fine and dandy!"); + } + + [Test] + public void Test_should_pass_when_errors_are_excpected() + { + Logger.Error("I knew this would happer"); + ExceptionVerification.ExcpectedErrors(1); + } + + [Test] + public void Test_should_pass_when_warns_are_excpected() + { + Logger.Warn("I knew this would happer"); + ExceptionVerification.ExcpectedWarns(1); + } + + [Test] + public void Test_should_pass_when_warns_are_ignored() + { + Logger.Warn("I knew this would happer"); + Logger.Warn("I knew this would happer"); + Logger.Warn("I knew this would happer"); + ExceptionVerification.IgnoreWarns(); + } + + [Test] + public void Test_should_pass_when_errors_are_ignored() + { + Logger.Error("I knew this would happer"); + Logger.Error("I knew this would happer"); + Logger.Error("I knew this would happer"); + ExceptionVerification.IgnoreErrors(); + } + + [Test] + public void Test_should_pass_when_exception_type_is_ignored() + { + Logger.ErrorException("bad exception", new WebException("Test")); + ExceptionVerification.MarkInconclusive(typeof(WebException)); + } + } +} diff --git a/NzbDrone.Core.Test/Framework/ExceptionVerification.cs b/NzbDrone.Core.Test/Framework/ExceptionVerification.cs index 6cbabaf2d..c30d31b1d 100644 --- a/NzbDrone.Core.Test/Framework/ExceptionVerification.cs +++ b/NzbDrone.Core.Test/Framework/ExceptionVerification.cs @@ -72,8 +72,22 @@ namespace NzbDrone.Core.Test.Framework Ignore(LogLevel.Error); } + internal static void MarkInconclusive(Type exception) + { + var inconclusiveLogs = _logs.Where(l => l.Exception.GetType() == exception).ToList(); + + if (inconclusiveLogs.Count != 0) + { + inconclusiveLogs.ForEach(c => _logs.Remove(c)); + Assert.Inconclusive(GetLogsString(inconclusiveLogs)); + + } + } + private static void Excpected(LogLevel level, int count) { + + var levelLogs = _logs.Where(l => l.Level == level).ToList(); if (levelLogs.Count != count) @@ -82,8 +96,7 @@ namespace NzbDrone.Core.Test.Framework 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" + message = "********************************************************************************************************************************\n\r" + message + "\n\r********************************************************************************************************************************"; diff --git a/NzbDrone.Core.Test/Framework/Fixtures.cs b/NzbDrone.Core.Test/Framework/Fixtures.cs index 4d16f35ba..30e514579 100644 --- a/NzbDrone.Core.Test/Framework/Fixtures.cs +++ b/NzbDrone.Core.Test/Framework/Fixtures.cs @@ -21,8 +21,7 @@ namespace NzbDrone.Core.Test { try { - LogManager.Configuration = - new XmlLoggingConfiguration(Path.Combine(CentralDispatch.AppPath, "log.config"), false); + LogManager.Configuration = new XmlLoggingConfiguration(Path.Combine(CentralDispatch.AppPath, "log.config"), false); LogManager.ThrowExceptions = true; var exceptionVerification = new ExceptionVerification(); @@ -35,7 +34,6 @@ namespace NzbDrone.Core.Test Console.WriteLine("Unable to configure logging. " + e); } - var filesToDelete = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sdf", SearchOption.AllDirectories); foreach (var file in filesToDelete) { @@ -45,6 +43,8 @@ namespace NzbDrone.Core.Test } catch { } } + + MockLib.CreateDataBaseTemplate(); } } } \ No newline at end of file diff --git a/NzbDrone.Core.Test/Framework/MockLib.cs b/NzbDrone.Core.Test/Framework/MockLib.cs index c220858f1..237372d9b 100644 --- a/NzbDrone.Core.Test/Framework/MockLib.cs +++ b/NzbDrone.Core.Test/Framework/MockLib.cs @@ -17,20 +17,19 @@ namespace NzbDrone.Core.Test.Framework /// internal static class MockLib { - public static string[] StandardSeries - { - get { return new[] { "c:\\tv\\the simpsons", "c:\\tv\\family guy", "c:\\tv\\southpark", "c:\\tv\\24" }; } - } + private const string DbTemplateName = "_dbtemplate.sdf"; public static IDatabase GetEmptyDatabase(bool enableLogging = false, string fileName = "") { - Console.WriteLine("Creating an empty PetaPoco database"); + Console.WriteLine("Cloning database from template."); if (String.IsNullOrWhiteSpace(fileName)) { fileName = Guid.NewGuid() + ".sdf"; } + File.Copy(DbTemplateName, fileName); + var connectionString = Connection.GetConnectionString(fileName); var database = Connection.GetPetaPocoDb(connectionString); @@ -38,7 +37,15 @@ namespace NzbDrone.Core.Test.Framework return database; } - public static Series GetFakeSeries(int id, string title) + public static void CreateDataBaseTemplate() + { + Console.WriteLine("Creating an empty PetaPoco database"); + var connectionString = Connection.GetConnectionString(DbTemplateName); + var database = Connection.GetPetaPocoDb(connectionString); + database.Dispose(); + } + + public static Series GetFakeSeries(int id, string title) { return Builder.CreateNew() .With(c => c.SeriesId = id) diff --git a/NzbDrone.Core.Test/IndexerTests.cs b/NzbDrone.Core.Test/IndexerTests.cs index 6d85b1d28..40ab07a1a 100644 --- a/NzbDrone.Core.Test/IndexerTests.cs +++ b/NzbDrone.Core.Test/IndexerTests.cs @@ -66,7 +66,7 @@ namespace NzbDrone.Core.Test .Setup(h => h.DownloadStream(It.IsAny(), It.IsAny())) .Returns(File.OpenRead(".\\Files\\Rss\\newbin_none_english.xml")); - + var newzbin = mocker.Resolve(); var parseResults = newzbin.FetchRss(); @@ -204,6 +204,8 @@ namespace NzbDrone.Core.Test var result = mocker.Resolve().FetchEpisode(title, season, episode); + ExceptionVerification.MarkInconclusive(typeof(WebException)); + result.Should().NotBeEmpty(); result.Should().OnlyContain(r => r.CleanTitle == Parser.NormalizeTitle(title)); result.Should().OnlyContain(r => r.SeasonNumber == season); @@ -229,6 +231,8 @@ namespace NzbDrone.Core.Test var result = mocker.Resolve().FetchEpisode(title, season, episode); + ExceptionVerification.MarkInconclusive(typeof(WebException)); + result.Should().NotBeEmpty(); result.Should().OnlyContain(r => r.CleanTitle == Parser.NormalizeTitle(title)); result.Should().OnlyContain(r => r.SeasonNumber == season); @@ -252,6 +256,8 @@ namespace NzbDrone.Core.Test var result = mocker.Resolve().FetchEpisode("Simpsons", 21, 23); + ExceptionVerification.MarkInconclusive(typeof(WebException)); + result.Should().NotBeEmpty(); result.Should().OnlyContain(r => r.CleanTitle == "simpsons"); result.Should().OnlyContain(r => r.SeasonNumber == 21); @@ -275,11 +281,12 @@ namespace NzbDrone.Core.Test var result = mocker.Resolve().FetchEpisode("Blue Bloods", 1, 19); + ExceptionVerification.MarkInconclusive(typeof(WebException)); + result.Should().NotBeEmpty(); result.Should().OnlyContain(r => r.CleanTitle == "bluebloods"); result.Should().OnlyContain(r => r.SeasonNumber == 1); result.Should().OnlyContain(r => r.EpisodeNumbers.Contains(19)); - } [Test] @@ -299,6 +306,8 @@ namespace NzbDrone.Core.Test var result = mocker.Resolve().FetchEpisode("Blue Bloods", 1, 19); + ExceptionVerification.MarkInconclusive(typeof(WebException)); + result.Should().NotBeEmpty(); result.Should().OnlyContain(r => r.CleanTitle == "bluebloods"); result.Should().OnlyContain(r => r.SeasonNumber == 1); diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 86e4fcfdd..878721f38 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -91,6 +91,7 @@ + diff --git a/NzbDrone.Core.Test/dbBenchmark.cs b/NzbDrone.Core.Test/dbBenchmark.cs index 3b8b3800b..80a35b06c 100644 --- a/NzbDrone.Core.Test/dbBenchmark.cs +++ b/NzbDrone.Core.Test/dbBenchmark.cs @@ -15,6 +15,7 @@ using PetaPoco; namespace NzbDrone.Core.Test { [TestFixture] + [Category("Benchmark")] // ReSharper disable InconsistentNaming public class DbBenchmark : TestBase { diff --git a/NzbDrone.Core/Datastore/MigrationsHelper.cs b/NzbDrone.Core/Datastore/MigrationsHelper.cs index b5b74ab8e..0267ce58b 100644 --- a/NzbDrone.Core/Datastore/MigrationsHelper.cs +++ b/NzbDrone.Core/Datastore/MigrationsHelper.cs @@ -20,7 +20,7 @@ namespace NzbDrone.Core.Datastore EnsureDatabase(connetionString); - Logger.Info("Preparing to run database migration"); + Logger.Trace("Preparing to run database migration"); try { diff --git a/NzbDrone.Core/Instrumentation/ExceptioneerTarget.cs b/NzbDrone.Core/Instrumentation/ExceptioneerTarget.cs index 2cd8caf76..3805eda37 100644 --- a/NzbDrone.Core/Instrumentation/ExceptioneerTarget.cs +++ b/NzbDrone.Core/Instrumentation/ExceptioneerTarget.cs @@ -1,5 +1,4 @@ -using System; -using System.Diagnostics; +using System.Diagnostics; using Exceptioneer.WindowsFormsClient; using NLog; using NLog.Targets; @@ -12,17 +11,18 @@ namespace NzbDrone.Core.Instrumentation protected override void Write(LogEventInfo logEvent) { - if (!Debugger.IsAttached) - { - Logger.Trace("Sending Exception to Exceptioneer"); + if (logEvent == null || logEvent.Exception == null) return; + if (Debugger.IsAttached || Process.GetCurrentProcess().ProcessName.Contains("JetBrains")) return; + + Logger.Trace("Sending Exception to Exceptioneer. {0}", Process.GetCurrentProcess().ProcessName); + + new Client + { + ApiKey = "43BBF60A-EB2A-4C1C-B09E-422ADF637265", + ApplicationName = "NZBDrone", + CurrentException = logEvent.Exception + }.Submit(); - new Client - { - ApiKey = "43BBF60A-EB2A-4C1C-B09E-422ADF637265", - ApplicationName = "NZBDrone", - CurrentException = logEvent.Exception - }.Submit(); - } } } } \ No newline at end of file diff --git a/NzbDrone.Core/Instrumentation/LogConfiguration.cs b/NzbDrone.Core/Instrumentation/LogConfiguration.cs index 4e93694c2..ec398ed5f 100644 --- a/NzbDrone.Core/Instrumentation/LogConfiguration.cs +++ b/NzbDrone.Core/Instrumentation/LogConfiguration.cs @@ -23,8 +23,8 @@ namespace NzbDrone.Core.Instrumentation public static void StartDbLogging() { - -#if RELEASE +#if DEBUG +#else var exTarget = new ExceptioneerTarget(); LogManager.Configuration.AddTarget("Exceptioneer", exTarget); LogManager.Configuration.LoggingRules.Add(new LoggingRule("*", NLog.LogLevel.Error, exTarget)); diff --git a/NzbDrone/AppMain.cs b/NzbDrone/AppMain.cs index 801ffae68..f794f9093 100644 --- a/NzbDrone/AppMain.cs +++ b/NzbDrone/AppMain.cs @@ -1,16 +1,12 @@ using System; using System.Reflection; -using NLog; using Ninject; +using NzbDrone.Providers; namespace NzbDrone { public static class AppMain { - - - private static readonly Logger Logger = LogManager.GetLogger("Host.Main"); - public static void Main(string[] args) { try @@ -21,8 +17,7 @@ namespace NzbDrone } catch (Exception e) { - Console.WriteLine(e.ToString()); - Logger.Fatal(e.ToString()); + MonitoringProvider.AppDomainException(e); } } diff --git a/NzbDrone/Providers/MonitoringProvider.cs b/NzbDrone/Providers/MonitoringProvider.cs index 55c80d0af..1ebd317e8 100644 --- a/NzbDrone/Providers/MonitoringProvider.cs +++ b/NzbDrone/Providers/MonitoringProvider.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.Runtime.Remoting; using System.Timers; +using Exceptioneer.WindowsFormsClient; using NLog; using Ninject; @@ -33,7 +34,7 @@ namespace NzbDrone.Providers public void Start() { - AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e)); + AppDomain.CurrentDomain.UnhandledException += ((s, e) => AppDomainException(e.ExceptionObject as Exception)); AppDomain.CurrentDomain.ProcessExit += ProgramExited; AppDomain.CurrentDomain.DomainUnload += ProgramExited; @@ -42,7 +43,7 @@ namespace NzbDrone.Providers prioCheckTimer.Elapsed += EnsurePriority; prioCheckTimer.Enabled = true; - _pingTimer = new Timer(60000) {AutoReset = true}; + _pingTimer = new Timer(60000) { AutoReset = true }; _pingTimer.Elapsed += (PingServer); _pingTimer.Start(); } @@ -101,12 +102,12 @@ namespace NzbDrone.Providers } - private static void AppDomainException(object excepion) + public static void AppDomainException(Exception excepion) { Console.WriteLine("EPIC FAIL: {0}", excepion); - Logger.Fatal("EPIC FAIL: {0}", excepion); -#if RELEASE +#if DEBUG +#else new Client { ApiKey = "43BBF60A-EB2A-4C1C-B09E-422ADF637265", @@ -114,6 +115,8 @@ namespace NzbDrone.Providers CurrentException = excepion as Exception }.Submit(); #endif + + Logger.Fatal("EPIC FAIL: {0}", excepion); } } } \ No newline at end of file