Use dynamically chosen ports to avoid address binding collisions

This commit is contained in:
John Luo 2018-08-23 14:48:28 -07:00
parent 9e7dfc4d15
commit c88e5147da
4 changed files with 67 additions and 40 deletions

View File

@ -6,18 +6,24 @@ if [ "$TRAVIS_OS_NAME" == "osx" ]; then
brew install python
fi
type -p python
python --version
PYTHON_CMD=python
if type -p python2.7 > /dev/null; then
echo "Using 'python2.7' executable because it's available."
PYTHON_CMD=python2.7
fi
$PYTHON_CMD --version
# Install local virtualenv
mkdir .python
cd .python
curl -O https://pypi.python.org/packages/d4/0c/9840c08189e030873387a73b90ada981885010dd9aea134d6de30cd24cb8/virtualenv-15.1.0.tar.gz
curl -OL https://pypi.python.org/packages/d4/0c/9840c08189e030873387a73b90ada981885010dd9aea134d6de30cd24cb8/virtualenv-15.1.0.tar.gz
tar xf virtualenv-15.1.0.tar.gz
cd ..
# Make a virtualenv
python ./.python/virtualenv-15.1.0/virtualenv.py .virtualenv
$PYTHON_CMD ./.python/virtualenv-15.1.0/virtualenv.py .virtualenv
.virtualenv/bin/python --version
.virtualenv/bin/pip --version
@ -27,4 +33,4 @@ python ./.python/virtualenv-15.1.0/virtualenv.py .virtualenv
# We're done. The travis config has already established the path to WSTest should be within the virtualenv.
ls -l .virtualenv/bin
.virtualenv/bin/wstest --version
.virtualenv/bin/wstest --version

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.WebSockets.Test
{
public class KestrelWebSocketHelpers
{
public static IDisposable CreateServer(ILoggerFactory loggerFactory, Func<HttpContext, Task> app)
public static IDisposable CreateServer(ILoggerFactory loggerFactory, Func<HttpContext, Task> app, int port)
{
Action<IApplicationBuilder> startup = builder =>
{
@ -45,7 +45,7 @@ namespace Microsoft.AspNetCore.WebSockets.Test
var configBuilder = new ConfigurationBuilder();
configBuilder.AddInMemoryCollection();
var config = configBuilder.Build();
config["server.urls"] = "http://localhost:54321";
config["server.urls"] = $"http://localhost:{port}";
var host = new WebHostBuilder()
.ConfigureServices(s => s.AddSingleton(loggerFactory))

View File

@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Testing" Version="$(MicrosoftExtensionsLoggingTestingPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.IntegrationTesting" Version="$(MicrosoftAspNetCoreServerIntegrationTestingPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(MicrosoftAspNetCoreServerKestrelPackageVersion)" />
<PackageReference Include="Microsoft.AspNetCore.Testing" Version="$(MicrosoftAspNetCoreTestingPackageVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPackageVersion)" />

View File

@ -6,6 +6,7 @@ using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting.Common;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Logging.Testing;
using Xunit;
@ -23,10 +24,13 @@ namespace Microsoft.AspNetCore.WebSockets.Test
#endif
public class WebSocketMiddlewareTests : LoggedTest
{
private static string ClientAddress = "ws://localhost:54321/";
private readonly int Port;
private readonly string Address;
public WebSocketMiddlewareTests(ITestOutputHelper output) : base(output)
{
Port = TestUriHelper.GetNextPort();
Address = $"ws://localhost:{Port}/";
}
[ConditionalFact]
@ -38,11 +42,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
{
Assert.True(context.WebSockets.IsWebSocketRequest);
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
}
}
}
@ -58,14 +63,15 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.True(context.WebSockets.IsWebSocketRequest);
Assert.Equal("alpha, bravo, charlie", context.Request.Headers["Sec-WebSocket-Protocol"]);
var webSocket = await context.WebSockets.AcceptWebSocketAsync("Bravo");
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
client.Options.AddSubProtocol("alpha");
client.Options.AddSubProtocol("bravo");
client.Options.AddSubProtocol("charlie");
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
// The Windows version of ClientWebSocket uses the casing from the header (Bravo)
// However, the Managed version seems match the header against the list generated by
@ -94,11 +100,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.True(result.EndOfMessage);
Assert.Equal(0, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
var orriginalData = new byte[0];
await client.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
@ -123,11 +130,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
@ -151,11 +159,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.Equal(orriginalData.Length, result.Count);
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
@ -191,11 +200,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.Equal(WebSocketMessageType.Text, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}
}
@ -235,11 +245,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.Equal(WebSocketMessageType.Binary, result.MessageType);
Assert.Equal(orriginalData, serverBuffer);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData, 0, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData, 2, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await client.SendAsync(new ArraySegment<byte>(orriginalData, 4, 7), WebSocketMessageType.Binary, true, CancellationToken.None);
@ -260,11 +271,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
var clientBuffer = new byte[orriginalData.Length];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
@ -288,11 +300,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
var clientBuffer = new byte[orriginalData.Length];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
@ -316,11 +329,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
var clientBuffer = new byte[orriginalData.Length];
WebSocketReceiveResult result;
int receivedCount = 0;
@ -354,11 +368,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 0, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 2, 2), WebSocketMessageType.Binary, false, CancellationToken.None);
await webSocket.SendAsync(new ArraySegment<byte>(orriginalData, 4, 7), WebSocketMessageType.Binary, true, CancellationToken.None);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
var clientBuffer = new byte[orriginalData.Length];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.False(result.EndOfMessage);
@ -404,11 +419,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.CloseSent, client.State);
@ -429,11 +445,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
var webSocket = await context.WebSockets.AcceptWebSocketAsync();
await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
var clientBuffer = new byte[1024];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);
@ -468,11 +485,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.Equal(closeDescription, result.CloseStatusDescription);
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.Closed, client.State);
@ -501,11 +519,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.Equal(closeDescription, result.CloseStatusDescription);
await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, closeDescription, CancellationToken.None);
Assert.Equal(WebSocketState.CloseSent, client.State);
@ -536,11 +555,12 @@ namespace Microsoft.AspNetCore.WebSockets.Test
Assert.Equal(WebSocketMessageType.Close, result.MessageType);
Assert.Equal(WebSocketCloseStatus.NormalClosure, result.CloseStatus);
Assert.Equal(closeDescription, result.CloseStatusDescription);
}))
},
Port))
{
using (var client = new ClientWebSocket())
{
await client.ConnectAsync(new Uri(ClientAddress), CancellationToken.None);
await client.ConnectAsync(new Uri(Address), CancellationToken.None);
var clientBuffer = new byte[1024];
var result = await client.ReceiveAsync(new ArraySegment<byte>(clientBuffer), CancellationToken.None);
Assert.True(result.EndOfMessage);