From bb7b2808e2f70389157408809ec47cc8860b4938 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 10 Sep 2023 14:44:37 -0700 Subject: [PATCH] Mock debouncer for testing --- src/NzbDrone.Common/TPL/DebounceManager.cs | 17 +++++++++++++++ src/NzbDrone.Common/TPL/Debouncer.cs | 14 ++++++------- .../HealthCheck/HealthCheckServiceFixture.cs | 7 +++++++ .../HealthCheck/HealthCheckService.cs | 13 ++++-------- src/NzbDrone.Test.Common/MockDebouncer.cs | 21 +++++++++++++++++++ 5 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 src/NzbDrone.Common/TPL/DebounceManager.cs create mode 100644 src/NzbDrone.Test.Common/MockDebouncer.cs diff --git a/src/NzbDrone.Common/TPL/DebounceManager.cs b/src/NzbDrone.Common/TPL/DebounceManager.cs new file mode 100644 index 000000000..60803a3a9 --- /dev/null +++ b/src/NzbDrone.Common/TPL/DebounceManager.cs @@ -0,0 +1,17 @@ +using System; + +namespace NzbDrone.Common.TPL +{ + public interface IDebounceManager + { + Debouncer CreateDebouncer(Action action, TimeSpan debounceDuration); + } + + public class DebounceManager : IDebounceManager + { + public Debouncer CreateDebouncer(Action action, TimeSpan debounceDuration) + { + return new Debouncer(action, debounceDuration); + } + } +} diff --git a/src/NzbDrone.Common/TPL/Debouncer.cs b/src/NzbDrone.Common/TPL/Debouncer.cs index 0fa101525..7f8435961 100644 --- a/src/NzbDrone.Common/TPL/Debouncer.cs +++ b/src/NzbDrone.Common/TPL/Debouncer.cs @@ -4,11 +4,11 @@ namespace NzbDrone.Common.TPL { public class Debouncer { - private readonly Action _action; - private readonly System.Timers.Timer _timer; + protected readonly Action _action; + protected readonly System.Timers.Timer _timer; - private volatile int _paused; - private volatile bool _triggered; + protected volatile int _paused; + protected volatile bool _triggered; public Debouncer(Action action, TimeSpan debounceDuration) { @@ -27,7 +27,7 @@ namespace NzbDrone.Common.TPL } } - public void Execute() + public virtual void Execute() { lock (_timer) { @@ -39,7 +39,7 @@ namespace NzbDrone.Common.TPL } } - public void Pause() + public virtual void Pause() { lock (_timer) { @@ -48,7 +48,7 @@ namespace NzbDrone.Common.TPL } } - public void Resume() + public virtual void Resume() { lock (_timer) { diff --git a/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs index fcf9d58c8..8bb794ba1 100644 --- a/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs +++ b/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs @@ -1,8 +1,11 @@ +using System; using System.Collections.Generic; using FluentAssertions; +using Moq; using NUnit.Framework; using NzbDrone.Common.Cache; using NzbDrone.Common.Messaging; +using NzbDrone.Common.TPL; using NzbDrone.Core.HealthCheck; using NzbDrone.Core.Test.Framework; @@ -19,6 +22,10 @@ namespace NzbDrone.Core.Test.HealthCheck Mocker.SetConstant>(new[] { _healthCheck }); Mocker.SetConstant(Mocker.Resolve()); + Mocker.SetConstant(Mocker.Resolve()); + + Mocker.GetMock().Setup(s => s.CreateDebouncer(It.IsAny(), It.IsAny())) + .Returns((a, t) => new MockDebouncer(a, t)); } [Test] diff --git a/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs b/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs index 3c809f69d..31aa9c199 100644 --- a/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs +++ b/src/NzbDrone.Core/HealthCheck/HealthCheckService.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using NLog; using NzbDrone.Common.Cache; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Messaging; @@ -29,8 +28,6 @@ namespace NzbDrone.Core.HealthCheck private readonly IProvideHealthCheck[] _scheduledHealthChecks; private readonly Dictionary _eventDrivenHealthChecks; private readonly IEventAggregator _eventAggregator; - private readonly ICacheManager _cacheManager; - private readonly Logger _logger; private readonly ICached _healthCheckResults; private readonly HashSet _pendingHealthChecks; @@ -42,17 +39,15 @@ namespace NzbDrone.Core.HealthCheck public HealthCheckService(IEnumerable healthChecks, IEventAggregator eventAggregator, ICacheManager cacheManager, - IRuntimeInfo runtimeInfo, - Logger logger) + IDebounceManager debounceManager, + IRuntimeInfo runtimeInfo) { _healthChecks = healthChecks.ToArray(); _eventAggregator = eventAggregator; - _cacheManager = cacheManager; - _logger = logger; - _healthCheckResults = _cacheManager.GetCache(GetType()); + _healthCheckResults = cacheManager.GetCache(GetType()); _pendingHealthChecks = new HashSet(); - _debounce = new Debouncer(ProcessHealthChecks, TimeSpan.FromSeconds(5)); + _debounce = debounceManager.CreateDebouncer(ProcessHealthChecks, TimeSpan.FromSeconds(5)); _startupHealthChecks = _healthChecks.Where(v => v.CheckOnStartup).ToArray(); _scheduledHealthChecks = _healthChecks.Where(v => v.CheckOnSchedule).ToArray(); diff --git a/src/NzbDrone.Test.Common/MockDebouncer.cs b/src/NzbDrone.Test.Common/MockDebouncer.cs new file mode 100644 index 000000000..aa85135b2 --- /dev/null +++ b/src/NzbDrone.Test.Common/MockDebouncer.cs @@ -0,0 +1,21 @@ +using System; +using NzbDrone.Common.TPL; + +namespace NzbDrone.Test.Common +{ + public class MockDebouncer : Debouncer + { + public MockDebouncer(Action action, TimeSpan debounceDuration) + : base(action, debounceDuration) + { + } + + public override void Execute() + { + lock (_timer) + { + _action(); + } + } + } +}