public void RetryWorksAsExpected() { // --- Arrange var service = ServiceManager.GetService <IEmailComposerService>(); var sendertask = new EmailSenderTask(); ServiceManager.RemoveService(typeof(IEmailSender)); ServiceManager.Register <IEmailSender, EmailSenderWithRetry>(); for (var i = 0; i < 5; i++) { var request = new EmailDefinitionDto { FromAddr = "*****@*****.**", FromName = "You", ToAddr = "*****@*****.**", TemplateType = "MeetingProposed", Parameters = new Dictionary <string, string> { { "FullName", "MyName" } }, SendAfterUtc = null }; service.SendEmail(request); } // --- Act FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "EmailCount", "4"); FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "MaxRetry", "3"); EmailSenderWithRetry.ForceFailure = true; sendertask.Setup(new EmailTaskExecutionContext()); sendertask.Run(); sendertask.Run(); EmailSenderWithRetry.ForceFailure = false; sendertask.Run(); // --- Assert var db = new SqlDatabase(DB_CONN); var emailsToSend = db.Fetch <EmailToSendRecord>(); emailsToSend.ShouldHaveCountOf(1); var emailsSent = db.Fetch <EmailSentRecord>(); emailsSent.ShouldHaveCountOf(4); var emailsFailed = db.Fetch <EmailFailedRecord>(); emailsFailed.ShouldHaveCountOf(0); foreach (var email in emailsSent) { email.Subject.ShouldEqual("Evaluation meeting proposed"); email.Message.ShouldContain("MyName"); email.Message.ShouldNotContain("FullName"); } }
public void EmailsFailWithUnknownTemplate() { // --- Arrange var service = ServiceManager.GetService <IEmailComposerService>(); var sendertask = new EmailSenderTask(); ServiceManager.RemoveService(typeof(IEmailSender)); ServiceManager.Register <IEmailSender, AlwaysWorkingEmailSender>(); for (var i = 0; i < 5; i++) { var request = new EmailDefinitionDto { FromAddr = "*****@*****.**", FromName = "You", ToAddr = "*****@*****.**", TemplateType = "Unknown", Parameters = new Dictionary <string, string> { { "FullName", "MyName" } }, SendAfterUtc = null }; service.SendEmail(request); } // --- Act FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "EmailCount", "4"); FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "MaxRetry", "0"); sendertask.Setup(new EmailTaskExecutionContext()); sendertask.Run(); // --- Assert var db = new SqlDatabase(DB_CONN); var emailsToSend = db.Fetch <EmailToSendRecord>(); emailsToSend.ShouldHaveCountOf(1); var emailsSent = db.Fetch <EmailSentRecord>(); emailsSent.ShouldHaveCountOf(0); var emailsFailed = db.Fetch <EmailFailedRecord>(); emailsFailed.ShouldHaveCountOf(4); foreach (var email in emailsFailed) { email.Subject.ShouldBeNull(); email.Message.ShouldBeNull(); email.LastError.ShouldContain("not found"); } }
public void EmailsFailWithProcessingException() { // --- Arrange var db = new SqlDatabase(DB_CONN); var sendertask = new EmailSenderTask(); ServiceManager.RemoveService(typeof(IEmailSender)); ServiceManager.Register <IEmailSender, AlwaysWorkingEmailSender>(); for (var i = 0; i < 5; i++) { db.Insert(new EmailToSendRecord { FromAddr = "*****@*****.**", FromName = "Me", ToAddr = "*****@*****.**", TemplateType = "MeetingProposed", Parameters = "<dummy><Parameter/></dummy>" }); } // --- Act FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "EmailCount", "4"); FakeConfigurationReader.SetConfigurationValue("SmtpConfig", "MaxRetry", "0"); sendertask.Setup(new EmailTaskExecutionContext()); sendertask.Run(); // --- Assert var emailsToSend = db.Fetch <EmailToSendRecord>(); emailsToSend.ShouldHaveCountOf(1); //1 var emailsSent = db.Fetch <EmailSentRecord>(); emailsSent.ShouldHaveCountOf(0); var emailsFailed = db.Fetch <EmailFailedRecord>(); emailsFailed.ShouldHaveCountOf(4); foreach (var email in emailsFailed) { email.Subject.ShouldBeNull(); email.Message.ShouldBeNull(); email.LastError.ShouldContain("Email message"); } }