Co-Authored-By: Taloth <Taloth@users.noreply.github.com> Signed-off-by: Robin Dadswell <robin@dadswell.email>pull/770/head
parent
634153b658
commit
649ecd94ea
@ -0,0 +1,81 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using FluentAssertions;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NzbDrone.Common.Cache;
|
||||||
|
using NzbDrone.Common.Messaging;
|
||||||
|
using NzbDrone.Core.HealthCheck;
|
||||||
|
using NzbDrone.Core.Test.Framework;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Test.HealthCheck
|
||||||
|
{
|
||||||
|
public class HealthCheckServiceFixture : CoreTest<HealthCheckService>
|
||||||
|
{
|
||||||
|
private FakeHealthCheck _healthCheck;
|
||||||
|
|
||||||
|
[SetUp]
|
||||||
|
public void SetUp()
|
||||||
|
{
|
||||||
|
_healthCheck = new FakeHealthCheck();
|
||||||
|
|
||||||
|
Mocker.SetConstant<IEnumerable<IProvideHealthCheck>>(new[] { _healthCheck });
|
||||||
|
Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>());
|
||||||
|
}
|
||||||
|
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +1,46 @@
|
|||||||
|
using System;
|
||||||
|
using NzbDrone.Common.Messaging;
|
||||||
|
|
||||||
namespace NzbDrone.Core.HealthCheck
|
namespace NzbDrone.Core.HealthCheck
|
||||||
{
|
{
|
||||||
public class EventDrivenHealthCheck
|
public interface IEventDrivenHealthCheck
|
||||||
|
{
|
||||||
|
IProvideHealthCheck HealthCheck { get; }
|
||||||
|
|
||||||
|
bool ShouldExecute(IEvent message, bool previouslyFailed);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class EventDrivenHealthCheck<TEvent> : IEventDrivenHealthCheck
|
||||||
{
|
{
|
||||||
public IProvideHealthCheck HealthCheck { get; set; }
|
public IProvideHealthCheck HealthCheck { get; set; }
|
||||||
public CheckOnCondition Condition { get; set; }
|
public CheckOnCondition Condition { get; set; }
|
||||||
|
public ICheckOnCondition<TEvent> EventFilter { get; set; }
|
||||||
|
|
||||||
public EventDrivenHealthCheck(IProvideHealthCheck healthCheck, CheckOnCondition condition)
|
public EventDrivenHealthCheck(IProvideHealthCheck healthCheck, CheckOnCondition condition)
|
||||||
{
|
{
|
||||||
HealthCheck = healthCheck;
|
HealthCheck = healthCheck;
|
||||||
Condition = condition;
|
Condition = condition;
|
||||||
|
EventFilter = healthCheck as ICheckOnCondition<TEvent>;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ShouldExecute(IEvent message, bool previouslyFailed)
|
||||||
|
{
|
||||||
|
if (Condition == CheckOnCondition.SuccessfulOnly && previouslyFailed)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Condition == CheckOnCondition.FailedOnly && !previouslyFailed)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (EventFilter != null && !EventFilter.ShouldCheckOnEvent((TEvent)message))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue