Sonarr/src/NzbDrone.Core.Test/HealthCheck/HealthCheckServiceFixture.cs

90 lines
2.4 KiB
C#
Raw Normal View History

2023-09-10 21:44:37 +00:00
using System;
2019-10-20 19:51:12 +00:00
using System.Collections.Generic;
using FluentAssertions;
2023-09-10 21:44:37 +00:00
using Moq;
2019-10-20 19:51:12 +00:00
using NUnit.Framework;
using NzbDrone.Common.Cache;
using NzbDrone.Common.Messaging;
2023-09-10 21:44:37 +00:00
using NzbDrone.Common.TPL;
2019-10-20 19:51:12 +00:00
using NzbDrone.Core.HealthCheck;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Test.Common;
2019-10-20 19:51:12 +00:00
namespace NzbDrone.Core.Test.HealthCheck
{
public class HealthCheckServiceFixture : CoreTest<HealthCheckService>
{
2021-08-03 04:43:28 +00:00
private FakeHealthCheck _healthCheck;
2019-10-20 19:51:12 +00:00
[SetUp]
public void SetUp()
{
_healthCheck = new FakeHealthCheck();
Mocker.SetConstant<IEnumerable<IProvideHealthCheck>>(new[] { _healthCheck });
Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>());
2023-09-10 21:44:37 +00:00
Mocker.SetConstant<IDebounceManager>(Mocker.Resolve<DebounceManager>());
Mocker.GetMock<IDebounceManager>().Setup(s => s.CreateDebouncer(It.IsAny<Action>(), It.IsAny<TimeSpan>()))
.Returns<Action, TimeSpan>((a, t) => new MockDebouncer(a, t));
2019-10-20 19:51:12 +00:00
}
[Test]
public void should_not_execute_conditional()
{
Subject.HandleAsync(new FakeEvent());
_healthCheck.Executed.Should().BeFalse();
}
[Test]
public void should_execute_conditional()
{
Subject.HandleAsync(new FakeEvent() { ShouldExecute = true });
_healthCheck.Executed.Should().BeTrue();
}
[Test]
public void should_execute_unconditional()
{
Subject.HandleAsync(new FakeEvent2());
_healthCheck.Executed.Should().BeTrue();
}
}
public class FakeEvent : IEvent
{
public bool ShouldExecute { get; set; }
}
public class FakeEvent2 : IEvent
{
public bool ShouldExecute { get; set; }
}
[CheckOn(typeof(FakeEvent))]
[CheckOn(typeof(FakeEvent2))]
public class FakeHealthCheck : IProvideHealthCheck, ICheckOnCondition<FakeEvent>
{
public bool CheckOnStartup => false;
public bool CheckOnSchedule => false;
public bool Executed { get; set; }
public bool Checked { get; set; }
public Core.HealthCheck.HealthCheck Check()
{
Executed = true;
return new Core.HealthCheck.HealthCheck(GetType());
}
public bool ShouldCheckOnEvent(FakeEvent message)
{
return message.ShouldExecute;
}
}
}