Esempio n. 1
0
        /// <summary>
        /// A simple function that takes a string and does a ToUpper
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task FunctionHandler(string input, ILambdaContext context)
        {
            IAmazonSQS              sqsClient              = new AmazonSQSClient();
            ISqsAdapter             sqsAdapter             = new SqsAdapter(sqsClient, "");
            IAmazonS3               s3Client               = new AmazonS3Client();
            IS3Adapter              s3Adapter              = new S3Adapter(s3Client);
            IEmailMessageRepository emailMessageRepository = new EmailMessageRepository(sqsAdapter, s3Adapter);

            IAmazonSimpleEmailService sesClient   = new AmazonSimpleEmailServiceClient();
            IEmailDeliveryConfig      emailConfig = new EmailDeliveryConfig()
            {
                FromAddress = ""
            };
            ISESAdapter           sesAdapter      = new SESAdapter(sesClient, emailConfig);
            IEmailMessageDelivery messageDelivery = new EmailMessageDelivery(sesAdapter);
            IEmailQueueProcessor  emailProcessor  = new EmailQueueProcessor(emailMessageRepository, messageDelivery);

            var emptyProcesses = 0;

            while (emptyProcesses <= 3 && context.RemainingTime > TimeSpan.FromSeconds(30))
            {
                var count = await emailProcessor.ProcessEmailMessages();

                if (count == 0)
                {
                    emptyProcesses++;
                }
            }
        }
Esempio n. 2
0
        public async Task Confirmed_Run_SendsEmail()
        {
            using (IdentityWsDbContext ef = CreateEf()) {
                Email email;
                ef.Emails.Add(email = new Email
                {
                    To = new Alias
                    {
                        DateConfirmed = now.UtcNow
                    }
                });
                await ef.SaveChangesAsync();

                EmailQueueProcessor patient     = new EmailQueueProcessor(dummyLog, now);
                bool email_sent                 = false;
                Mock <IEmailSender> mock_sender = new Mock <IEmailSender>();
                mock_sender.Setup(m => m.Send(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(),
                                              It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Callback <string, string, string, string, string, string>((a, b, c, d, e, f) => email_sent = true);
                IServiceProvider services = MakeServices(mock_sender.Object, ef);

                patient.Run(services, null);

                email.Should().Match <Email>(e =>
                                             e.ProcessingCount == 1 &&
                                             e.DateLastProcessed == now.UtcNow &&
                                             string.IsNullOrEmpty(e.LastProcessingError),
                                             "the alias is confirmed");
                email_sent.Should().BeTrue("the email can be sent");
            }
        }
Esempio n. 3
0
        public async Task ProhibitedUnconfirmed_Run_FailsPermanently()
        {
            using (IdentityWsDbContext ef = CreateEf()) {
                Email email;
                ef.Emails.Add(email = new Email
                {
                    To = new Alias()
                });
                await ef.SaveChangesAsync();

                EmailQueueProcessor patient     = new EmailQueueProcessor(dummyLog, now);
                bool email_sent                 = false;
                Mock <IEmailSender> mock_sender = new Mock <IEmailSender>();
                mock_sender.Setup(m => m.Send(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(),
                                              It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Callback <string, string, string, string, string, string>((a, b, c, d, e, f) => email_sent = true);
                IServiceProvider services = MakeServices(mock_sender.Object, ef);

                patient.Run(services, null);

                email.Should().Match <Email>(e =>
                                             e.ProcessingCount == 10 &&
                                             e.DateLastProcessed == now.UtcNow &&
                                             e.LastProcessingError == "Unconfirmed",
                                             "the alias is unconfirmed and Email.SendIfUnconfirmed is false");
                email_sent.Should().BeFalse("an error ocurred");
            }
        }
Esempio n. 4
0
        public async Task OutsideBackoffPeriod_Run_SendsEmail()
        {
            using (IdentityWsDbContext ef = CreateEf()) {
                DateTime last_processed = now.UtcNow.AddMinutes(-16);
                Email    email;
                ef.Emails.Add(email = new Email
                {
                    To = new Alias
                    {
                        DateConfirmed = now.UtcNow
                    },
                    ProcessingCount     = 4, // Processing should be allowed 16 mins after error (2^^4).
                    LastProcessingError = "some weird error",
                    DateLastProcessed   = last_processed
                });
                await ef.SaveChangesAsync();

                EmailQueueProcessor patient     = new EmailQueueProcessor(dummyLog, now);
                bool email_sent                 = false;
                Mock <IEmailSender> mock_sender = new Mock <IEmailSender>();
                mock_sender.Setup(m => m.Send(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(),
                                              It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Callback <string, string, string, string, string, string>((a, b, c, d, e, f) => email_sent = true);
                IServiceProvider services = MakeServices(mock_sender.Object, ef);

                patient.Run(services, null);

                email.Should().Match <Email>(e =>
                                             e.ProcessingCount == 5 &&
                                             e.DateLastProcessed == now.UtcNow &&
                                             string.IsNullOrEmpty(e.LastProcessingError),
                                             "the last processing date is (just) outside the back-off period");
                email_sent.Should().BeTrue("the email can be sent");
            }
        }
Esempio n. 5
0
        public async Task ExceptionDuringSend_Run_SavesMessageAndContinues()
        {
            using (IdentityWsDbContext ef = CreateEf()) {
                Email email1, email2;
                ef.Emails.AddRange(new[] {
                    email1 = new Email
                    {
                        To = new Alias
                        {
                            DateConfirmed = now.UtcNow
                        },
                        BodyText = "Hello"
                    }, email2 = new Email
                    {
                        To = new Alias
                        {
                            DateConfirmed = now.UtcNow
                        },
                        BodyText = "World"
                    }
                });
                await ef.SaveChangesAsync();

                EmailQueueProcessor patient     = new EmailQueueProcessor(dummyLog, now);
                string emailed_text             = null;
                bool   called_back              = false;
                Mock <IEmailSender> mock_sender = new Mock <IEmailSender>();
                mock_sender.Setup(m => m.Send(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(),
                                              It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()))
                .Callback <string, string, string, string, string, string>((a, b, c, d, e, f) => {
                    if (!called_back)
                    {
                        called_back = true;
                        throw new Exception("Exceptional!");
                    }
                    emailed_text = e;
                });
                IServiceProvider services = MakeServices(mock_sender.Object, ef);

                patient.Run(services, null);

                email1.Should().Match <Email>(e =>
                                              e.ProcessingCount == 1 &&
                                              e.DateLastProcessed == now.UtcNow &&
                                              e.LastProcessingError == "Exceptional!",
                                              "an exception was thrown during sending");
                email2.Should().Match <Email>(e =>
                                              e.ProcessingCount == 1 &&
                                              e.DateLastProcessed == now.UtcNow &&
                                              string.IsNullOrEmpty(e.LastProcessingError),
                                              "sending should succeed");
                emailed_text.Should().Be("World", "that's the subject of email2");
            }
        }