Ejemplo n.º 1
0
        public async Task VerifyMessagesArePublished()
        {
            // Setup the message bus.
            var cs        = Any.String();
            var publisher = new MockPublisher();
            var mbf       = new MockMessageBusFactory {
                Publisher = publisher
            };
            var mbd = new MessageBusDescription {
                ConnectionString = cs, Factory = mbf
            };
            var bus    = new MessageBus(mbd);
            var entity = Any.String();

            Assert.IsFalse(
                publisher.IsInitialized,
                "The publisher should not be initialized before it is called the first time.");

            // Send a message on the bus.
            var message = new MockMessage
            {
                CorrelationKey = Any.String(),
                Message        = Any.String(),
                MessageKey     = Any.String(),
                PartitionKey   = Any.String(),
                Properties     =
                    new Dictionary <string, object> {
                    { Any.String(), Any.String() }
                }
            };
            await bus.SendAsync(entity, message);

            Assert.IsTrue(
                publisher.IsInitialized,
                "The publisher should be initialized after it is called the first time.");
            Assert.AreEqual(publisher.Description.ConnectionString, cs);
            Assert.AreEqual(publisher.Description.Entity, entity);

            await bus.CloseAsync();

            Assert.IsTrue(publisher.IsClosed, "The publisher should be closed.");

            // Verify the message sent.
            Assert.AreEqual(publisher.IsInitialized, true);
            Assert.AreEqual(publisher.Message.CorrelationKey, message.CorrelationKey);
            Assert.AreEqual(publisher.Message.Message, message.Message);
            Assert.AreEqual(publisher.Message.MessageKey, message.MessageKey);
            Assert.AreEqual(publisher.Message.PartitionKey, message.PartitionKey);
        }
        public async Task VerifyMessageCrossesBridge()
        {
            // Create the source message bus.
            var sourceBus = new MockMessageBus();
            var sourceEntity = Any.String();

            // Create the target message bus.
            var targetBus = new MockMessageBus();
            const string TargetEntity = "TestEventHub";

            // Setup the target callback. This is the output of the bridge.
            var called = false;
            await targetBus.RegisterHandlerAsync(TargetEntity, "TestEventHub", m => Task.Run(() => { called = true; }));
            
            // Create the message bus bridge.
            var bridgeName = Any.String();

            var bridge = new MySimpleMessageBridge();
            await
                bridge.InitializeAsync(
                    new MessageBusBridgeDescription
                        {
                            BridgeName = bridgeName, 
                            SourceBus = sourceBus, 
                            SourceEntity = sourceEntity, 
                            TargetBus = targetBus,
                            TargetEntity = TargetEntity
                        });

            // Send the message.
            var message = new MockMessage
                              {
                                  CorrelationKey = Any.String(),
                                  Message = "{\"Name\":\"TestEventHub\"}", 
                                  MessageKey = Any.String(), 
                                  PartitionKey = Any.String()
                              };
            await sourceBus.SendAsync(sourceEntity, message);

            // Assert that the message reached the other side.
            Assert.IsTrue(called, "The target callback must be called.");

            await bridge.CloseAsync();
        }
        public async Task VerifyMessageCrossesBridge()
        {
            // Create the source message bus.
            var sourceBus    = new MockMessageBus();
            var sourceEntity = Any.String();

            // Create the target message bus.
            var          targetBus    = new MockMessageBus();
            const string TargetEntity = "TestEventHub";

            // Setup the target callback. This is the output of the bridge.
            var called = false;
            await targetBus.RegisterHandlerAsync(TargetEntity, "TestEventHub", m => Task.Run(() => { called = true; }));

            // Create the message bus bridge.
            var bridgeName = Any.String();

            var bridge = new MySimpleMessageBridge();
            await
            bridge.InitializeAsync(
                new MessageBusBridgeDescription
            {
                BridgeName   = bridgeName,
                SourceBus    = sourceBus,
                SourceEntity = sourceEntity,
                TargetBus    = targetBus,
                TargetEntity = TargetEntity
            });

            // Send the message.
            var message = new MockMessage
            {
                CorrelationKey = Any.String(),
                Message        = "{\"Name\":\"TestEventHub\"}",
                MessageKey     = Any.String(),
                PartitionKey   = Any.String()
            };
            await sourceBus.SendAsync(sourceEntity, message);

            // Assert that the message reached the other side.
            Assert.IsTrue(called, "The target callback must be called.");

            await bridge.CloseAsync();
        }
        public async Task VerifyMessagesArePublished()
        {
            // Setup the message bus.
            var cs = Any.String();
            var publisher = new MockPublisher();
            var mbf = new MockMessageBusFactory { Publisher = publisher };
            var mbd = new MessageBusDescription { ConnectionString = cs, Factory = mbf };
            var bus = new MessageBus(mbd);
            var entity = Any.String();

            Assert.IsFalse(
                publisher.IsInitialized, 
                "The publisher should not be initialized before it is called the first time.");

            // Send a message on the bus.
            var message = new MockMessage
                              {
                                  CorrelationKey = Any.String(), 
                                  Message = Any.String(), 
                                  MessageKey = Any.String(), 
                                  PartitionKey = Any.String(), 
                                  Properties =
                                      new Dictionary<string, object> { { Any.String(), Any.String() } }
                              };
            await bus.SendAsync(entity, message);

            Assert.IsTrue(
                publisher.IsInitialized, 
                "The publisher should be initialized after it is called the first time.");
            Assert.AreEqual(publisher.Description.ConnectionString, cs);
            Assert.AreEqual(publisher.Description.Entity, entity);

            await bus.CloseAsync();

            Assert.IsTrue(publisher.IsClosed, "The publisher should be closed.");

            // Verify the message sent.
            Assert.AreEqual(publisher.IsInitialized, true);
            Assert.AreEqual(publisher.Message.CorrelationKey, message.CorrelationKey);
            Assert.AreEqual(publisher.Message.Message, message.Message);
            Assert.AreEqual(publisher.Message.MessageKey, message.MessageKey);
            Assert.AreEqual(publisher.Message.PartitionKey, message.PartitionKey);
        }