sonarr-repo-only/NzbDrone.Common.Test/MessagingTests/MessageAggregatorEventTests.cs

146 lines
4.0 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Threading;
2013-09-11 06:33:47 +00:00
using FluentAssertions;
2013-02-23 20:09:44 +00:00
using Moq;
using NUnit.Framework;
using NzbDrone.Common.Messaging;
2013-09-11 06:33:47 +00:00
using NzbDrone.Core.Messaging;
2013-02-23 20:09:44 +00:00
using NzbDrone.Test.Common;
2013-09-11 06:33:47 +00:00
namespace NzbDrone.Common.Test.MessagingTests
2013-02-23 20:09:44 +00:00
{
[TestFixture]
public class MessageAggregatorEventTests : TestBase<MessageAggregator>
2013-02-23 20:09:44 +00:00
{
private Mock<IHandle<EventA>> HandlerA1;
private Mock<IHandle<EventA>> HandlerA2;
private Mock<IHandle<EventB>> HandlerB1;
private Mock<IHandle<EventB>> HandlerB2;
private Mock<IHandleAsync<EventA>> AsyncHandlerA1;
[SetUp]
public void Setup()
2013-02-23 20:09:44 +00:00
{
HandlerA1 = new Mock<IHandle<EventA>>();
HandlerA2 = new Mock<IHandle<EventA>>();
HandlerB1 = new Mock<IHandle<EventB>>();
HandlerB2 = new Mock<IHandle<EventB>>();
AsyncHandlerA1 = new Mock<IHandleAsync<EventA>>();
Mocker.GetMock<IServiceFactory>()
.Setup(c => c.BuildAll<IHandle<EventA>>())
.Returns(new List<IHandle<EventA>> { HandlerA1.Object, HandlerA2.Object });
Mocker.GetMock<IServiceFactory>()
.Setup(c => c.BuildAll<IHandle<EventB>>())
.Returns(new List<IHandle<EventB>> { HandlerB1.Object, HandlerB2.Object });
2013-02-23 20:09:44 +00:00
}
[Test]
public void should_publish_event_to_handlers()
2013-02-23 20:09:44 +00:00
{
var eventA = new EventA();
Subject.PublishEvent(eventA);
2013-02-23 20:09:44 +00:00
HandlerA1.Verify(c => c.Handle(eventA), Times.Once());
HandlerA2.Verify(c => c.Handle(eventA), Times.Once());
2013-02-23 20:09:44 +00:00
}
2013-02-23 20:34:51 +00:00
2013-02-23 20:09:44 +00:00
[Test]
public void should_not_publish_to_incompatible_handlers()
{
var eventA = new EventA();
2013-02-23 20:09:44 +00:00
Subject.PublishEvent(eventA);
2013-02-23 20:09:44 +00:00
HandlerA1.Verify(c => c.Handle(eventA), Times.Once());
HandlerA2.Verify(c => c.Handle(eventA), Times.Once());
HandlerB1.Verify(c => c.Handle(It.IsAny<EventB>()), Times.Never());
HandlerB2.Verify(c => c.Handle(It.IsAny<EventB>()), Times.Never());
2013-02-23 20:09:44 +00:00
}
[Test]
public void broken_handler_should_not_effect_others_handler()
{
var eventA = new EventA();
HandlerA1.Setup(c => c.Handle(It.IsAny<EventA>()))
.Throws(new NotImplementedException());
Subject.PublishEvent(eventA);
HandlerA1.Verify(c => c.Handle(eventA), Times.Once());
HandlerA2.Verify(c => c.Handle(eventA), Times.Once());
ExceptionVerification.ExpectedErrors(1);
}
[Test]
public void should_queue_multiple_async_events()
{
var eventA = new EventA();
var handlers = new List<IHandleAsync<EventA>>
{
AsyncHandlerA1.Object,
AsyncHandlerA1.Object,
AsyncHandlerA1.Object,
AsyncHandlerA1.Object,
AsyncHandlerA1.Object,
AsyncHandlerA1.Object,
AsyncHandlerA1.Object,
};
Mocker.GetMock<IServiceFactory>()
.Setup(c => c.BuildAll<IHandle<EventA>>())
.Returns(new List<IHandle<EventA>>());
Mocker.GetMock<IServiceFactory>()
.Setup(c => c.BuildAll<IHandleAsync<EventA>>())
.Returns(handlers);
var counter = new ConcurrencyCounter(handlers.Count);
AsyncHandlerA1.Setup(c => c.HandleAsync(It.IsAny<EventA>()))
.Callback<EventA>(c =>
{
var id = counter.Start();
Thread.Sleep(1000);
counter.Stop(id);
});
Subject.PublishEvent(eventA);
counter.WaitForAllItems();
2013-09-11 06:33:47 +00:00
counter.MaxThreads.Should().Be(3);
}
2013-02-23 20:09:44 +00:00
}
public class EventA : IEvent
{
}
public class EventB : IEvent
{
}
2013-02-23 20:09:44 +00:00
}