Skip to content

Commit

Permalink
Merge pull request smsohan#128 from swegner/async-test
Browse files Browse the repository at this point in the history
Fix TestSmtpClient.SentMails behavior with SendAsync
  • Loading branch information
smsohan committed Mar 4, 2014
2 parents 7cd782b + 81c0f1f commit 475099d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Mvc.Mailer.Test/TestSmtpClientTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using System.Net.Mail;
using System.Threading;

namespace Mvc.Mailer.Test {
[TestFixture]
Expand Down Expand Up @@ -57,6 +58,20 @@ public void SendAsync_should_add_to_sent_mails() {
Assert.AreSame(messageB, TestSmtpClient.SentMails[1]);
}

[Test]
public void SendAsync_from_background_thread_should_add_to_sent_mails() {
var messageA = new MailMessage { From = new MailAddress("hello@example.com"), Subject = "Hello", Body = "There" };
messageA.To.Add("hi@example.com");

// Use actual thread for deterministic repro; this also works with async/await as long as
// task scheduler performs work in separate thread.
Thread backgroundThread = new Thread(() => _testSmtpClient.SendAsync(messageA));
backgroundThread.Start();
backgroundThread.Join();

Assert.AreEqual(1, TestSmtpClient.SentMails.Count);
}

[Test]
public void SendAsync_should_set_async_to_false() {
var messageA = new MailMessage { From = new MailAddress("hello@example.com"), Subject = "Hello", Body = "There" };
Expand Down
1 change: 0 additions & 1 deletion Mvc.Mailer/TestSmtpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace Mvc.Mailer {
public class TestSmtpClient : SmtpClientBase {
[ThreadStatic]
private static List<MailMessage> _sentMails;
public static List<MailMessage> SentMails {
get {
Expand Down

0 comments on commit 475099d

Please sign in to comment.