diff --git a/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsPointToPointSubscriber/WhenAFailoverRegionIsSetup.cs b/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsPointToPointSubscriber/WhenAFailoverRegionIsSetup.cs index 2155006c1..e9cdc217d 100644 --- a/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsPointToPointSubscriber/WhenAFailoverRegionIsSetup.cs +++ b/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsPointToPointSubscriber/WhenAFailoverRegionIsSetup.cs @@ -118,7 +118,6 @@ private async Task AndAMessageIsPublished() _message = new SimpleMessage { Id = Guid.NewGuid() }; await _publisher.PublishAsync(_message); - await Task.Yield(); await Task.Delay(TimeSpan.FromSeconds(1)); } diff --git a/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsPointToPointSubscriber/WhenOneBusIsSubscribedToTwoRegions.cs b/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsPointToPointSubscriber/WhenOneBusIsSubscribedToTwoRegions.cs index 3ee0d8d17..4ed211259 100644 --- a/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsPointToPointSubscriber/WhenOneBusIsSubscribedToTwoRegions.cs +++ b/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsPointToPointSubscriber/WhenOneBusIsSubscribedToTwoRegions.cs @@ -99,7 +99,6 @@ private async Task WhenMessagesArePublishedToBothRegions() await _primaryPublisher.PublishAsync(_message1); await _secondaryPublisher.PublishAsync(_message2); - await Task.Yield(); await Task.Delay(TimeSpan.FromSeconds(1)); } diff --git a/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsTopicSubscriber/WhenAFailoverRegionIsSetup.cs b/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsTopicSubscriber/WhenAFailoverRegionIsSetup.cs index 96b7abb46..1e62eed3c 100644 --- a/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsTopicSubscriber/WhenAFailoverRegionIsSetup.cs +++ b/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsTopicSubscriber/WhenAFailoverRegionIsSetup.cs @@ -118,7 +118,6 @@ private async Task AndAMessageIsPublished() _message = new SimpleMessage { Id = Guid.NewGuid() }; await _publisher.PublishAsync(_message); - await Task.Yield(); await Task.Delay(TimeSpan.FromSeconds(1)); } diff --git a/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsTopicSubscriber/WhenOneBusIsSubscribedToTwoRegions.cs b/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsTopicSubscriber/WhenOneBusIsSubscribedToTwoRegions.cs index 4e87e7e58..700b93259 100644 --- a/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsTopicSubscriber/WhenOneBusIsSubscribedToTwoRegions.cs +++ b/JustSaying.IntegrationTests/JustSayingFluently/MultiRegion/WithSqsTopicSubscriber/WhenOneBusIsSubscribedToTwoRegions.cs @@ -99,7 +99,6 @@ private async Task WhenMessagesArePublishedToBothRegions() await _primaryPublisher.PublishAsync(_message1); await _secondaryPublisher.PublishAsync(_message2); - await Task.Yield(); await Task.Delay(TimeSpan.FromSeconds(1)); } diff --git a/JustSaying.TestingFramework/Tasks.cs b/JustSaying.TestingFramework/Tasks.cs index cb39cbf29..d31d4f542 100644 --- a/JustSaying.TestingFramework/Tasks.cs +++ b/JustSaying.TestingFramework/Tasks.cs @@ -1,33 +1,32 @@ -using System; -using System.Threading.Tasks; - -namespace JustSaying.TestingFramework -{ - public static class Tasks - { - private const int DefaultTimeoutMillis = 10000; - private const int DelaySendMillis = 200; - - public static async Task WaitWithTimeoutAsync(Task task) - => await WaitWithTimeoutAsync(task, TimeSpan.FromMilliseconds(DefaultTimeoutMillis)) - .ConfigureAwait(false); - - public static async Task WaitWithTimeoutAsync(Task task, TimeSpan timeoutDuration) - { - var timeoutTask = Task.Delay(timeoutDuration); - var firstToComplete = await Task.WhenAny(task, timeoutTask).ConfigureAwait(false); - - if (firstToComplete != timeoutTask) return true; - return false; - } - public static void DelaySendDone(TaskCompletionSource doneSignal) - { - Task.Run(async () => - { - await Task.Yield(); - await Task.Delay(DelaySendMillis).ConfigureAwait(false); - doneSignal.SetResult(null); - }); - } - } -} +using System; +using System.Threading.Tasks; + +namespace JustSaying.TestingFramework +{ + public static class Tasks + { + private const int DefaultTimeoutMillis = 10000; + private const int DelaySendMillis = 200; + + public static async Task WaitWithTimeoutAsync(Task task) + => await WaitWithTimeoutAsync(task, TimeSpan.FromMilliseconds(DefaultTimeoutMillis)) + .ConfigureAwait(false); + + public static async Task WaitWithTimeoutAsync(Task task, TimeSpan timeoutDuration) + { + var timeoutTask = Task.Delay(timeoutDuration); + var firstToComplete = await Task.WhenAny(task, timeoutTask).ConfigureAwait(false); + + if (firstToComplete != timeoutTask) return true; + return false; + } + public static void DelaySendDone(TaskCompletionSource doneSignal) + { + Task.Run(async () => + { + await Task.Delay(DelaySendMillis).ConfigureAwait(false); + doneSignal.SetResult(null); + }); + } + } +} diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/Support/ThrowingDuringMessageProcessingStrategy.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/Support/ThrowingDuringMessageProcessingStrategy.cs index 3f0d03041..834352736 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/Support/ThrowingDuringMessageProcessingStrategy.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/Support/ThrowingDuringMessageProcessingStrategy.cs @@ -20,9 +20,9 @@ public ThrowingDuringMessageProcessingStrategy(TaskCompletionSource done _doneSignal = doneSignal; } - public async Task WaitForAvailableWorkers() + public Task WaitForAvailableWorkers() { - await Task.Yield(); + return Task.CompletedTask; } public Task StartWorker(Func action, CancellationToken cancellationToken) diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenExactlyOnceIsAppliedToHandler.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenExactlyOnceIsAppliedToHandler.cs index 11309ce41..009a15114 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenExactlyOnceIsAppliedToHandler.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenExactlyOnceIsAppliedToHandler.cs @@ -1,66 +1,64 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using JustSaying.Messaging.MessageHandling; -using JustSaying.TestingFramework; -using JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener.Support; -using NSubstitute; -using Shouldly; -using Xunit; - -namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener -{ - public class WhenExactlyOnceIsAppliedToHandler : BaseQueuePollingTest - { - private int _expectedtimeout; - private readonly TaskCompletionSource _tcs = new TaskCompletionSource(); - private ExplicitExactlyOnceSignallingHandler _handler; - - protected override async Task Given() - { - await base.Given(); - _expectedtimeout = 5; - - var messageLockResponse = new MessageLockResponse - { - DoIHaveExclusiveLock = true - }; - - MessageLock = Substitute.For(); - MessageLock.TryAquireLockAsync(Arg.Any(), Arg.Any()) - .Returns(messageLockResponse); - - _handler = new ExplicitExactlyOnceSignallingHandler(_tcs); - Handler = _handler; - } - - protected override async Task When() - { - SystemUnderTest.AddMessageHandler(() => Handler); - var cts = new CancellationTokenSource(); - SystemUnderTest.Listen(cts.Token); - - - // wait until it's done - await Tasks.WaitWithTimeoutAsync(_tcs.Task); - cts.Cancel(); - await Task.Yield(); - } - - [Fact] - public void ProcessingIsPassedToTheHandler() - { - _handler.HandleWasCalled.ShouldBeTrue(); - } - - [Fact] - public async Task MessageIsLocked() - { - var messageId = DeserialisedMessage.Id.ToString(); - - await MessageLock.Received().TryAquireLockAsync( - Arg.Is(a => a.Contains(messageId)), - TimeSpan.FromSeconds(_expectedtimeout)); - } - } -} +using System; +using System.Threading; +using System.Threading.Tasks; +using JustSaying.Messaging.MessageHandling; +using JustSaying.TestingFramework; +using JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener.Support; +using NSubstitute; +using Shouldly; +using Xunit; + +namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener +{ + public class WhenExactlyOnceIsAppliedToHandler : BaseQueuePollingTest + { + private int _expectedtimeout; + private readonly TaskCompletionSource _tcs = new TaskCompletionSource(); + private ExplicitExactlyOnceSignallingHandler _handler; + + protected override async Task Given() + { + await base.Given(); + _expectedtimeout = 5; + + var messageLockResponse = new MessageLockResponse + { + DoIHaveExclusiveLock = true + }; + + MessageLock = Substitute.For(); + MessageLock.TryAquireLockAsync(Arg.Any(), Arg.Any()) + .Returns(messageLockResponse); + + _handler = new ExplicitExactlyOnceSignallingHandler(_tcs); + Handler = _handler; + } + + protected override async Task When() + { + SystemUnderTest.AddMessageHandler(() => Handler); + var cts = new CancellationTokenSource(); + SystemUnderTest.Listen(cts.Token); + + // wait until it's done + await Tasks.WaitWithTimeoutAsync(_tcs.Task); + cts.Cancel(); + } + + [Fact] + public void ProcessingIsPassedToTheHandler() + { + _handler.HandleWasCalled.ShouldBeTrue(); + } + + [Fact] + public async Task MessageIsLocked() + { + var messageId = DeserialisedMessage.Id.ToString(); + + await MessageLock.Received().TryAquireLockAsync( + Arg.Is(a => a.Contains(messageId)), + TimeSpan.FromSeconds(_expectedtimeout)); + } + } +} diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenExactlyOnceIsAppliedToHandlerWithoutExplicitTimeout.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenExactlyOnceIsAppliedToHandlerWithoutExplicitTimeout.cs index e0434f9ff..6d1243156 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenExactlyOnceIsAppliedToHandlerWithoutExplicitTimeout.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenExactlyOnceIsAppliedToHandlerWithoutExplicitTimeout.cs @@ -1,65 +1,64 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using JustSaying.Messaging.MessageHandling; -using JustSaying.TestingFramework; -using JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener.Support; -using NSubstitute; -using Shouldly; -using Xunit; - -namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener -{ - public class WhenExactlyOnceIsAppliedWithoutSpecificTimeout : BaseQueuePollingTest - { - //private readonly int _maximumTimeout = int.MaxValue; - private readonly int _maximumTimeout = (int)TimeSpan.MaxValue.TotalSeconds; - private readonly TaskCompletionSource _tcs = new TaskCompletionSource(); - private ExactlyOnceSignallingHandler _handler; - - protected override async Task Given() - { - await base.Given(); - - var messageLockResponse = new MessageLockResponse - { - DoIHaveExclusiveLock = true - }; - - MessageLock = Substitute.For(); - MessageLock.TryAquireLockAsync(Arg.Any(), Arg.Any()) - .Returns(messageLockResponse); - - _handler = new ExactlyOnceSignallingHandler(_tcs); - Handler = _handler; - } - - protected override async Task When() - { - SystemUnderTest.AddMessageHandler(() => Handler); - var cts = new CancellationTokenSource(); - SystemUnderTest.Listen(cts.Token); - - // wait until it's done - await Tasks.WaitWithTimeoutAsync(_tcs.Task); - cts.Cancel(); - await Task.Yield(); - } - - [Fact] - public void MessageIsLocked() - { - var messageId = DeserialisedMessage.Id.ToString(); - - MessageLock.Received().TryAquireLockAsync( - Arg.Is(a => a.Contains(messageId)), - TimeSpan.FromSeconds(_maximumTimeout)); - } - - [Fact] - public void ProcessingIsPassedToTheHandler() - { - _handler.HandleWasCalled.ShouldBeTrue(); - } - } -} +using System; +using System.Threading; +using System.Threading.Tasks; +using JustSaying.Messaging.MessageHandling; +using JustSaying.TestingFramework; +using JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener.Support; +using NSubstitute; +using Shouldly; +using Xunit; + +namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener +{ + public class WhenExactlyOnceIsAppliedWithoutSpecificTimeout : BaseQueuePollingTest + { + //private readonly int _maximumTimeout = int.MaxValue; + private readonly int _maximumTimeout = (int)TimeSpan.MaxValue.TotalSeconds; + private readonly TaskCompletionSource _tcs = new TaskCompletionSource(); + private ExactlyOnceSignallingHandler _handler; + + protected override async Task Given() + { + await base.Given(); + + var messageLockResponse = new MessageLockResponse + { + DoIHaveExclusiveLock = true + }; + + MessageLock = Substitute.For(); + MessageLock.TryAquireLockAsync(Arg.Any(), Arg.Any()) + .Returns(messageLockResponse); + + _handler = new ExactlyOnceSignallingHandler(_tcs); + Handler = _handler; + } + + protected override async Task When() + { + SystemUnderTest.AddMessageHandler(() => Handler); + var cts = new CancellationTokenSource(); + SystemUnderTest.Listen(cts.Token); + + // wait until it's done + await Tasks.WaitWithTimeoutAsync(_tcs.Task); + cts.Cancel(); + } + + [Fact] + public void MessageIsLocked() + { + var messageId = DeserialisedMessage.Id.ToString(); + + MessageLock.Received().TryAquireLockAsync( + Arg.Is(a => a.Contains(messageId)), + TimeSpan.FromSeconds(_maximumTimeout)); + } + + [Fact] + public void ProcessingIsPassedToTheHandler() + { + _handler.HandleWasCalled.ShouldBeTrue(); + } + } +} diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenListeningStartsAndStops.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenListeningStartsAndStops.cs index 654a25c41..790a2c254 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenListeningStartsAndStops.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenListeningStartsAndStops.cs @@ -1,85 +1,83 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using Amazon.SQS.Model; -using JustSaying.Messaging.MessageProcessingStrategies; -using NSubstitute; -using Xunit; - -namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener -{ - public class WhenListeningStartsAndStops : BaseQueuePollingTest - { - private const string SubjectOfMessageAfterStop = @"POST_STOP_MESSAGE"; - private const string BodyOfMessageAfterStop = @"{""Subject"":""POST_STOP_MESSAGE"",""Message"":""object""}"; - - private int expectedMaxMessageCount; - - protected override async Task Given() - { - await base.Given(); - - // we expect to get max 10 messages per batch - // except on single-core machines when we top out at ParallelHandlerExecutionPerCore=8 - expectedMaxMessageCount = Math.Min(MessageConstants.MaxAmazonMessageCap, - Environment.ProcessorCount * MessageConstants.ParallelHandlerExecutionPerCore); - - var response1 = GenerateResponseMessage(SubjectOfMessageAfterStop, Guid.NewGuid()); - var response2 = new ReceiveMessageResponse - { - Messages = new List() - }; - - Sqs.ReceiveMessageAsync( - Arg.Any(), - Arg.Any()) - .Returns( - _ => response1, - _ => response2); - } - - protected override async Task When() - { - await base.When(); - - var cts = new CancellationTokenSource(); - SystemUnderTest.Listen(cts.Token); - await Task.Yield(); - - cts.Cancel(); - await Task.Yield(); - } - - [Fact] - public void MessagesAreReceived() - { - Sqs.Received().ReceiveMessageAsync( - Arg.Any(), - Arg.Any()); - } - - [Fact] - public void CorrectQueueIsPolled() - { - Sqs.Received().ReceiveMessageAsync( - Arg.Is(x => x.QueueUrl == QueueUrl), - Arg.Any()); - } - - [Fact] - public void TheMaxMessageAllowanceIsGrabbed() - { - Sqs.Received().ReceiveMessageAsync( - Arg.Is(x => x.MaxNumberOfMessages == expectedMaxMessageCount), - Arg.Any()); - } - - [Fact] - public void MessageIsProcessed() - { - SerialisationRegister.Received().DeserializeMessage( - BodyOfMessageAfterStop); - } - } -} +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Amazon.SQS.Model; +using JustSaying.Messaging.MessageProcessingStrategies; +using NSubstitute; +using Xunit; + +namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener +{ + public class WhenListeningStartsAndStops : BaseQueuePollingTest + { + private const string SubjectOfMessageAfterStop = @"POST_STOP_MESSAGE"; + private const string BodyOfMessageAfterStop = @"{""Subject"":""POST_STOP_MESSAGE"",""Message"":""object""}"; + + private int expectedMaxMessageCount; + + protected override async Task Given() + { + await base.Given(); + + // we expect to get max 10 messages per batch + // except on single-core machines when we top out at ParallelHandlerExecutionPerCore=8 + expectedMaxMessageCount = Math.Min(MessageConstants.MaxAmazonMessageCap, + Environment.ProcessorCount * MessageConstants.ParallelHandlerExecutionPerCore); + + var response1 = GenerateResponseMessage(SubjectOfMessageAfterStop, Guid.NewGuid()); + var response2 = new ReceiveMessageResponse + { + Messages = new List() + }; + + Sqs.ReceiveMessageAsync( + Arg.Any(), + Arg.Any()) + .Returns( + _ => response1, + _ => response2); + } + + protected override async Task When() + { + await base.When(); + + var cts = new CancellationTokenSource(); + SystemUnderTest.Listen(cts.Token); + + cts.Cancel(); + } + + [Fact] + public void MessagesAreReceived() + { + Sqs.Received().ReceiveMessageAsync( + Arg.Any(), + Arg.Any()); + } + + [Fact] + public void CorrectQueueIsPolled() + { + Sqs.Received().ReceiveMessageAsync( + Arg.Is(x => x.QueueUrl == QueueUrl), + Arg.Any()); + } + + [Fact] + public void TheMaxMessageAllowanceIsGrabbed() + { + Sqs.Received().ReceiveMessageAsync( + Arg.Is(x => x.MaxNumberOfMessages == expectedMaxMessageCount), + Arg.Any()); + } + + [Fact] + public void MessageIsProcessed() + { + SerialisationRegister.Received().DeserializeMessage( + BodyOfMessageAfterStop); + } + } +} diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenMessageProcessingThrowsBefore.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenMessageProcessingThrowsBefore.cs index a2d6e2075..e7f12447e 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenMessageProcessingThrowsBefore.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenMessageProcessingThrowsBefore.cs @@ -1,56 +1,55 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Amazon.SQS.Model; -using JustSaying.TestingFramework; -using JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener.Support; -using NSubstitute; -using Xunit; - -namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener -{ - /// - /// this test exercises different exception handlers to the "handler throws an exception" path in WhenMessageHandlingThrows - /// - public class WhenMessageProcessingThrowsBefore : BaseQueuePollingTest - { - protected override async Task Given() - { - await base.Given(); - Handler.Handle(null).ReturnsForAnyArgs(true); - } - - protected override async Task When() - { - var doneSignal = new TaskCompletionSource(); - SystemUnderTest.WithMessageProcessingStrategy(new ThrowingBeforeMessageProcessingStrategy(doneSignal)); - - SystemUnderTest.AddMessageHandler(() => Handler); - var cts = new CancellationTokenSource(); - SystemUnderTest.Listen(cts.Token); - - await Tasks.WaitWithTimeoutAsync(doneSignal.Task); - - cts.Cancel(); - await Task.Yield(); - } - - [Fact] - public void MessageHandlerWasNotCalled() - { - Handler.DidNotReceiveWithAnyArgs().Handle(Arg.Any()); - } - - [Fact] - public void FailedMessageIsNotRemovedFromQueue() - { - Sqs.DidNotReceiveWithAnyArgs().DeleteMessageAsync(Arg.Any()); - } - - [Fact] - public void ExceptionIsLoggedToMonitor() - { - Monitor.DidNotReceiveWithAnyArgs().HandleException(Arg.Any()); - } - } -} +using System; +using System.Threading; +using System.Threading.Tasks; +using Amazon.SQS.Model; +using JustSaying.TestingFramework; +using JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener.Support; +using NSubstitute; +using Xunit; + +namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener +{ + /// + /// this test exercises different exception handlers to the "handler throws an exception" path in WhenMessageHandlingThrows + /// + public class WhenMessageProcessingThrowsBefore : BaseQueuePollingTest + { + protected override async Task Given() + { + await base.Given(); + Handler.Handle(null).ReturnsForAnyArgs(true); + } + + protected override async Task When() + { + var doneSignal = new TaskCompletionSource(); + SystemUnderTest.WithMessageProcessingStrategy(new ThrowingBeforeMessageProcessingStrategy(doneSignal)); + + SystemUnderTest.AddMessageHandler(() => Handler); + var cts = new CancellationTokenSource(); + SystemUnderTest.Listen(cts.Token); + + await Tasks.WaitWithTimeoutAsync(doneSignal.Task); + + cts.Cancel(); + } + + [Fact] + public void MessageHandlerWasNotCalled() + { + Handler.DidNotReceiveWithAnyArgs().Handle(Arg.Any()); + } + + [Fact] + public void FailedMessageIsNotRemovedFromQueue() + { + Sqs.DidNotReceiveWithAnyArgs().DeleteMessageAsync(Arg.Any()); + } + + [Fact] + public void ExceptionIsLoggedToMonitor() + { + Monitor.DidNotReceiveWithAnyArgs().HandleException(Arg.Any()); + } + } +} diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenMessageProcessingThrowsDuring.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenMessageProcessingThrowsDuring.cs index 1e9519b0c..df2124c7a 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenMessageProcessingThrowsDuring.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenMessageProcessingThrowsDuring.cs @@ -1,56 +1,55 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Amazon.SQS.Model; -using JustSaying.TestingFramework; -using JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener.Support; -using NSubstitute; -using Xunit; - -namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener -{ - /// - /// this test exercises different exception handlers to the "handler throws an exception" path in WhenMessageHandlingThrows - /// - public class WhenMessageProcessingThrowsDuring : BaseQueuePollingTest - { - protected override async Task Given() - { - await base.Given(); - Handler.Handle(null).ReturnsForAnyArgs(true); - } - - protected override async Task When() - { - var doneSignal = new TaskCompletionSource(); - SystemUnderTest.WithMessageProcessingStrategy(new ThrowingDuringMessageProcessingStrategy(doneSignal)); - - SystemUnderTest.AddMessageHandler(() => Handler); - var cts = new CancellationTokenSource(); - SystemUnderTest.Listen(cts.Token); - - await Tasks.WaitWithTimeoutAsync(doneSignal.Task); - - cts.Cancel(); - await Task.Yield(); - } - - [Fact] - public void MessageHandlerWasNotCalled() - { - Handler.DidNotReceiveWithAnyArgs().Handle(Arg.Any()); - } - - [Fact] - public void FailedMessageIsNotRemovedFromQueue() - { - Sqs.DidNotReceiveWithAnyArgs().DeleteMessageAsync(Arg.Any()); - } - - [Fact] - public void ExceptionIsLoggedToMonitor() - { - Monitor.DidNotReceiveWithAnyArgs().HandleException(Arg.Any()); - } - } -} +using System; +using System.Threading; +using System.Threading.Tasks; +using Amazon.SQS.Model; +using JustSaying.TestingFramework; +using JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener.Support; +using NSubstitute; +using Xunit; + +namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener +{ + /// + /// this test exercises different exception handlers to the "handler throws an exception" path in WhenMessageHandlingThrows + /// + public class WhenMessageProcessingThrowsDuring : BaseQueuePollingTest + { + protected override async Task Given() + { + await base.Given(); + Handler.Handle(null).ReturnsForAnyArgs(true); + } + + protected override async Task When() + { + var doneSignal = new TaskCompletionSource(); + SystemUnderTest.WithMessageProcessingStrategy(new ThrowingDuringMessageProcessingStrategy(doneSignal)); + + SystemUnderTest.AddMessageHandler(() => Handler); + var cts = new CancellationTokenSource(); + SystemUnderTest.Listen(cts.Token); + + await Tasks.WaitWithTimeoutAsync(doneSignal.Task); + + cts.Cancel(); + } + + [Fact] + public void MessageHandlerWasNotCalled() + { + Handler.DidNotReceiveWithAnyArgs().Handle(Arg.Any()); + } + + [Fact] + public void FailedMessageIsNotRemovedFromQueue() + { + Sqs.DidNotReceiveWithAnyArgs().DeleteMessageAsync(Arg.Any()); + } + + [Fact] + public void ExceptionIsLoggedToMonitor() + { + Monitor.DidNotReceiveWithAnyArgs().HandleException(Arg.Any()); + } + } +} diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreExceptionsInMessageProcessing.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreExceptionsInMessageProcessing.cs index 416cdef7f..06689d007 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreExceptionsInMessageProcessing.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreExceptionsInMessageProcessing.cs @@ -57,7 +57,6 @@ protected override async Task When() SystemUnderTest.Listen(cts.Token); await Task.Delay(100); cts.Cancel(); - await Task.Yield(); } [Fact] diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreExceptionsInSqsCalling.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreExceptionsInSqsCalling.cs index 7a5b8110d..274acb1fd 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreExceptionsInSqsCalling.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreExceptionsInSqsCalling.cs @@ -1,74 +1,73 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using Amazon.SQS; -using Amazon.SQS.Model; -using JustSaying.Messaging.MessageHandling; -using JustSaying.Messaging.MessageSerialisation; -using JustSaying.Messaging.Monitoring; -using JustSaying.TestingFramework; -using Microsoft.Extensions.Logging; -using NSubstitute; -using Shouldly; -using Xunit; - -namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener -{ - public class WhenThereAreExceptionsInSqsCalling : BaseQueuePollingTest - { - private int _sqsCallCounter; - private readonly TaskCompletionSource _tcs = new TaskCompletionSource(); - - protected override Task Given() - { - Sqs = Substitute.For(); - SerialisationRegister = Substitute.For(); - Monitor = Substitute.For(); - Handler = Substitute.For>(); - LoggerFactory = Substitute.For(); - GenerateResponseMessage(MessageTypeString, Guid.NewGuid()); - - DeserialisedMessage = new SimpleMessage { RaisingComponent = "Component" }; - - Sqs.ReceiveMessageAsync( - Arg.Any(), - Arg.Any()) - .Returns(_ => ExceptionOnFirstCall()); - - return Task.CompletedTask; - } - - private Task ExceptionOnFirstCall() - { - _sqsCallCounter++; - if (_sqsCallCounter == 1) - { - throw new TestException("testing the failure on first call"); - } - if (_sqsCallCounter == 2) - { - Tasks.DelaySendDone(_tcs); - } - - return Task.FromResult(new ReceiveMessageResponse()); - } - - protected override async Task When() - { - SystemUnderTest.AddMessageHandler(() => Handler); - var cts = new CancellationTokenSource(); - SystemUnderTest.Listen(cts.Token); - - // wait until it's done - await Tasks.WaitWithTimeoutAsync(_tcs.Task); - cts.Cancel(); - await Task.Yield(); - } - - [Fact] - public void QueueIsPolledMoreThanOnce() - { - _sqsCallCounter.ShouldBeGreaterThan(1); - } - } -} +using System; +using System.Threading; +using System.Threading.Tasks; +using Amazon.SQS; +using Amazon.SQS.Model; +using JustSaying.Messaging.MessageHandling; +using JustSaying.Messaging.MessageSerialisation; +using JustSaying.Messaging.Monitoring; +using JustSaying.TestingFramework; +using Microsoft.Extensions.Logging; +using NSubstitute; +using Shouldly; +using Xunit; + +namespace JustSaying.UnitTests.AwsTools.MessageHandling.SqsNotificationListener +{ + public class WhenThereAreExceptionsInSqsCalling : BaseQueuePollingTest + { + private int _sqsCallCounter; + private readonly TaskCompletionSource _tcs = new TaskCompletionSource(); + + protected override Task Given() + { + Sqs = Substitute.For(); + SerialisationRegister = Substitute.For(); + Monitor = Substitute.For(); + Handler = Substitute.For>(); + LoggerFactory = Substitute.For(); + GenerateResponseMessage(MessageTypeString, Guid.NewGuid()); + + DeserialisedMessage = new SimpleMessage { RaisingComponent = "Component" }; + + Sqs.ReceiveMessageAsync( + Arg.Any(), + Arg.Any()) + .Returns(_ => ExceptionOnFirstCall()); + + return Task.CompletedTask; + } + + private Task ExceptionOnFirstCall() + { + _sqsCallCounter++; + if (_sqsCallCounter == 1) + { + throw new TestException("testing the failure on first call"); + } + if (_sqsCallCounter == 2) + { + Tasks.DelaySendDone(_tcs); + } + + return Task.FromResult(new ReceiveMessageResponse()); + } + + protected override async Task When() + { + SystemUnderTest.AddMessageHandler(() => Handler); + var cts = new CancellationTokenSource(); + SystemUnderTest.Listen(cts.Token); + + // wait until it's done + await Tasks.WaitWithTimeoutAsync(_tcs.Task); + cts.Cancel(); + } + + [Fact] + public void QueueIsPolledMoreThanOnce() + { + _sqsCallCounter.ShouldBeGreaterThan(1); + } + } +} diff --git a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreNoMessagesToProcess.cs b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreNoMessagesToProcess.cs index 944d5dab3..63d5eb847 100644 --- a/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreNoMessagesToProcess.cs +++ b/JustSaying.UnitTests/AwsTools/MessageHandling/SqsNotificationListener/WhenThereAreNoMessagesToProcess.cs @@ -50,7 +50,6 @@ protected override async Task When() SystemUnderTest.Listen(cts.Token); await Task.Delay(100); cts.Cancel(); - await Task.Yield(); } [Fact] diff --git a/JustSaying.UnitTests/Messaging/MessageProcessingStrategies/MessageLoopTests.cs b/JustSaying.UnitTests/Messaging/MessageProcessingStrategies/MessageLoopTests.cs index 59f04dfef..e571ccceb 100644 --- a/JustSaying.UnitTests/Messaging/MessageProcessingStrategies/MessageLoopTests.cs +++ b/JustSaying.UnitTests/Messaging/MessageProcessingStrategies/MessageLoopTests.cs @@ -51,9 +51,7 @@ public async Task SimulatedListenLoop_ProcessedAllMessages(int numberOfMessagesT watch.Stop(); - await Task.Yield(); await Task.Delay(2000); - await Task.Yield(); counter.Count.ShouldBe(numberOfMessagesToProcess); } diff --git a/JustSaying.UnitTests/Messaging/MessageProcessingStrategies/ThrottledTests.cs b/JustSaying.UnitTests/Messaging/MessageProcessingStrategies/ThrottledTests.cs index efc02a33e..2e448f782 100644 --- a/JustSaying.UnitTests/Messaging/MessageProcessingStrategies/ThrottledTests.cs +++ b/JustSaying.UnitTests/Messaging/MessageProcessingStrategies/ThrottledTests.cs @@ -130,7 +130,6 @@ public async Task AvailableWorkers_IsNeverNegative() private static async Task AllowTasksToComplete(TaskCompletionSource doneSignal) { doneSignal.SetResult(null); - await Task.Yield(); await Task.Delay(100); } }