コード例 #1
0
        public void Should_run_a_batch_nicely()
        {
            const int batchSize = 10000;
            var callbackCount = 0;
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            using (var channel = bus.OpenPublishChannel(x => x.WithPublisherConfirms()))
            {
                for (int i = 0; i < batchSize; i++)
                {
                    var message = new MyMessage {Text = string.Format("Hello Message {0}", i)};
                    channel.Publish(message, x =>
                        x.OnSuccess(() => {
                            callbackCount++;
                        })
                        .OnFailure(() =>
                        {
                            callbackCount++;
                        }));
                }

                // wait until all the publications have been acknowleged.
                while (callbackCount < batchSize)
                {
                    if (stopwatch.Elapsed.Seconds > 10)
                    {
                        throw new ApplicationException("Aborted batch with timeout");
                    }
                    Thread.Sleep(10);
                }
            }
        }
コード例 #2
0
        public void Should_be_able_to_serialize_and_deserialize_a_message()
        {
            var message = new MyMessage {Text = "Hello World"};

            var binaryMessage = serializer.MessageToBytes(message);
            var deseralizedMessage = serializer.BytesToMessage<MyMessage>(binaryMessage);

            message.Text.ShouldEqual(deseralizedMessage.Text);
        }
コード例 #3
0
        public void When_using_the_default_serialization_strategy_messages_are_correctly_round_tripped()
        {
            var typeNameSerializer = new TypeNameSerializer();
            var serializer = new JsonSerializer( typeNameSerializer );
            const string correlationId = "CorrelationId";
            var serializationStrategy = new DefaultMessageSerializationStrategy( typeNameSerializer, serializer, () => correlationId );

            var messageBody = new MyMessage {Text = "Hello world!"};
            var message = new Message<MyMessage>( messageBody );
            var serializedMessage = serializationStrategy.SerializeMessage( message );
            var deserializedMessage = serializationStrategy.DeserializeMessage( serializedMessage.Properties, serializedMessage.Body );

            Assert.That( deserializedMessage.Message.Body.GetType(), Is.EqualTo( message.Body.GetType() ) );
            Assert.That( deserializedMessage.Message.Body.Text, Is.EqualTo( message.Body.Text ) );
        }
コード例 #4
0
ファイル: PublishTests.cs プロジェクト: JohnEffo/EasyNetQ
        public void SetUp()
        {
            mockBuilder = new MockBuilder(x =>
                x.Register<Func<string>>(_ => () => correlationId));

            mockBuilder.NextModel.Stub(x =>
                x.BasicPublish(null, null, false, false, null, null))
                    .IgnoreArguments()
                    .Callback<string, string, bool, bool, IBasicProperties, byte[]>((e, r, m, i, p, b) =>
                    {
                        body = b;
                        properties = p;
                        return true;
                    });

            var message = new MyMessage { Text = "Hiya!" };
            mockBuilder.Bus.Publish(message);
        }
コード例 #5
0
ファイル: SubscribeTests.cs プロジェクト: stemarie/EasyNetQ
        public void SetUp()
        {
            var conventions = new Conventions
            {
                ConsumerTagConvention = () => consumerTag
            };

            mockBuilder = new MockBuilder(x => x
                .Register<IConventions>(_ => conventions)
                //.Register<IEasyNetQLogger>(_ => new ConsoleLogger())
                );

            var autoResetEvent = new AutoResetEvent(false);
            var handlerExecutionContext = (HandlerRunner)mockBuilder.ServiceProvider.Resolve<IHandlerRunner>();
            handlerExecutionContext.SynchronisationAction = () => autoResetEvent.Set();

            mockBuilder.Bus.Subscribe<MyMessage>(subscriptionId, message =>
            {
                deliveredMessage = message;
            });

            const string text = "Hello there, I am the text!";
            originalMessage = new MyMessage { Text = text };

            var body = new JsonSerializer().MessageToBytes(originalMessage);

            // deliver a message
            mockBuilder.Consumers[0].HandleBasicDeliver(
                consumerTag,
                deliveryTag,
                false, // redelivered
                typeName,
                "#",
                new BasicProperties
                {
                    Type = typeName,
                    CorrelationId = correlationId
                },
                body);

            // wait for the subscription thread to handle the message ...
            autoResetEvent.WaitOne(1000);
        }
コード例 #6
0
ファイル: PublishTests.cs プロジェクト: hippasus/EasyNetQ
        public void SetUp()
        {
            mockBuilder = new MockBuilder(x =>
                x.Register<ICorrelationIdGenerationStrategy>(_ => new StaticCorrelationIdGenerationStrategy(correlationId)));

            mockBuilder.NextModel.Stub(x =>
                x.BasicPublish(null, null, false, null, null))
                    .IgnoreArguments()
                    .Callback<string, string, bool, IBasicProperties, byte[]>((e, r, m, p, b) =>
                    {
                        body = b;
                        properties = p;
                        return true;
                    });

            var message = new MyMessage { Text = "Hiya!" };
            mockBuilder.Bus.Publish(message);
            WaitForMessageToPublish();
        }
コード例 #7
0
ファイル: PublishTests.cs プロジェクト: stemarie/EasyNetQ
        public void SetUp()
        {
            mockBuilder = new MockBuilder(x =>
                x.Register<Func<string>>(_ => () => correlationId));

            using (var channel = mockBuilder.Bus.OpenPublishChannel())
            {
                mockBuilder.Channels[0].Stub(x =>
                    x.BasicPublish(null, null, null, null))
                        .IgnoreArguments()
                        .Callback<string, string, IBasicProperties, byte[]>((e, r, p, b) =>
                        {
                            body = b;
                            properties = p;
                            return true;
                        });

                var message = new MyMessage { Text = "Hiya!" };
                channel.Publish(message);
            }
        }
コード例 #8
0
        public void Should_acknowledge_publication_with_publisher_confirms()
        {
            var wait = new AutoResetEvent(false);
            var confirmed = false;

            using (var channel = bus.OpenPublishChannel(x => x.WithPublisherConfirms()))
            {
                var message = new MyMessage {Text = "Hello Confirm!"};

                channel.Publish(message, x =>
                    x.OnSuccess(() =>
                    {
                        confirmed = true;
                        wait.Set();
                    })
                    .OnFailure(() => wait.Set()));

                wait.WaitOne(2000);
            }

            confirmed.ShouldBeTrue();
        }
コード例 #9
0
        public void Should_correctly_create_generic_message()
        {
            var message = new MyMessage { Text = "Hello World" };

            var genericMessage = MessageFactory.CreateInstance(typeof(MyMessage), message);

            Assert.IsNotNull(genericMessage);
            Assert.IsInstanceOf<Message<MyMessage>>(genericMessage);
            Assert.IsInstanceOf<MyMessage>(genericMessage.GetBody());
            Assert.IsTrue(genericMessage.MessageType == typeof(MyMessage));
            Assert.IsTrue(genericMessage.CastTo<Message<MyMessage>>().Body.Text == message.Text);

            var properties = new MessageProperties { CorrelationId = Guid.NewGuid().ToString() };
            var genericMessageWithProperties = MessageFactory.CreateInstance(typeof(MyMessage), message, properties);

            Assert.IsNotNull(genericMessageWithProperties);
            Assert.IsInstanceOf<Message<MyMessage>>(genericMessageWithProperties);
            Assert.IsInstanceOf<MyMessage>(genericMessageWithProperties.GetBody());
            Assert.IsTrue(genericMessageWithProperties.MessageType == typeof(MyMessage));
            Assert.IsTrue(genericMessageWithProperties.CastTo<Message<MyMessage>>().Body.Text == message.Text);
            Assert.IsTrue(genericMessageWithProperties.CastTo<Message<MyMessage>>().Properties.CorrelationId == properties.CorrelationId);
        }
コード例 #10
0
        public void SetUp()
        {
            var conventions = new Conventions(new TypeNameSerializer())
            {
                ConsumerTagConvention = () => consumerTag
            };

            consumerErrorStrategy = MockRepository.GenerateStub<IConsumerErrorStrategy>();
            consumerErrorStrategy.Stub(x => x.HandleConsumerError(null, null))
                .IgnoreArguments()
                .WhenCalled(i =>
                {
                    basicDeliverEventArgs = (ConsumerExecutionContext)i.Arguments[0];
                    raisedException = (Exception) i.Arguments[1];
                }).Return(AckStrategies.Ack);

            mockBuilder = new MockBuilder(x => x
                .Register<IConventions>(_ => conventions)
                .Register(_ => consumerErrorStrategy)
                //.Register<IEasyNetQLogger>(_ => new ConsoleLogger())
                );

            mockBuilder.Bus.Subscribe<MyMessage>(subscriptionId, message =>
            {
                throw originalException;
            });

            const string text = "Hello there, I am the text!";
            originalMessage = new MyMessage { Text = text };

            var body = new JsonSerializer(new TypeNameSerializer()).MessageToBytes(originalMessage);

            // deliver a message
            mockBuilder.Consumers[0].HandleBasicDeliver(
                consumerTag,
                deliveryTag,
                false, // redelivered
                typeName,
                "#",
                new BasicProperties
                {
                    Type = typeName,
                    CorrelationId = correlationId
                },
                body);

            // wait for the subscription thread to handle the message ...
            var autoResetEvent = new AutoResetEvent(false);
            mockBuilder.EventBus.Subscribe<AckEvent>(x => autoResetEvent.Set());
            autoResetEvent.WaitOne(1000);
        }
コード例 #11
0
 public void Should_be_able_to_publish()
 {
     var message = new MyMessage { Text = "Hello! " + Guid.NewGuid().ToString().Substring(0, 5) };
     bus.Publish(message);
     Console.Out.WriteLine("message.Text = {0}", message.Text);
 }
コード例 #12
0
 public void Should_be_able_to_publish()
 {
     var message = new MyMessage { Text = "Hello! " + Guid.NewGuid().ToString().Substring(0, 5) };
     using (var publishChannel = bus.OpenPublishChannel())
     {
         publishChannel.Publish(message);
     }
     Console.Out.WriteLine("message.Text = {0}", message.Text);
 }
コード例 #13
0
ファイル: PublishTests.cs プロジェクト: yonglehou/EasyNetQ
        public void SetUp()
        {
            mockBuilder = new MockBuilder();

            var message = new MyMessage { Text = "Hiya!" };
            mockBuilder.Bus.Publish(message, "X.A");
            WaitForMessageToPublish();
        }
コード例 #14
0
 public void Should_throw_if_callbacks_are_not_set()
 {
     using (var channel = bus.OpenPublishChannel(x => x.WithPublisherConfirms()))
     {
         var message = new MyMessage { Text = "Hello Confirm!" };
         channel.Publish(message);
     }
 }
コード例 #15
0
ファイル: PublishTests.cs プロジェクト: stemarie/EasyNetQ
        public void SetUp()
        {
            mockBuilder = new MockBuilder();

            using (var channel = mockBuilder.Bus.OpenPublishChannel())
            {
                var message = new MyMessage { Text = "Hiya!" };
                channel.Publish(message, x => x.WithTopic("X.A"));
            }
        }
コード例 #16
0
ファイル: PublishTests.cs プロジェクト: stemarie/EasyNetQ
        public void SetUp()
        {
            success = false;
            failure = false;
            mockBuilder = new MockBuilder();

            Action successAction = () =>
                {
                    success = true;
                };
            Action failureAction = () =>
                {
                    failure = true;
                };

            using (var channel = mockBuilder.Bus.OpenPublishChannel(x => x.WithPublisherConfirms()))
            {
                mockBuilder.Channels[0].AssertWasCalled(x => x.BasicAcks += Arg<BasicAckEventHandler>.Is.Anything);
                mockBuilder.Channels[0].AssertWasCalled(x => x.BasicNacks += Arg<BasicNackEventHandler>.Is.Anything);

                var message = new MyMessage { Text = "Hiya!" };
                channel.Publish(message, x => x
                    .OnSuccess(successAction)
                    .OnFailure(failureAction));
            }
        }