public void TestSource_WhenDifferentConfigsForBaseAndDerived_ThenReceiveFromBoth() { const string exchangeName = "exchange"; const string exchangeType = "type"; const string routingKey = "route"; const string derivedExchangeName = "derived-exchange"; const string derivedExchangeType = "derived-type"; const string derivedRoutingKey = "derived-route"; var derivedTestEvent = new DerivedTestEvent(); var derivedTestEvent2 = new DerivedTestEvent(); var sourceMock = new Mock <IEventSource>(MockBehavior.Strict); sourceMock.Setup(m => m.ReceiveEvents <DerivedTestEvent>()).Returns(Observable.Return(derivedTestEvent)); var derivedSourceMock = new Mock <IEventSource>(MockBehavior.Strict); derivedSourceMock.Setup(m => m.ReceiveEvents <DerivedTestEvent>()).Returns(Observable.Return(derivedTestEvent2)); var factoryMock = new Mock <IEventProcessorFactory>(MockBehavior.Strict); factoryMock.Setup(m => m.CreateSource(exchangeName, exchangeType, routingKey)).Returns(sourceMock.Object); factoryMock.Setup(m => m.CreateSource(derivedExchangeName, derivedExchangeType, derivedRoutingKey)).Returns(derivedSourceMock.Object); var exchangeConfiguration = new ExchangeConfiguration(); exchangeConfiguration.ConfigureEventSource(typeof(TestEvent), exchangeName, exchangeType, routingKey); exchangeConfiguration.ConfigureEventSource(typeof(DerivedTestEvent), derivedExchangeName, derivedExchangeType, derivedRoutingKey); var bus = new Bus(factoryMock.Object, exchangeConfiguration) as IEventSource; var receivedEvents = bus.ReceiveEvents <DerivedTestEvent>().ToArray().Wait(); Assert.Equal(2, receivedEvents.Length); Assert.True(receivedEvents.Contains(derivedTestEvent)); Assert.True(receivedEvents.Contains(derivedTestEvent2)); sourceMock.Verify(m => m.ReceiveEvents <DerivedTestEvent>(), Times.Once); derivedSourceMock.Verify(m => m.ReceiveEvents <DerivedTestEvent>(), Times.Once); }
public void TestSource_WhenConfiguredMultipleEventsWithEqualParameters_ThenCreateOnlyOneSource() { const string exchangeName = "exchange"; const string exchangeType = "type"; const string routingKey = "route"; var sourceMock = new Mock <IEventSource>(MockBehavior.Strict); sourceMock.Setup(m => m.ReceiveEvents <TestEvent>()).Returns(Observable.Empty <TestEvent>()); sourceMock.Setup(m => m.ReceiveEvents <AnotherTestEvent>()).Returns(Observable.Empty <AnotherTestEvent>()); var factoryMock = new Mock <IEventProcessorFactory>(MockBehavior.Strict); factoryMock.Setup(m => m.CreateSource(exchangeName, exchangeType, routingKey)).Returns(sourceMock.Object); var exchangeConfiguration = new ExchangeConfiguration(); exchangeConfiguration.ConfigureEventSource(typeof(TestEvent), exchangeName, exchangeType, routingKey); exchangeConfiguration.ConfigureEventSource(typeof(AnotherTestEvent), exchangeName, exchangeType, routingKey); var bus = new Bus(factoryMock.Object, exchangeConfiguration) as IEventSource; bus.ReceiveEvents <TestEvent>(); bus.ReceiveEvents <AnotherTestEvent>(); factoryMock.Verify(m => m.CreateSource(exchangeName, exchangeType, routingKey), Times.Once); }
public void TestSource_WhenConfiguredMultipleEvents_ThenChooseWisely() { const string exchangeName = "exchange"; const string exchangeType = "type"; const string routingKey = "route"; const string anotherExchangeName = "another-exchange"; const string anotherExchangeType = "another-type"; const string anotherRoutingKey = "another-route"; var @event = new TestEvent(); var anotherEvent = new AnotherTestEvent(); var sourceMock = new Mock <IEventSource>(MockBehavior.Strict); sourceMock.Setup(m => m.ReceiveEvents <TestEvent>()).Returns(Observable.Return(@event)); var anotherSourceMock = new Mock <IEventSource>(MockBehavior.Strict); anotherSourceMock.Setup(m => m.ReceiveEvents <AnotherTestEvent>()).Returns(Observable.Return(anotherEvent)); var factoryMock = new Mock <IEventProcessorFactory>(MockBehavior.Strict); factoryMock.Setup(m => m.CreateSource(exchangeName, exchangeType, routingKey)).Returns(sourceMock.Object); factoryMock.Setup(m => m.CreateSource(anotherExchangeName, anotherExchangeType, anotherRoutingKey)).Returns(anotherSourceMock.Object); var exchangeConfiguration = new ExchangeConfiguration(); exchangeConfiguration.ConfigureEventSource(typeof(TestEvent), exchangeName, exchangeType, routingKey); exchangeConfiguration.ConfigureEventSource(typeof(AnotherTestEvent), anotherExchangeName, anotherExchangeType, anotherRoutingKey); var bus = new Bus(factoryMock.Object, exchangeConfiguration) as IEventSource; var receivedEvent = bus.ReceiveEvents <TestEvent>().Wait(); var receivedAnotherEvent = bus.ReceiveEvents <AnotherTestEvent>().Wait(); Assert.Equal(receivedEvent, @event); Assert.Equal(receivedAnotherEvent, anotherEvent); sourceMock.Verify(m => m.ReceiveEvents <TestEvent>(), Times.Once); sourceMock.Verify(m => m.ReceiveEvents <AnotherTestEvent>(), Times.Never); anotherSourceMock.Verify(m => m.ReceiveEvents <TestEvent>(), Times.Never); anotherSourceMock.Verify(m => m.ReceiveEvents <AnotherTestEvent>(), Times.Once); }
public void TestSource_WhenConfiguredsDerivedEvent_ThenErrorWhileReceivingBase() { const string exchangeName = "exchange"; const string exchangeType = "type"; const string routingKey = "route"; var sourceMock = new Mock <IEventSource>(MockBehavior.Strict); var factoryMock = new Mock <IEventProcessorFactory>(MockBehavior.Strict); factoryMock.Setup(m => m.CreateSource(exchangeName, exchangeType, routingKey)).Returns(sourceMock.Object); var exchangeConfiguration = new ExchangeConfiguration(); exchangeConfiguration.ConfigureEventSource(typeof(DerivedTestEvent), exchangeName, exchangeType, routingKey); var bus = new Bus(factoryMock.Object, exchangeConfiguration) as IEventSource; Assert.Throws <InvalidOperationException>(() => bus.ReceiveEvents <TestEvent>().Wait()); }
public void TestSource_WhenEventConfigured_ThenReceive() { const string exchangeName = "exchange"; const string exchangeType = "type"; const string routingKey = "route"; var @event = new TestEvent(); var sourceMock = new Mock <IEventSource>(MockBehavior.Strict); sourceMock.Setup(m => m.ReceiveEvents <TestEvent>()).Returns(Observable.Return(@event)); var factoryMock = new Mock <IEventProcessorFactory>(MockBehavior.Strict); factoryMock.Setup(m => m.CreateSource(exchangeName, exchangeType, routingKey)).Returns(sourceMock.Object); var exchangeConfiguration = new ExchangeConfiguration(); exchangeConfiguration.ConfigureEventSource(typeof(TestEvent), exchangeName, exchangeType, routingKey); var bus = new Bus(factoryMock.Object, exchangeConfiguration) as IEventSource; var receivedEvent = bus.ReceiveEvents <TestEvent>().Wait(); sourceMock.Verify(m => m.ReceiveEvents <TestEvent>(), Times.Once); Assert.Equal(@event, receivedEvent); }