diff --git a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.ConnectionLifecycle.cs b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.ConnectionLifecycle.cs index 67e0e02428..0ee8a4c02a 100644 --- a/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.ConnectionLifecycle.cs +++ b/test/Microsoft.AspNetCore.SignalR.Client.Tests/HttpConnectionTests.ConnectionLifecycle.cs @@ -9,6 +9,7 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; using Microsoft.AspNetCore.Client.Tests; using Microsoft.AspNetCore.Connections; +using Microsoft.AspNetCore.SignalR.Tests; using Microsoft.AspNetCore.Sockets.Client.Http; using Microsoft.AspNetCore.Sockets.Client.Internal; using Microsoft.Extensions.Logging.Testing; @@ -94,7 +95,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests // with WebSockets available and not. If Websockets aren't available and // we can't to test the fallback once scenario we don't decrement the passthreshold // because we still try to start twice (SSE and LP). - if (!IsWebSocketsSupported() && passThreshold > 2) + if (!TestHelpers.IsWebSocketsSupported() && passThreshold > 2) { passThreshold -= 1; } @@ -150,7 +151,7 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests Assert.Equal("Unable to connect to the server with any of the available transports.", ex.Message); // If websockets aren't supported then we expect one less attmept to start. - if (!IsWebSocketsSupported()) + if (!TestHelpers.IsWebSocketsSupported()) { availableTransports -= 1; } @@ -363,23 +364,6 @@ namespace Microsoft.AspNetCore.SignalR.Client.Tests await Assert.ThrowsAsync(() => connection.StartAsync(TransferFormat.Text).OrTimeout()); Assert.Equal(nameof(HttpConnection), exception.ObjectName); } - - private static bool IsWebSocketsSupported() - { -#if NETCOREAPP2_1 - // .NET Core 2.1 and greater has sockets - return true; -#else - // Non-Windows platforms have sockets - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return true; - } - - // Windows 8 and greater has sockets - return Environment.OSVersion.Version >= new Version(6, 2); -#endif - } } } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TestHelpers.cs b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TestHelpers.cs index 17f037d178..b496299152 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TestHelpers.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/TestHelpers.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Runtime.InteropServices; namespace Microsoft.AspNetCore.SignalR.Tests { @@ -9,16 +10,19 @@ namespace Microsoft.AspNetCore.SignalR.Tests { public static bool IsWebSocketsSupported() { - try - { - new System.Net.WebSockets.ClientWebSocket().Dispose(); - } - catch (PlatformNotSupportedException) - { - return false; - } - +#if NETCOREAPP2_1 + // .NET Core 2.1 and greater has sockets return true; +#else + // Non-Windows platforms have sockets + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return true; + } + + // Windows 8 and greater has sockets + return Environment.OSVersion.Version >= new Version(6, 2); +#endif } } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/WebSocketsSupportedConditionAttribute.cs b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/WebSocketsSupportedConditionAttribute.cs index 5a831e73fb..86c831b00e 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests.Utils/WebSocketsSupportedConditionAttribute.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests.Utils/WebSocketsSupportedConditionAttribute.cs @@ -16,19 +16,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests { get { -#if NETCOREAPP2_1 - // .NET Core 2.1 and greater has sockets - return true; -#else - // Non-Windows platforms have Websockets - if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { - return true; - } - - // Windows 8 and greater has Websockets - return Environment.OSVersion.Version >= new Version(6, 2); -#endif + return TestHelpers.IsWebSocketsSupported(); } } diff --git a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs index fde6de4969..2b66967adf 100644 --- a/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs +++ b/test/Microsoft.AspNetCore.SignalR.Tests/EndToEndTests.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.IO.Pipelines; using System.Net.Http; using System.Net.WebSockets; +using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -408,6 +409,15 @@ namespace Microsoft.AspNetCore.SignalR.Tests private int _tries; private string _prevConnectionId = null; private IDuplexPipe _application; + private int availableTransports = 3; + + public FakeTransport() + { + if (!TestHelpers.IsWebSocketsSupported()) + { + availableTransports -= 1; + } + } public Task StartAsync(Uri url, IDuplexPipe application, TransferFormat transferFormat, IConnection connection) { @@ -424,7 +434,7 @@ namespace Microsoft.AspNetCore.SignalR.Tests _prevConnectionId = id; } - if (_tries < 3) + if (_tries < availableTransports) { throw new Exception(); }