Workstation Id should be returned correctly (dotnet/corefx#27076)
authorSaurabh Singh <saurabh500@users.noreply.github.com>
Tue, 13 Feb 2018 07:54:59 +0000 (23:54 -0800)
committerGitHub <noreply@github.com>
Tue, 13 Feb 2018 07:54:59 +0000 (23:54 -0800)
* Fix workstation Id

* Tests for workstation id

* Add more test cases

Commit migrated from https://github.com/dotnet/corefx/commit/7425455ace4c7819b786c2f0b42fb630944aeef3

src/libraries/System.Data.SqlClient/src/System/Data/SqlClient/SqlConnection.cs
src/libraries/System.Data.SqlClient/tests/FunctionalTests/SqlConnectionBasicTests.cs

index b2c20ec..39672ed 100644 (file)
@@ -306,7 +306,7 @@ namespace System.Data.SqlClient
                 // Note: In Longhorn you'll be able to rename a machine without
                 // rebooting.  Therefore, don't cache this machine name.
                 SqlConnectionString constr = (SqlConnectionString)ConnectionOptions;
-                string result = ((null != constr) ? constr.WorkstationId : string.Empty);
+                string result = constr?.WorkstationId ?? Environment.MachineName;
                 return result;
             }
         }
index 67a2405..96a7995 100644 (file)
@@ -89,6 +89,23 @@ namespace System.Data.SqlClient.Tests
             }
         }
 
+        [Theory]
+        [InlineData("RandomStringForTargetServer", false, true)]
+        [InlineData("RandomStringForTargetServer", true, false)]
+        [InlineData(null, false, false)]
+        [InlineData("", false, false)]
+        public void RetrieveWorkstationId(string workstation, bool withDispose, bool shouldMatchSetWorkstationId)
+        {
+            string connectionString = $"Workstation Id={workstation}";
+            SqlConnection conn = new SqlConnection(connectionString);
+            if(withDispose)
+            {
+                conn.Dispose();
+            }
+            string expected = shouldMatchSetWorkstationId ? workstation : Environment.MachineName;
+            Assert.Equal(expected, conn.WorkstationId);
+        }
+
         [Fact]
         public void ConnectionTimeoutTestWithThread()
         {