Example #1
0
        public async Task ItWorks()
        {
            var gotMessage = new ManualResetEvent(false);

            _activator.Handle <string>(async(bus, context, message) =>
            {
                Console.WriteLine($"Got message with ID {context.Headers.GetValue(Headers.MessageId)} - waiting 2 minutes....");
                await Task.Delay(TimeSpan.FromMinutes(2));
                Console.WriteLine("done waiting");

                gotMessage.Set();
            });

            _busStarter.Start();

            await _bus.SendLocal("hej med dig min ven!");

            gotMessage.WaitOrDie(TimeSpan.FromMinutes(2.1));

            // shut down bus
            _bus.Dispose();

            // see if queue is empty
            using var scope = new RebusTransactionScope();

            var message = await _transport.Receive(scope.TransactionContext, CancellationToken.None);

            await scope.CompleteAsync();

            if (message != null)
            {
                throw new AssertionException(
                          $"Did not expect to receive a message - got one with ID {message.Headers.GetValue(Headers.MessageId)}");
            }
        }
        public async Task ItWorks()
        {
            var gotMessage = new ManualResetEvent(false);

            _activator.Handle <string>(async str =>
            {
                Console.WriteLine("waiting 6 minutes....");

                // longer than the longest asb peek lock in the world...
                //await Task.Delay(TimeSpan.FromSeconds(3));
                await Task.Delay(TimeSpan.FromMinutes(6));

                Console.WriteLine("done waiting");

                gotMessage.Set();
            });

            await _bus.SendLocal("hej med dig min ven!");

            gotMessage.WaitOrDie(TimeSpan.FromMinutes(6.5));

            // shut down bus
            CleanUpDisposables();

            // see if queue is empty
            using (var transactionContext = new DefaultTransactionContext())
            {
                var message = await _transport.Receive(transactionContext, new CancellationTokenSource().Token);

                if (message != null)
                {
                    throw new AssertionException(
                              $"Did not expect to receive a message - got one with ID {message.Headers.GetValue(Headers.MessageId)}");
                }

                await transactionContext.Complete();
            }
        }
Example #3
0
        async Task <TransportMessage> GetNextMessageFromErrorQueue(int timeoutSeconds)
        {
            var cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken       = cancellationTokenSource.Token;
            var timeout = TimeSpan.FromSeconds(timeoutSeconds);

            cancellationTokenSource.CancelAfter(timeout);

            try
            {
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    using (var scope = new RebusTransactionScope())
                    {
                        var message = await errorQueueTransport.Receive(scope.TransactionContext, cancellationToken);

                        try
                        {
                            if (message != null)
                            {
                                return(message);
                            }
                        }
                        finally
                        {
                            await scope.CompleteAsync();
                        }
                    }
                }
            }
            catch (OperationCanceledException) when(cancellationToken.IsCancellationRequested)
            {
                throw new TimeoutException($"Did not receive message in queue '{errorQueueTransport.Address}' within timeout of {timeout}");
            }
        }
        public async Task DoesntIgnoreDefinedTimeoutWhenReceiving(int operationTimeoutInSeconds)
        {
            var operationTimeout = TimeSpan.FromSeconds(operationTimeoutInSeconds);

            var connString = StandardAzureServiceBusTransportFactory.ConnectionString;
            var builder    = new ServiceBusConnectionStringBuilder(connString)
            {
                OperationTimeout = operationTimeout
            };
            var newConnString = builder.ToString();

            var consoleLoggerFactory = new ConsoleLoggerFactory(false);
            var transport            = new AzureServiceBusTransport(newConnString, QueueName, consoleLoggerFactory, new TplAsyncTaskFactory(consoleLoggerFactory));

            Using(transport);

            transport.PurgeInputQueue();
            //Create the queue for the receiver since it cannot create it self beacuse of lacking rights on the namespace
            transport.CreateQueue(QueueName);

            var senderActivator = new BuiltinHandlerActivator();

            var senderBus = Configure.With(senderActivator)
                            .Transport(t => t.UseAzureServiceBus(newConnString, "sender"))
                            .Start();

            Using(senderBus);

            // queue 3 messages
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver");

            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver2");

            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver3");

            await Task.Delay(TimeSpan.FromSeconds(2)); // wait a bit to make sure the messages are queued.

            // receive 1
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, _cancellationToken);

                sw.Stop();
                await scope.CompleteAsync();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 2
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, _cancellationToken);

                sw.Stop();
                await scope.CompleteAsync();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 3
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, _cancellationToken);

                sw.Stop();
                await scope.CompleteAsync();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 4 - NOTHING
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, _cancellationToken);

                sw.Stop();
                await scope.CompleteAsync();

                msg.Should().BeNull();
                sw.Elapsed.Should().BeCloseTo(operationTimeout, 2000);
            }

            // put 1 more message
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver5");

            await Task.Delay(TimeSpan.FromSeconds(2)); // wait a bit to make sure the messages are queued.

            // receive 5
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, _cancellationToken);

                sw.Stop();
                await scope.CompleteAsync();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 6 - NOTHING
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, _cancellationToken);

                sw.Stop();
                await scope.CompleteAsync();

                msg.Should().BeNull();
                sw.Elapsed.Should().BeCloseTo(operationTimeout, 2000);
            }
        }
        public async Task DoesntIgnoreDefinedTimeoutWhenReceiving(int operationTimeoutInSeconds)
        {
            var operationTimeout = TimeSpan.FromSeconds(operationTimeoutInSeconds);

            var connString = AsbTestConfig.ConnectionString;

            var consoleLoggerFactory = new ConsoleLoggerFactory(false);
            var transport            = new AzureServiceBusTransport(connString, QueueName, consoleLoggerFactory, new TplAsyncTaskFactory(consoleLoggerFactory), new DefaultNameFormatter());

            transport.ReceiveOperationTimeout = TimeSpan.FromSeconds(operationTimeoutInSeconds);
            Using(transport);

            transport.Initialize();

            transport.PurgeInputQueue();
            //Create the queue for the receiver since it cannot create it self beacuse of lacking rights on the namespace
            transport.CreateQueue(QueueName);

            var senderActivator = new BuiltinHandlerActivator();

            var senderBus = Configure.With(senderActivator)
                            .Transport(t => t.UseAzureServiceBus(connString, "sender"))
                            .Start();

            Using(senderBus);

            // queue 3 messages
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver");

            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver2");

            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver3");

            //await Task.Delay(TimeSpan.FromSeconds(2)); // wait a bit to make sure the messages are queued.

            // receive 1
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, CancellationToken.None);

                sw.Stop();
                await scope.CompleteAsync();

                Assert.That(msg, Is.Not.Null);
                Assert.That(sw.Elapsed, Is.LessThan(TimeSpan.FromMilliseconds(1500)));
            }

            // receive 2
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, CancellationToken.None);

                sw.Stop();
                await scope.CompleteAsync();

                Assert.That(msg, Is.Not.Null);
                Assert.That(sw.Elapsed, Is.LessThan(TimeSpan.FromMilliseconds(1500)));
            }

            // receive 3
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, CancellationToken.None);

                sw.Stop();
                await scope.CompleteAsync();

                Assert.That(msg, Is.Not.Null);
                Assert.That(sw.Elapsed, Is.LessThan(TimeSpan.FromMilliseconds(1500)));
            }

            // receive 4 - NOTHING
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, CancellationToken.None);

                sw.Stop();
                await scope.CompleteAsync();

                Assert.That(msg, Is.Null);
                Assert.That(sw.Elapsed, Is.LessThan(operationTimeout.Add(TimeSpan.FromSeconds(2))).And.GreaterThan(operationTimeout.Subtract(TimeSpan.FromSeconds(2))));
            }

            // put 1 more message
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver5");

            await Task.Delay(TimeSpan.FromSeconds(2)); // wait a bit to make sure the messages are queued.

            // receive 5
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, CancellationToken.None);

                sw.Stop();
                await scope.CompleteAsync();

                Assert.That(msg, Is.Not.Null);
                Assert.That(sw.Elapsed, Is.LessThan(TimeSpan.FromMilliseconds(1500)));
            }

            // receive 6 - NOTHING
            using (var scope = new RebusTransactionScope())
            {
                var sw  = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(scope.TransactionContext, CancellationToken.None);

                sw.Stop();
                await scope.CompleteAsync();

                Assert.That(msg, Is.Null);
                Assert.That(sw.Elapsed, Is.LessThan(operationTimeout.Add(TimeSpan.FromSeconds(2))).And.GreaterThan(operationTimeout.Subtract(TimeSpan.FromSeconds(2))));
            }
        }
        public async void DoesntIgnoreDefinedTimeoutWhenReceiving(AzureServiceBusMode mode, int operationTimeoutInSeconds)
        {
            var operationTimeout = TimeSpan.FromSeconds(operationTimeoutInSeconds);

            var connString = StandardAzureServiceBusTransportFactory.ConnectionString;
            var builder = new ServiceBusConnectionStringBuilder(connString)
            {
                OperationTimeout = operationTimeout
            };
            var newConnString = builder.ToString();

            var consoleLoggerFactory = new ConsoleLoggerFactory(false);
            var transport = new AzureServiceBusTransport(newConnString, QueueName, consoleLoggerFactory, new TplAsyncTaskFactory(consoleLoggerFactory), new BusLifetimeEvents());

            Using(transport);

            transport.PurgeInputQueue();
            //Create the queue for the receiver since it cannot create it self beacuse of lacking rights on the namespace
            transport.CreateQueue(QueueName);

            var senderActivator = new BuiltinHandlerActivator();

            var senderBus = Configure.With(senderActivator)
                .Transport(t => t.UseAzureServiceBus(newConnString, "sender", mode))
                .Start();

            Using(senderBus);

            // queue 3 messages
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver");
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver2");
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver3");

            await Task.Delay(TimeSpan.FromSeconds(2)); // wait a bit to make sure the messages are queued.

            // receive 1
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 2
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 3
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 4 - NOTHING
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().BeNull();
                sw.Elapsed.Should().BeCloseTo(operationTimeout, 2000);
            }

            // put 1 more message 
            await senderBus.Advanced.Routing.Send(QueueName, "message to receiver5");

            await Task.Delay(TimeSpan.FromSeconds(2)); // wait a bit to make sure the messages are queued.

            // receive 5
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().NotBeNull();
                sw.Elapsed.Should().BeLessThan(TimeSpan.FromMilliseconds(1500));
            }

            // receive 6 - NOTHING
            using (var transactionContext = new DefaultTransactionContext())
            {
                var sw = System.Diagnostics.Stopwatch.StartNew();
                var msg = await transport.Receive(transactionContext);
                sw.Stop();
                await transactionContext.Complete();

                msg.Should().BeNull();
                sw.Elapsed.Should().BeCloseTo(operationTimeout, 2000);
            }
        }