diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/Connection/ConnectionServiceTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/Connection/ConnectionServiceTests.cs index 8205cf21..435f2af2 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/Connection/ConnectionServiceTests.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/Connection/ConnectionServiceTests.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Data.Common; +using System.Data.SqlClient; using System.Reflection; using System.Threading; using System.Threading.Tasks; @@ -830,5 +831,35 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Connection ConnectionInfo info; Assert.True(service.TryFindConnection(connectParams.OwnerUri, out info)); } + + /// + /// Verify that Linux/OSX SqlExceptions thrown do not contain an error code. + /// This is a bug in .NET core (see https://github.com/dotnet/corefx/issues/12472). + /// If this test ever fails, it means that this bug has been fixed. When this is + /// the case, look at RetryPolicyUtils.cs in IsRetryableNetworkConnectivityError(), + /// and remove the code block specific to Linux/OSX. + /// + [Fact] + public void TestThatLinuxAndOSXSqlExceptionHasNoErrorCode() + { + TestUtils.RunIfLinuxOrOSX(() => + { + try + { + SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); + builder.DataSource = "bad-server-name"; + builder.UserID = "sa"; + builder.Password = "bad password"; + + SqlConnection connection = new SqlConnection(builder.ConnectionString); + connection.Open(); // This should fail + } + catch (SqlException ex) + { + // Error code should be 0 due to bug + Assert.Equal(ex.Number, 0); + } + }); + } } } diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestUtils.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestUtils.cs index b2d52180..61887cc0 100644 --- a/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestUtils.cs +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/TestUtils.cs @@ -14,6 +14,14 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.Utility test(); } } + + public static void RunIfLinuxOrOSX(Action test) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + test(); + } + } public static void RunIfWindows(Action test) {