static void Main(string[] args) { int max = 50; CountdownEvent cd = new CountdownEvent(max); string endPoint = "tcp://localhost:5557"; System.Console.WriteLine("Publishing on {0}\n", endPoint); const string topic = "TestTopicxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; { var publisher = new MessagePublisher<IMessage>("tcp://localhost:5557", new ProtoBufMessageSerializer(), topic); for (int i = 0; i < max; i++) { publisher.PublishAsync(new Message1AndItIs32CharactersLongForSureDefinitionForSure() { Id = i }); Thread.Sleep(TimeSpan.FromSeconds(0.5)); System.Console.WriteLine("Published: {0}", i); } } System.Console.WriteLine("[finished - any key to continue]"); System.Console.ReadKey(); }
public void SerializesMessage() { Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>(); Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>(); Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>(); MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object); TestMessage message = new TestMessage(); messagePublisher.PublishAsync(message); mockMessageSerializer.Verify(ms => ms.Serialize(It.IsAny<Stream>(), It.Is<TestMessage>(it => Object.ReferenceEquals(it, message))), Times.Once()); }
public async void TestSendingAndReceivingBytesOverLocalHost() { const string topic = "TestTopic"; var messages = new ConcurrentBag <IMessage>(); var observer = new AnonymousObserver <IMessage>(msg => { Console.WriteLine("Received: " + msg); messages.Add(msg); }, err => Console.WriteLine("Error: " + err)); IMessageSource <IMessage> source = new MessageSource <IMessage>("tcp://localhost:5556", new IMessageDeserializer <IMessage>[] { new ProtoBufMessageDeserializer <TestMessage1>(), new ProtoBufMessageDeserializer <TestMessage2>() }, topic); var sub = source.Messages.Subscribe(observer); IMessagePublisher <IMessage> publisher = new MessagePublisher <IMessage>("tcp://localhost:5556", new ProtoBufMessageSerializer(), topic); await publisher.PublishAsync(new TestMessage1 { Id = 1 }); await publisher.PublishAsync(new TestMessage1 { Id = 2 }); await publisher.PublishAsync(new TestMessage2 { Id = 1 }); await publisher.PublishAsync(new TestMessage2 { Id = 2 }); await publisher.PublishAsync(new TestMessage1 { Id = 3 }); await publisher.PublishAsync(new TestMessage2 { Id = 3 }); await Task.Delay(TimeSpan.FromSeconds(2)); Assert.That(messages.OfType <TestMessage1>().Any(msg => msg.Id == 1), "TestMessage1 1 not received"); Assert.That(messages.OfType <TestMessage1>().Any(msg => msg.Id == 2), "TestMessage1 2 not received"); Assert.That(messages.OfType <TestMessage1>().Any(msg => msg.Id == 3), "TestMessage1 3 not received"); Assert.That(messages.OfType <TestMessage2>().Any(msg => msg.Id == 1), "TestMessage2 1 not received"); Assert.That(messages.OfType <TestMessage2>().Any(msg => msg.Id == 2), "TestMessage2 2 not received"); Assert.That(messages.OfType <TestMessage2>().Any(msg => msg.Id == 3), "TestMessage2 3 not received"); sub.Dispose(); source.Dispose(); }
public async Task Test_MessagePublisher_Succeeds() { var queueMock = new Mock <CloudQueue>( CloudQueueFixture.GetQueueUri(), CloudQueueFixture.GetStorageCredentials()); queueMock.Setup(x => x.AddMessageAsync(It.IsAny <CloudQueueMessage>())) .Returns(Task.CompletedTask); queueMock.CallBase = false; var messageSerializer = Mock.Of <IMessageSerializer>(); Func <byte[], CloudQueueMessage> cloudQueueMessageFn = null; var messagePublisher = new MessagePublisher <ITestServiceMessage>( queueMock.Object, messageSerializer, cloudQueueMessageFn); var testCommand = new TestCommand1(); await messagePublisher.PublishAsync(testCommand); queueMock.Verify(x => x.AddMessageAsync(It.IsAny <CloudQueueMessage>()), Times.Once()); }
public async Task TestMessagesLongerThan32Characters() { int max = 5; CountdownEvent cd = new CountdownEvent(max); const string topic = "TestTopicxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; IDisposable sub; { var source = new MessageSource <IMessage>("tcp://localhost:5557", new IMessageDeserializer <IMessage>[] { new ProtoBufMessageDeserializer <TestMessageWhereTypeIsVeryMuchDefinitionLongerThen32Characters>(), }, topic); sub = source.Messages.Subscribe(msg => { Console.WriteLine("Received: " + msg); cd.Signal(); }, err => Console.WriteLine("Error: " + err)); } { var publisher = new MessagePublisher <IMessage>("tcp://localhost:5557", new ProtoBufMessageSerializer(), topic); for (int i = 0; i < max; i++) { await publisher.PublishAsync(new TestMessageWhereTypeIsVeryMuchDefinitionLongerThen32Characters() { Id = i }); } } await Task.Delay(TimeSpan.FromSeconds(2)); sub.Dispose(); }
public async Task GetsMessagePropertiesFromPropertyProviderAndAppliesThemToTheBrokeredMessage() { Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>(); BrokeredMessage brokeredMessageSent = null; mockMessageSender.Setup(ms => ms.SendAsync(It.IsAny<BrokeredMessage>())) .Callback<BrokeredMessage>(bm => { brokeredMessageSent = bm; }) .Returns(Task.FromResult<object>(null)); Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>(); Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>(); KeyValuePair<string, object>[] properties = new[] { new KeyValuePair<string, object>("Prop1", 1), new KeyValuePair<string, object>("Prop2", "two"), }; mockMessagePropertyProvider.Setup(mpp => mpp.GetProperties(It.IsAny<TestMessage>())) .Returns(properties); MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object); TestMessage message = new TestMessage(); await messagePublisher.PublishAsync(message); mockMessagePropertyProvider.Verify(mpp => mpp.GetProperties(It.Is<TestMessage>(it => Object.ReferenceEquals(it, message))), Times.Once); brokeredMessageSent.Should().NotBeNull(); // TODO: should be able to do this cleaner brokeredMessageSent.Properties.Should().ContainKeys(properties.Select(kvp => kvp.Key), "Should have translated all properties provided by the property provider to the BrokeredMessage."); brokeredMessageSent.Properties.Should().ContainValues(properties.Select(kvp => kvp.Value), "Should have translated all properties provided by the property provider to the BrokeredMessage."); }
public async Task RequestRequesterIdEndsUpAsReplyToSessionId() { Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>(); BrokeredMessage brokeredMessageSent = null; mockMessageSender.Setup(ms => ms.SendAsync(It.IsAny<BrokeredMessage>())) .Callback<BrokeredMessage>(bm => { brokeredMessageSent = bm; }) .Returns(Task.FromResult<object>(null)); Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>(); Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>(); MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object); TestRequest request = new TestRequest { RequesterId = "TestRequesterId" }; await messagePublisher.PublishAsync(request); mockMessageSender.Verify(ms => ms.SendAsync(It.IsAny<BrokeredMessage>()), Times.Once()); brokeredMessageSent.Should().NotBeNull(); brokeredMessageSent.ReplyToSessionId.Should().Be(request.RequesterId); }
public async Task SendsMessage() { Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>(); Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>(); Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>(); MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object); TestMessage message = new TestMessage(); await messagePublisher.PublishAsync(message); mockMessageSender.Verify(ms => ms.SendAsync(It.IsAny<BrokeredMessage>()), Times.Once()); }
public async Task AppliesMessageTypeNamePropertyToTheBrokeredMessage() { Mock<IMessageSender> mockMessageSender = new Mock<IMessageSender>(); BrokeredMessage brokeredMessageSent = null; mockMessageSender.Setup(ms => ms.SendAsync(It.IsAny<BrokeredMessage>())) .Callback<BrokeredMessage>(bm => { brokeredMessageSent = bm; }) .Returns(Task.FromResult<object>(null)); Mock<IMessageSerializer> mockMessageSerializer = new Mock<IMessageSerializer>(); Mock<IMessagePropertyProvider<TestMessage>> mockMessagePropertyProvider = new Mock<IMessagePropertyProvider<TestMessage>>(); MessagePublisher<TestMessage> messagePublisher = new MessagePublisher<TestMessage>(mockMessageSender.Object, mockMessageSerializer.Object, mockMessagePropertyProvider.Object); TestMessage message = new TestMessage(); await messagePublisher.PublishAsync(message); brokeredMessageSent.Should().NotBeNull(); brokeredMessageSent.Properties.Should().Contain(new KeyValuePair<string,object>(MessagePropertyNames.TypeName, typeof(TestMessage).Name), "Should have applied the mesage type name property to the BrokeredMessage."); }
public async Task SpecifingTopics() { var config = new TestMessageConfiguration(); var pubCount = 0; var subCount = 0; var subTopicList = new List <string>(); config.IsValid().ShouldBe(true); var stringPropertyValue = "string"; var message1 = new TestMessage { String = stringPropertyValue }; var message2 = new TestMessage2 { String = stringPropertyValue }; var message3 = new TestMessage3 { String = stringPropertyValue }; config.MessagePublishing += async(sender, e) => { pubCount++; e.ShouldBe(config.LatestMessagePublishingEventArgs); new[] { "TestTopic", "2TestTopic", "3TestTopic" } .ShouldContain(e.Topic); var strongTyped = JsonConvert.DeserializeObject <TestMessage>(e.Message); strongTyped.String.ShouldBe(stringPropertyValue); }; config.Subscription.Add("2TestTopic"); config.Subscription.Add("3TestTopic"); config.MessageReceiving += async(sender, e) => { subCount++; subTopicList.Add(e.Topic); config.Subscription.ShouldContain(e.Topic); var strongTyped = JsonConvert.DeserializeObject <TestMessage>(e.Message); strongTyped.String.ShouldBe(stringPropertyValue); }; var publisherWrapper = new KafkaProducerWrapper(config); var receiverWrapper = new KafkaConsumerWrapper(config); var publisher = new MessagePublisher(config, publisherWrapper); receiverWrapper.Connect(); publisherWrapper.Connect(); await publisher.PublishAsync(message1); await publisher.PublishAsync(message2); await publisher.PublishAsync(message3); publisherWrapper.Disconnect(); await Task.Delay(10000).ConfigureAwait(false); receiverWrapper.Disconnect(); config.LatestMessagePublishingEventArgs.ShouldNotBeNull(); config.LatestMessageReceivingEventArgs.ShouldNotBeNull(); pubCount.ShouldBe(3); subCount.ShouldBe(2); }
public async Task ShouldCorrectlyPublishAndSubscribeToMulipleMultiplexedTopics() { const string brokerUri = "tcp://localhost:61616"; const string topicName1 = "Obvs.Tests.ShouldCorrectlyPublishAndSubscribeToMulipleMultiplexedTopics1"; const string topicName2 = "Obvs.Tests.ShouldCorrectlyPublishAndSubscribeToMulipleMultiplexedTopics2"; IMessagePropertyProvider<IMessage> getProperties = new DefaultPropertyProvider<IMessage>(); IConnectionFactory connectionFactory = new ConnectionFactory(brokerUri); var lazyConnection = new Lazy<IConnection>(() => { var conn = connectionFactory.CreateConnection(); conn.Start(); return conn; }); IMessagePublisher<IMessage> publisher1 = new MessagePublisher<IMessage>( lazyConnection, new ActiveMQTopic(topicName1), new JsonMessageSerializer(), getProperties, _testScheduler); IMessagePublisher<IMessage> publisher2 = new MessagePublisher<IMessage>( lazyConnection, new ActiveMQTopic(topicName2), new JsonMessageSerializer(), getProperties, _testScheduler); IMessageDeserializer<IMessage>[] deserializers = { new JsonMessageDeserializer<TestMessage>(), new JsonMessageDeserializer<TestMessage2>() }; IMessageSource<IMessage> source = new MergedMessageSource<IMessage>(new[] { new MessageSource<IMessage>( lazyConnection, deserializers, new ActiveMQTopic(topicName1)), new MessageSource<IMessage>( lazyConnection, deserializers, new ActiveMQTopic(topicName2)) }); source.Messages.Subscribe(Console.WriteLine); await publisher1.PublishAsync(new TestMessage { Id = 1234 }); await publisher1.PublishAsync(new TestMessage2 { Id = 4567 }); await publisher2.PublishAsync(new TestMessage { Id = 8910 }); await publisher2.PublishAsync(new TestMessage2 { Id = 1112 }); Thread.Sleep(TimeSpan.FromSeconds(3)); }
public void ShouldCorrectlyPublishAndSubscribeToMulipleMultiplexedTopics() { const string brokerUri = "tcp://localhost:61616"; const string topicName1 = "Obvs.Tests.ShouldCorrectlyPublishAndSubscribeToMulipleMultiplexedTopics1"; const string topicName2 = "Obvs.Tests.ShouldCorrectlyPublishAndSubscribeToMulipleMultiplexedTopics2"; IMessagePropertyProvider <IMessage> getProperties = new DefaultPropertyProvider <IMessage>(); IConnectionFactory connectionFactory = new ConnectionFactory(brokerUri); var lazyConnection = new Lazy <IConnection>(() => { var conn = connectionFactory.CreateConnection(); conn.Start(); return(conn); }); var scheduler = new EventLoopScheduler(); IMessagePublisher <IMessage> publisher1 = new MessagePublisher <IMessage>( lazyConnection, new ActiveMQTopic(topicName1), new JsonMessageSerializer(), getProperties, scheduler); IMessagePublisher <IMessage> publisher2 = new MessagePublisher <IMessage>( lazyConnection, new ActiveMQTopic(topicName2), new JsonMessageSerializer(), getProperties, scheduler); IMessageDeserializer <IMessage>[] deserializers = { new JsonMessageDeserializer <TestMessage>(), new JsonMessageDeserializer <TestMessage2>() }; IMessageSource <IMessage> source = new MergedMessageSource <IMessage>(new[] { new MessageSource <IMessage>( lazyConnection, deserializers, new ActiveMQTopic(topicName1)), new MessageSource <IMessage>( lazyConnection, deserializers, new ActiveMQTopic(topicName2)) }); source.Messages.Subscribe(Console.WriteLine); publisher1.PublishAsync(new TestMessage { Id = 1234 }); publisher1.PublishAsync(new TestMessage2 { Id = 4567 }); publisher2.PublishAsync(new TestMessage { Id = 8910 }); publisher2.PublishAsync(new TestMessage2 { Id = 1112 }); Thread.Sleep(TimeSpan.FromSeconds(3)); }