2013-04-27 05:03:34 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-02-23 23:09:44 +03:00
|
|
|
using Moq;
|
|
|
|
using NUnit.Framework;
|
2013-04-24 04:56:00 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-02-23 23:09:44 +03:00
|
|
|
using NzbDrone.Test.Common;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Test.EventingTests
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2013-04-27 05:03:34 +03:00
|
|
|
public class MessageAggregatorEventTests : TestBase
|
2013-02-23 23:09:44 +03:00
|
|
|
{
|
|
|
|
[Test]
|
|
|
|
public void should_publish_event_to_handlers()
|
|
|
|
{
|
2013-02-24 22:18:48 +03:00
|
|
|
var eventA = new EventA();
|
2013-04-27 05:03:34 +03:00
|
|
|
|
2013-02-24 22:18:48 +03:00
|
|
|
|
|
|
|
var intHandler = new Mock<IHandle<EventA>>();
|
2013-04-24 04:56:00 +03:00
|
|
|
var aggregator = new MessageAggregator(TestLogger, () => new List<IProcessMessage> { intHandler.Object });
|
2013-04-27 05:03:34 +03:00
|
|
|
aggregator.PublishEvent(eventA);
|
2013-02-23 23:09:44 +03:00
|
|
|
|
2013-02-24 22:18:48 +03:00
|
|
|
intHandler.Verify(c => c.Handle(eventA), Times.Once());
|
2013-02-23 23:09:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void should_publish_to_more_than_one_handler()
|
|
|
|
{
|
2013-02-24 22:18:48 +03:00
|
|
|
var eventA = new EventA();
|
|
|
|
|
|
|
|
var intHandler1 = new Mock<IHandle<EventA>>();
|
|
|
|
var intHandler2 = new Mock<IHandle<EventA>>();
|
2013-04-24 04:56:00 +03:00
|
|
|
var aggregator = new MessageAggregator(TestLogger, () => new List<IProcessMessage> { intHandler1.Object, intHandler2.Object });
|
2013-04-27 05:03:34 +03:00
|
|
|
aggregator.PublishEvent(eventA);
|
2013-02-23 23:09:44 +03:00
|
|
|
|
2013-02-24 22:18:48 +03:00
|
|
|
intHandler1.Verify(c => c.Handle(eventA), Times.Once());
|
|
|
|
intHandler2.Verify(c => c.Handle(eventA), Times.Once());
|
2013-02-23 23:09:44 +03:00
|
|
|
}
|
2013-02-23 23:34:51 +03:00
|
|
|
|
2013-02-23 23:09:44 +03:00
|
|
|
[Test]
|
|
|
|
public void should_not_publish_to_incompatible_handlers()
|
|
|
|
{
|
2013-02-24 22:18:48 +03:00
|
|
|
var eventA = new EventA();
|
|
|
|
|
|
|
|
var aHandler = new Mock<IHandle<EventA>>();
|
|
|
|
var bHandler = new Mock<IHandle<EventB>>();
|
2013-04-24 04:56:00 +03:00
|
|
|
var aggregator = new MessageAggregator(TestLogger, () => new List<IProcessMessage> { aHandler.Object, bHandler.Object });
|
2013-02-23 23:09:44 +03:00
|
|
|
|
2013-04-27 05:03:34 +03:00
|
|
|
aggregator.PublishEvent(eventA);
|
2013-02-23 23:09:44 +03:00
|
|
|
|
2013-02-24 22:18:48 +03:00
|
|
|
aHandler.Verify(c => c.Handle(eventA), Times.Once());
|
|
|
|
bHandler.Verify(c => c.Handle(It.IsAny<EventB>()), Times.Never());
|
2013-02-23 23:09:44 +03:00
|
|
|
}
|
|
|
|
|
2013-04-27 05:03:34 +03:00
|
|
|
|
|
|
|
[Test]
|
|
|
|
public void broken_handler_should_not_effect_others_handler()
|
|
|
|
{
|
|
|
|
var eventA = new EventA();
|
|
|
|
|
|
|
|
var intHandler1 = new Mock<IHandle<EventA>>();
|
|
|
|
var intHandler2 = new Mock<IHandle<EventA>>();
|
|
|
|
var intHandler3 = new Mock<IHandle<EventA>>();
|
|
|
|
|
|
|
|
intHandler2.Setup(c => c.Handle(It.IsAny<EventA>()))
|
|
|
|
.Throws(new NotImplementedException());
|
|
|
|
|
|
|
|
var aggregator = new MessageAggregator(TestLogger, () => new List<IProcessMessage> { intHandler1.Object, intHandler2.Object , intHandler3.Object});
|
|
|
|
aggregator.PublishEvent(eventA);
|
|
|
|
|
|
|
|
intHandler1.Verify(c => c.Handle(eventA), Times.Once());
|
|
|
|
intHandler3.Verify(c => c.Handle(eventA), Times.Once());
|
|
|
|
|
|
|
|
|
|
|
|
ExceptionVerification.ExpectedErrors(1);
|
|
|
|
}
|
|
|
|
|
2013-02-23 23:09:44 +03:00
|
|
|
}
|
|
|
|
|
2013-02-24 22:18:48 +03:00
|
|
|
|
2013-04-27 05:03:34 +03:00
|
|
|
public class EventA : IEvent
|
2013-02-24 22:18:48 +03:00
|
|
|
{
|
2013-04-27 05:03:34 +03:00
|
|
|
|
2013-02-24 22:18:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class EventB : IEvent
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-23 23:09:44 +03:00
|
|
|
}
|