Esempio n. 1
0
        static string GetMessagesAsText(ConcurrentList <Tuple <DateTime, DateTime, int> > receivedMessages, ConcurrentList <MessageWithExpectedReturnTime> sentMessages)
        {
            var receivedMessagesAsText =
                string.Join(Environment.NewLine,
                            receivedMessages
                            .Select(m => string.Join(";",
                                                     new[]
            {
                m.Item3.ToString(),
                m.Item1.ToString(),
                m.Item2.ToString(),
            })));

            var receivedMessageIds = receivedMessages
                                     .Select(m => m.Item3)
                                     .ToList();

            var sentButNotReceivedMessages = sentMessages
                                             .Where(m => !receivedMessageIds.Contains(m.MessageId))
                                             .ToList();

            return(string.Format(@"Received messages:
{0}

Sent, but not received messages:
{1}", receivedMessagesAsText,
                                 string.Join(Environment.NewLine, sentButNotReceivedMessages.Select(m => m.MessageId))));
        }
Esempio n. 2
0
        public void WorksReliablyWithManyTimeouts(int messageCount, int acceptedToleranceSec)
        {
            // arrange
            var receivedMessages = new ConcurrentList <Tuple <DateTime, DateTime, int> >();
            var sentMessages     = new ConcurrentList <MessageWithExpectedReturnTime>();

            var resetEvent = new ManualResetEvent(false);

            handlerActivator
            .Handle <MessageWithExpectedReturnTime>(
                m =>
            {
                receivedMessages.Add(Tuple.Create(m.ExpectedReturnTime, DateTime.UtcNow, m.MessageId));

                var currentCount = receivedMessages.Count;

                if (currentCount >= messageCount)
                {
                    Console.WriteLine("Got {0} messages, setting reset event", currentCount);
                    resetEvent.Set();
                }
            });

            var acceptedTolerance = acceptedToleranceSec.Seconds();
            var random            = new Random();
            var number            = 0;

            // act
            messageCount.Times(() =>
            {
                var delay   = (random.Next(20) + 10).Seconds();
                var message = new MessageWithExpectedReturnTime
                {
                    ExpectedReturnTime = DateTime.UtcNow + delay,
                    MessageId          = number++
                };
                bus.Defer(delay, message);
                sentMessages.Add(message);
            });

            using (var timer = new Timer())
            {
                timer.Elapsed += (o, ea) => Console.WriteLine("{0}: got {1} messages", DateTime.UtcNow, receivedMessages.Count);
                timer.Interval = 3000;
                timer.Start();

                if (!resetEvent.WaitOne(150.Seconds()))
                {
                    Assert.Fail(@"Only {0} messages were received within the 45 s timeout!!

Here they are:
{1}",
                                receivedMessages.Count, GetMessagesAsText(receivedMessages, sentMessages));
                }

                // just wait a while, to be sure that more messages are not arriving
                Thread.Sleep(1000);
            }

            // assert
            if (receivedMessages.Count != sentMessages.Count)
            {
                Assert.Fail(@"Expected {0} messages to have been received, but we got {1}!!

Here they are:
{1}", sentMessages.Count, receivedMessages.Count, GetMessagesAsText(receivedMessages, sentMessages));
            }

            foreach (var messageTimes in receivedMessages)
            {
                var lowerBound = messageTimes.Item1 - acceptedTolerance;
                var upperBound = messageTimes.Item1 + acceptedTolerance;

                if (messageTimes.Item2 <= lowerBound || messageTimes.Item2 >= upperBound)
                {
                    Assert.Fail("Something is wrong with message # {0} - the time of receiving it ({1}) was outside accepted bounds {2} - {3} - the timeout was set to {4}",
                                messageTimes.Item3, messageTimes.Item2, lowerBound, upperBound, messageTimes.Item1);
                }
            }
        }