public void Initialize()
        {
            mockNotificationHandler = new Mock <INotificationHandler <TestNotification> >();
            mockNotificationHandler.Setup(p => p.HandleAsync(It.IsAny <TestNotification>(), It.IsAny <ILambdaContext>()))
            .Returns(Task.CompletedTask);

            mockServiceScope = new Mock <IServiceScope>();

            mockServiceScopeFactory = new Mock <IServiceScopeFactory>();
            mockServiceScopeFactory.Setup(p => p.CreateScope()).Returns(mockServiceScope.Object);

            mockServiceProvider = new Mock <IServiceProvider>();
            mockServiceProvider.Setup(p => p.GetService(typeof(INotificationHandler <TestNotification>)))
            .Returns(mockNotificationHandler.Object);
            mockServiceProvider.Setup(p => p.GetService(typeof(IServiceScopeFactory)))
            .Returns(mockServiceScopeFactory.Object);

            mockServiceScope.Setup(p => p.ServiceProvider).Returns(mockServiceProvider.Object);


            mockLoggerFactory = new Mock <ILoggerFactory>();
            mockLoggerFactory.Setup(p => p.CreateLogger(It.IsAny <string>()))
            .Returns(Mock.Of <ILogger>());

            parallelExecutionOptions = new ParallelSnsExecutionOptions {
                MaxDegreeOfParallelism = 4
            };
        }
        public void MaxDegreeOfParallelism_Should_ProperlyPropagated_And_Limited_To_Set_Max()
        {
            var snsEvent = new SNSEvent
            {
                Records = new List <SNSEvent.SNSRecord>
                {
                    new SNSEvent.SNSRecord
                    {
                        Sns = new SNSEvent.SNSMessage
                        {
                            Message = "{}"
                        }
                    },
                    new SNSEvent.SNSRecord
                    {
                        Sns = new SNSEvent.SNSMessage
                        {
                            Message = "{}"
                        }
                    },
                    new SNSEvent.SNSRecord
                    {
                        Sns = new SNSEvent.SNSMessage
                        {
                            Message = "{}"
                        }
                    },
                    new SNSEvent.SNSRecord
                    {
                        Sns = new SNSEvent.SNSMessage
                        {
                            Message = "{}"
                        }
                    }
                }
            };

            var cq = new ConcurrentQueue <Task>();

            //We are checking if parallelism actually does what it's supposed to do. So we should have more then 2 concurrent processes running
            parallelExecutionOptions = new ParallelSnsExecutionOptions {
                MaxDegreeOfParallelism = 4
            };
            mockNotificationHandler.Setup(p => p.HandleAsync(It.IsAny <TestNotification>(), It.IsAny <ILambdaContext>()))
            .Returns(async() =>
            {
                var t = Task.Delay(1);
                cq.Enqueue(t);
                Console.WriteLine(cq.Count);
                if (cq.Count > 2)
                {
                    throw new Exception("Concurrent Tasks exceeded 2");
                }
                await t;
                cq.TryDequeue(out t);
            });

            var sut = CreateSystemUnderTest();

            Assert.ThrowsAsync <Exception>(() => sut.HandleAsync(snsEvent, new TestLambdaContext()));
        }
        public async Task MaxDegreeOfParallelism_Should_ProperlyPropagated()
        {
            var snsEvent = new SNSEvent
            {
                Records = new List <SNSEvent.SNSRecord>
                {
                    new SNSEvent.SNSRecord
                    {
                        Sns = new SNSEvent.SNSMessage
                        {
                            Message = "{}"
                        }
                    },
                    new SNSEvent.SNSRecord
                    {
                        Sns = new SNSEvent.SNSMessage
                        {
                            Message = "{}"
                        }
                    },
                    new SNSEvent.SNSRecord
                    {
                        Sns = new SNSEvent.SNSMessage
                        {
                            Message = "{}"
                        }
                    },
                    new SNSEvent.SNSRecord
                    {
                        Sns = new SNSEvent.SNSMessage
                        {
                            Message = "{}"
                        }
                    }
                }
            };

            var cq = new ConcurrentQueue <Task>();

            parallelExecutionOptions = new ParallelSnsExecutionOptions {
                MaxDegreeOfParallelism = 2
            };
            mockNotificationHandler.Setup(p => p.HandleAsync(It.IsAny <TestNotification>(), It.IsAny <ILambdaContext>()))
            .Returns(async() =>
            {
                var t = Task.Delay(1);
                cq.Enqueue(t);
                if (cq.Count > 2)
                {
                    throw new Exception("not good");
                }
                await t;
                cq.TryDequeue(out t);
            });

            var sut = CreateSystemUnderTest();

            await sut.HandleAsync(snsEvent, new TestLambdaContext());

            mockNotificationHandler.VerifyAll();
            mockNotificationHandler.Verify(
                handler => handler.HandleAsync(It.IsAny <TestNotification>(), It.IsAny <ILambdaContext>()),
                Times.Exactly(snsEvent.Records.Count));
        }