Esempio n. 1
0
        public static async Task <TransportMessage> AwaitReceive(this ITransport transport, double timeoutSeconds = 5)
        {
            var stopwatch = Stopwatch.StartNew();
            var timeout   = TimeSpan.FromSeconds(timeoutSeconds);
            var source    = new CancellationTokenSource();

            while (stopwatch.Elapsed < timeout)
            {
                TransportMessage receivedTransportMessage;

                using (var transactionContext = new DefaultTransactionContextScope())
                {
                    receivedTransportMessage = await transport.Receive(AmbientTransactionContext.Current, source.Token);

                    await transactionContext.Complete();
                }

                if (receivedTransportMessage != null)
                {
                    return(receivedTransportMessage);
                }
            }

            throw new AssertionException($"Did not receive transport message from {transport} within {timeout} timeout");
        }
Esempio n. 2
0
        async Task <List <string> > GetAll(ITransport input)
        {
            var transportMessages = new List <string>();
            var receivedNulls     = 0;

            while (receivedNulls < 5)
            {
                using (var transactionContext = new DefaultTransactionContextScope())
                {
                    var msg = await input.Receive(AmbientTransactionContext.Current, _cancellationToken);

                    if (msg != null)
                    {
                        transportMessages.Add(GetStringBody(msg));
                        await transactionContext.Complete();

                        continue;
                    }

                    await Task.Delay(100);

                    receivedNulls++;
                }
            }

            return(transportMessages);
        }
Esempio n. 3
0
        public static async Task <TransportMessage> WaitForNextMessage(this ITransport transport, int timeoutSeconds = 5)
        {
            var stopwatch = Stopwatch.StartNew();

            while (true)
            {
                using (var context = new DefaultTransactionContextScope())
                {
                    var nextMessage = await transport.Receive(AmbientTransactionContext.Current, new CancellationToken());

                    if (nextMessage != null)
                    {
                        return(nextMessage);
                    }

                    await context.Complete();
                }

                await Task.Delay(100);

                if (stopwatch.Elapsed < TimeSpan.FromSeconds(timeoutSeconds))
                {
                    continue;
                }

                throw new TimeoutException($"Did not receive message from transport with address '{transport.Address}' within {timeoutSeconds} s timeout");
            }
        }
Esempio n. 4
0
        public async Task DoesNotReceiveExpiredMessage()
        {
            var queueName = TestConfig.GetName("expiration");
            var transport = _factory.Create(queueName);
            var id        = Guid.NewGuid().ToString();

            using (var transactionContext = new DefaultTransactionContextScope())
            {
                var headers = new Dictionary <string, string>
                {
                    { Headers.MessageId, Guid.NewGuid().ToString() },
                    { "recognizzle", id },
                    { Headers.TimeToBeReceived, "00:00:04" } //< expires after 4 seconds!
                };
                await transport.Send(queueName, MessageWith(headers), AmbientTransactionContext.Current);

                await transactionContext.Complete();
            }

            const int millisecondsDelay = 7000;

            var stopwatch = Stopwatch.StartNew();
            await Task.Delay(millisecondsDelay);

            Console.WriteLine($"Delay of {millisecondsDelay} ms actually lasted {stopwatch.ElapsedMilliseconds:0} ms");

            using (var transactionContext = new DefaultTransactionContextScope())
            {
                var transportMessage = await transport.Receive(AmbientTransactionContext.Current, _cancellationToken);

                await transactionContext.Complete();

                Assert.That(transportMessage, Is.Null);
            }
        }
Esempio n. 5
0
        public async Task ReceivesNonExpiredMessage()
        {
            var queueName = TestConfig.GetName("expiration");
            var transport = _factory.Create(queueName);
            var id        = Guid.NewGuid().ToString();

            using (var transactionContext = new DefaultTransactionContextScope())
            {
                var headers = new Dictionary <string, string>
                {
                    { Headers.MessageId, Guid.NewGuid().ToString() },
                    { "recognizzle", id }
                };
                await transport.Send(queueName, MessageWith(headers), AmbientTransactionContext.Current);

                await transactionContext.Complete();
            }

            await Task.Delay(5000);

            using (var transactionContext = new DefaultTransactionContextScope())
            {
                var transportMessage = await transport.Receive(AmbientTransactionContext.Current, _cancellationToken);

                await transactionContext.Complete();

                Assert.That(transportMessage, Is.Not.Null);

                var headers = transportMessage.Headers;

                Assert.That(headers.ContainsKey("recognizzle"));
                Assert.That(headers["recognizzle"], Is.EqualTo(id));
            }
        }
Esempio n. 6
0
        public async Task ReceivesAlmostExpiredMessage()
        {
            var queueName = TestConfig.GetName("expiration");
            var transport = _factory.Create(queueName);
            var id        = Guid.NewGuid().ToString();

            using (var transactionContext = new DefaultTransactionContextScope())
            {
                var headers = new Dictionary <string, string>
                {
                    { Headers.MessageId, Guid.NewGuid().ToString() },
                    { "recognizzle", id },
                    { Headers.TimeToBeReceived, "00:00:20" },
                    { Headers.SentTime, DateTimeOffset.UtcNow.ToString("O") }//< expires after 10 seconds!
                };
                await transport.Send(queueName, MessageWith(headers), AmbientTransactionContext.Current);

                await transactionContext.Complete();
            }

            await Task.Delay(3000);

            using (var transactionContext = new DefaultTransactionContextScope())
            {
                var transportMessage = await transport.Receive(AmbientTransactionContext.Current, _cancellationToken);

                await transactionContext.Complete();

                Assert.That(transportMessage, Is.Not.Null);
            }
        }
Esempio n. 7
0
        protected async Task WithContext(Func <ITransactionContext, Task> contextAction, bool completeTransaction = true)
        {
            using (var context = new DefaultTransactionContextScope())
            {
                await contextAction(AmbientTransactionContext.Current);

                if (completeTransaction)
                {
                    await context.Complete();
                }
            }
        }
Esempio n. 8
0
        public void Run()
        {
            Text.PrintLine("Will start receiving messages from '{0}'", InputQueue);

            if (DefaultOutputQueue != null)
            {
                Text.PrintLine("(will provide '{0}' as the default queue to forward messages to)", DefaultOutputQueue);
            }

            Text.PrintLine();

            while (true)
            {
                using (var transactionContext = new DefaultTransactionContextScope())
                {
                    var transportMessage = _transport.Receive(AmbientTransactionContext.Current, new CancellationTokenSource().Token).Result;

                    if (transportMessage == null)
                    {
                        break;
                    }

                    try
                    {
                        HandleMessage(transportMessage, AmbientTransactionContext.Current);

                        transactionContext.Complete().Wait();
                    }
                    catch (Exception exception)
                    {
                        Text.PrintLine("Failed: {0}", exception.Message);
                    }
                }

                Text.PrintLine("No more messages");
            }
        }