Bug fix: await SmtpClient.SendMailAsync never ends (dotnet/corefx#35915)
authorMarco Rossignoli <marco.rossignoli@gmail.com>
Sat, 9 Mar 2019 22:23:32 +0000 (23:23 +0100)
committerDavid Shulman <david.shulman@microsoft.com>
Sat, 9 Mar 2019 22:23:32 +0000 (14:23 -0800)
Call InvokeCallback() in case of CompletedSynchronously.

Fixes dotnet/corefx#35738

Commit migrated from https://github.com/dotnet/corefx/commit/848811c496a7cb9d48ddee2775e982b4b732e8ac

src/libraries/System.Net.Mail/src/System/Net/Mail/MailPriority.cs
src/libraries/System.Net.Mail/tests/Functional/SmtpClientTest.cs
src/libraries/System.Net.Mail/tests/Functional/System.Net.Mail.Functional.Tests.csproj

index cb25230..591b996 100644 (file)
@@ -341,6 +341,7 @@ namespace System.Net.Mail
                 if (newResult.CompletedSynchronously)
                 {
                     writer.EndGetContentStream(newResult).Close();
+                    result.InvokeCallback();
                 }
                 return result;
             }
index 36088d9..bc71a28 100644 (file)
@@ -305,25 +305,28 @@ namespace System.Net.Mail.Tests
             }
         }
 
-        [Fact]
-        public async Task TestMailDeliveryAsync()
+        [Theory]
+        [InlineData("howdydoo")]
+        [InlineData("")]
+        [InlineData(null)]
+        public async Task TestMailDeliveryAsync(string body)
         {
             SmtpServer server = new SmtpServer();
             SmtpClient client = new SmtpClient("localhost", server.EndPoint.Port);
-            MailMessage msg = new MailMessage("foo@example.com", "bar@example.com", "hello", "howdydoo");
+            MailMessage msg = new MailMessage("foo@example.com", "bar@example.com", "hello", body);
             string clientDomain = IPGlobalProperties.GetIPGlobalProperties().HostName.Trim().ToLower();
 
             try
             {
                 Thread t = new Thread(server.Run);
                 t.Start();
-                await client.SendMailAsync(msg);
+                await client.SendMailAsync(msg).TimeoutAfter((int)TimeSpan.FromSeconds(30).TotalMilliseconds);
                 t.Join();
 
                 Assert.Equal("<foo@example.com>", server.MailFrom);
                 Assert.Equal("<bar@example.com>", server.MailTo);
                 Assert.Equal("hello", server.Subject);
-                Assert.Equal("howdydoo", server.Body);
+                Assert.Equal(body ?? "", server.Body);
                 Assert.Equal(clientDomain, server.ClientDomain);
             }
             finally
index 73fc43d..7cec78f 100644 (file)
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
     <ProjectGuid>{A26D88B7-6EF6-4C8C-828B-7B57732CCE38}</ProjectGuid>
     <Configurations>netstandard-Debug;netstandard-Release</Configurations>
@@ -26,6 +26,9 @@
     <Compile Include="$(CommonTestPath)\System\IO\TempFile.cs">
       <Link>Common\System\IO\TempFile.cs</Link>
     </Compile>
+    <Compile Include="$(CommonTestPath)\System\Threading\Tasks\TaskTimeoutExtensions.cs">
+      <Link>Common\System\Threading\Tasks\TaskTimeoutExtensions.cs</Link>
+    </Compile>
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System.Net.NetworkInformation" />