1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-19 17:32:38 +01:00

Mock debouncer for testing

(cherry picked from commit bb7b2808e2f70389157408809ec47cc8860b4938)
This commit is contained in:
Mark McDowall 2023-09-10 14:44:37 -07:00 committed by Bogdan
parent 7d85922f8d
commit dce637905a
5 changed files with 56 additions and 19 deletions

View File

@ -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);
}
}
}

View File

@ -4,11 +4,11 @@ namespace NzbDrone.Common.TPL
{ {
public class Debouncer public class Debouncer
{ {
private readonly Action _action; protected readonly Action _action;
private readonly System.Timers.Timer _timer; protected readonly System.Timers.Timer _timer;
private volatile int _paused; protected volatile int _paused;
private volatile bool _triggered; protected volatile bool _triggered;
public Debouncer(Action action, TimeSpan debounceDuration) public Debouncer(Action action, TimeSpan debounceDuration)
{ {
@ -27,7 +27,7 @@ private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
} }
} }
public void Execute() public virtual void Execute()
{ {
lock (_timer) lock (_timer)
{ {
@ -39,7 +39,7 @@ public void Execute()
} }
} }
public void Pause() public virtual void Pause()
{ {
lock (_timer) lock (_timer)
{ {
@ -48,7 +48,7 @@ public void Pause()
} }
} }
public void Resume() public virtual void Resume()
{ {
lock (_timer) lock (_timer)
{ {

View File

@ -1,10 +1,14 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using FluentAssertions; using FluentAssertions;
using Moq;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.Messaging; using NzbDrone.Common.Messaging;
using NzbDrone.Common.TPL;
using NzbDrone.Core.HealthCheck; using NzbDrone.Core.HealthCheck;
using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.HealthCheck namespace NzbDrone.Core.Test.HealthCheck
{ {
@ -19,10 +23,10 @@ public void SetUp()
Mocker.SetConstant<IEnumerable<IProvideHealthCheck>>(new[] { _healthCheck }); Mocker.SetConstant<IEnumerable<IProvideHealthCheck>>(new[] { _healthCheck });
Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>()); Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>());
Mocker.SetConstant<IDebounceManager>(Mocker.Resolve<DebounceManager>());
Mocker.GetMock<IServerSideNotificationService>() Mocker.GetMock<IDebounceManager>().Setup(s => s.CreateDebouncer(It.IsAny<Action>(), It.IsAny<TimeSpan>()))
.Setup(v => v.GetServerChecks()) .Returns<Action, TimeSpan>((a, t) => new MockDebouncer(a, t));
.Returns(new List<Core.HealthCheck.HealthCheck>());
} }
[Test] [Test]

View File

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Messaging; using NzbDrone.Common.Messaging;
@ -29,8 +28,6 @@ public class HealthCheckService : IHealthCheckService,
private readonly IProvideHealthCheck[] _scheduledHealthChecks; private readonly IProvideHealthCheck[] _scheduledHealthChecks;
private readonly Dictionary<Type, IEventDrivenHealthCheck[]> _eventDrivenHealthChecks; private readonly Dictionary<Type, IEventDrivenHealthCheck[]> _eventDrivenHealthChecks;
private readonly IEventAggregator _eventAggregator; private readonly IEventAggregator _eventAggregator;
private readonly ICacheManager _cacheManager;
private readonly Logger _logger;
private readonly ICached<HealthCheck> _healthCheckResults; private readonly ICached<HealthCheck> _healthCheckResults;
private readonly HashSet<IProvideHealthCheck> _pendingHealthChecks; private readonly HashSet<IProvideHealthCheck> _pendingHealthChecks;
@ -42,17 +39,15 @@ public class HealthCheckService : IHealthCheckService,
public HealthCheckService(IEnumerable<IProvideHealthCheck> healthChecks, public HealthCheckService(IEnumerable<IProvideHealthCheck> healthChecks,
IEventAggregator eventAggregator, IEventAggregator eventAggregator,
ICacheManager cacheManager, ICacheManager cacheManager,
IRuntimeInfo runtimeInfo, IDebounceManager debounceManager,
Logger logger) IRuntimeInfo runtimeInfo)
{ {
_healthChecks = healthChecks.ToArray(); _healthChecks = healthChecks.ToArray();
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_cacheManager = cacheManager;
_logger = logger;
_healthCheckResults = _cacheManager.GetCache<HealthCheck>(GetType()); _healthCheckResults = cacheManager.GetCache<HealthCheck>(GetType());
_pendingHealthChecks = new HashSet<IProvideHealthCheck>(); _pendingHealthChecks = new HashSet<IProvideHealthCheck>();
_debounce = new Debouncer(ProcessHealthChecks, TimeSpan.FromSeconds(5)); _debounce = debounceManager.CreateDebouncer(ProcessHealthChecks, TimeSpan.FromSeconds(5));
_startupHealthChecks = _healthChecks.Where(v => v.CheckOnStartup).ToArray(); _startupHealthChecks = _healthChecks.Where(v => v.CheckOnStartup).ToArray();
_scheduledHealthChecks = _healthChecks.Where(v => v.CheckOnSchedule).ToArray(); _scheduledHealthChecks = _healthChecks.Where(v => v.CheckOnSchedule).ToArray();

View File

@ -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();
}
}
}
}