public bool Start() { //subscriber.Subscribe<TestMessageEntity>(msg => //{ // var json = msg.TryToJson(); // Console.WriteLine(json); // Console.WriteLine(); //}, () => //{ // Console.WriteLine("消息订阅失败"); //}, ExchangeTypeCode.Direct); easyNetQSubscriber.Subscribe <TestMessageEntity>(msg => { var json = msg.TryToJson(); Console.WriteLine(json); Console.WriteLine(); }, () => { Console.WriteLine("消息订阅失败"); }); return(true); }
public async void Subscribe_CorrectlyFormedEvent_ProcessingWithoutErrors_NoExceptionsShouldBeThrown() { //prepare IntegrationEvent <BookingCreated> integrationEvent = new IntegrationEvent <BookingCreated>() { CorrelationId = Guid.NewGuid(), EventCreationDate = DateTime.UtcNow, EventId = Guid.NewGuid(), EventType = _bookingEventType, Version = _version, Content = new BookingCreated { BookingName = _bookingName } }; var messagebytes = SerializeEvent(integrationEvent); EventEmulator eventEmulator = null; _mockBus.Setup(x => x.Consume(_mockQueue.Object, It.IsAny <Func <byte[], MessageProperties, MessageReceivedInfo, Task> >(), It.IsAny <Action <IConsumerConfiguration> >())) .Returns((IQueue queue, Func <byte[], MessageProperties, MessageReceivedInfo, Task> func, Action <IConsumerConfiguration> configure) => { eventEmulator = new EventEmulator(func); return(_consumerDisposable.Object); }); var mockSubscription = new Mock <ISubscription>(); mockSubscription.Setup(x => x.InvokeAsync(Encoding.UTF8.GetString(messagebytes))).Returns(Task.FromResult(true)); Dictionary <string, ISubscription> subscriptions = new Dictionary <string, ISubscription> { { "bookingcreated", mockSubscription.Object } }; _subsciptionSelector.Setup(x => x.Select(subscriptions, "bookingcreated")) .Returns((IDictionary <string, ISubscription> subs, string eventType) => mockSubscription.Object); _easyNetQSubscriber.Subscribe(subscriptions); //Act MessageProperties messageProperties = new MessageProperties(); await eventEmulator.Execute(messagebytes, messageProperties, null); //check eventEmulator.RaisedException.Should().BeNull(); mockSubscription.Verify(x => x.InvokeAsync(Encoding.UTF8.GetString(messagebytes)), Times.Once); mockSubscription.Verify(x => x.NotifyAboutDeadLetterAsync(It.IsAny <string>(), It.IsAny <Exception>()), Times.Never); var parsedMessage = DefaultIntergrationEventParser.Parse(messagebytes); var expectedEvent = parsedMessage.IntegrationEvent .AsSource() .OfLikeness <IntegrationEvent>() .CreateProxy(); _successHandlingStrategy.Verify(x => x.HandleSuccessAsync(expectedEvent)); }