2013-02-23 20:09:44 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Moq;
|
|
|
|
|
using NUnit.Framework;
|
2013-04-24 01:56:00 +00:00
|
|
|
|
using NzbDrone.Common.Messaging;
|
2013-02-23 20:09:44 +00:00
|
|
|
|
using NzbDrone.Test.Common;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Common.Test.EventingTests
|
|
|
|
|
{
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class ServiceNameFixture : TestBase
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_publish_event_to_handlers()
|
|
|
|
|
{
|
2013-02-24 19:18:48 +00:00
|
|
|
|
var eventA = new EventA();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var intHandler = new Mock<IHandle<EventA>>();
|
2013-04-24 01:56:00 +00:00
|
|
|
|
var aggregator = new MessageAggregator(TestLogger, () => new List<IProcessMessage> { intHandler.Object });
|
2013-02-24 19:18:48 +00:00
|
|
|
|
aggregator.Publish(eventA);
|
2013-02-23 20:09:44 +00:00
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
intHandler.Verify(c => c.Handle(eventA), Times.Once());
|
2013-02-23 20:09:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void should_publish_to_more_than_one_handler()
|
|
|
|
|
{
|
2013-02-24 19:18:48 +00:00
|
|
|
|
var eventA = new EventA();
|
|
|
|
|
|
|
|
|
|
var intHandler1 = new Mock<IHandle<EventA>>();
|
|
|
|
|
var intHandler2 = new Mock<IHandle<EventA>>();
|
2013-04-24 01:56:00 +00:00
|
|
|
|
var aggregator = new MessageAggregator(TestLogger, () => new List<IProcessMessage> { intHandler1.Object, intHandler2.Object });
|
2013-02-24 19:18:48 +00:00
|
|
|
|
aggregator.Publish(eventA);
|
2013-02-23 20:09:44 +00:00
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
intHandler1.Verify(c => c.Handle(eventA), Times.Once());
|
|
|
|
|
intHandler2.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()
|
|
|
|
|
{
|
2013-02-24 19:18:48 +00:00
|
|
|
|
var eventA = new EventA();
|
|
|
|
|
|
|
|
|
|
var aHandler = new Mock<IHandle<EventA>>();
|
|
|
|
|
var bHandler = new Mock<IHandle<EventB>>();
|
2013-04-24 01:56:00 +00:00
|
|
|
|
var aggregator = new MessageAggregator(TestLogger, () => new List<IProcessMessage> { aHandler.Object, bHandler.Object });
|
2013-02-23 20:09:44 +00:00
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
aggregator.Publish(eventA);
|
2013-02-23 20:09:44 +00:00
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
aHandler.Verify(c => c.Handle(eventA), Times.Once());
|
|
|
|
|
bHandler.Verify(c => c.Handle(It.IsAny<EventB>()), Times.Never());
|
2013-02-23 20:09:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 19:18:48 +00:00
|
|
|
|
|
|
|
|
|
public class EventA:IEvent
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class EventB : IEvent
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-23 20:09:44 +00:00
|
|
|
|
}
|