public void WorksReliablyWithMoreTimeouts() { // arrange var messages = new List <Tuple <DateTime, DateTime> >(); handlerActivator.Handle <MessageWithExpectedReturnTime>(m => messages.Add(new Tuple <DateTime, DateTime>(m.ExpectedReturnTime, DateTime.UtcNow))); var acceptedTolerance = 2.Seconds(); var random = new Random(); // act 500.Times(() => { var delay = (random.Next(20) + 10).Seconds(); var message = new MessageWithExpectedReturnTime { ExpectedReturnTime = DateTime.UtcNow + delay }; bus.Defer(delay, message); }); Thread.Sleep(30.Seconds() + acceptedTolerance + acceptedTolerance); // assert messages.Count.ShouldBe(500); foreach (var messageTimes in messages) { messageTimes.Item2.ShouldBeGreaterThan(messageTimes.Item1 - acceptedTolerance); messageTimes.Item2.ShouldBeLessThan(messageTimes.Item1 + acceptedTolerance); } }
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); } } }