Skip to content

Commit

Permalink
refactor: use portmanager to find next available tcp port
Browse files Browse the repository at this point in the history
  • Loading branch information
CumpsD committed Mar 25, 2019
1 parent bb709d9 commit 302af91
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public SqlConnection CreateMasterConnection()

public SqlConnectionStringBuilder CreateMasterConnectionStringBuilder()
=> new SqlConnectionStringBuilder(
$"server=localhost,1433;User Id=sa;Password={Password};Initial Catalog=master");
$"server=tcp:localhost,1433;User Id=sa;Password={Password};Initial Catalog=master");

public SqlConnectionStringBuilder CreateConnectionStringBuilder()
=> new SqlConnectionStringBuilder(
$"server=localhost,1433;User Id=sa;Password={Password};Initial Catalog={_databaseName}");
$"server=tcp:localhost,1433;User Id=sa;Password={Password};Initial Catalog={_databaseName}");

public async Task CreateDatabase(CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace PublicServiceRegistry.Api.Backoffice.Tests.Framework
{
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;
Expand All @@ -16,7 +15,7 @@ public class EmbeddedDockerSqlServerDatabase : ISqlServerDatabase
private const string Password = "E@syP@ssw0rd";
private const string Image = "microsoft/mssql-server-linux";
private const string Tag = "2017-latest";
private const int Port = 1433; // 11433;
private static readonly int Port = PortManager.GetNextPort();

public EmbeddedDockerSqlServerDatabase(string databaseName)
{
Expand All @@ -43,11 +42,11 @@ public SqlConnection CreateMasterConnection()

public SqlConnectionStringBuilder CreateMasterConnectionStringBuilder()
=> new SqlConnectionStringBuilder(
$"server=127.0.0.1,{Port};User Id=sa;Password={Password};Initial Catalog=master");
$"server=tcp:localhost,{Port};User Id=sa;Password={Password};Initial Catalog=master");

public SqlConnectionStringBuilder CreateConnectionStringBuilder()
=> new SqlConnectionStringBuilder(
$"server=127.0.0.1,{Port};User Id=sa;Password={Password};Initial Catalog={_databaseName}");
$"server=tcp:localhost,{Port};User Id=sa;Password={Password};Initial Catalog={_databaseName}");

public async Task CreateDatabase(CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace PublicServiceRegistry.Api.Backoffice.Tests.Framework
{
using System.Net;
using System.Net.Sockets;

// Original source: https://github.com/aspnet/KestrelHttpServer/blob/95722670c14855e3d5a59d482f01b9b38ed9dff1/test/Microsoft.AspNetCore.Server.Kestrel.TestCommon/PortManager.cs
internal static class PortManager
{
private static readonly object PortLock = new object();

public static int GetNextPort()
{
int port;
lock (PortLock)
{
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0));
port = ((IPEndPoint)socket.LocalEndPoint).Port;
socket.Close();
}
}

return port;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ public class WebServerTests : IAsyncLifetime

private static string AppsettingsJson => Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName, "appsettings.json");

public WebServerTests(DockerFixture fixture, ITestOutputHelper helper)
public WebServerTests(
DockerFixture fixture,
ITestOutputHelper helper)
{
_helper = helper;
_database = fixture.GenerateDatabase(Guid.NewGuid().ToString("N"));
}

[Fact(Skip = "Trying to fix build")]
[Fact]
[IntegrationTest]
public async Task TheServerWorks()
{
Expand Down Expand Up @@ -65,7 +67,7 @@ public async Task TheServerWorks()
Assert.StartsWith("Welcome to the Basisregisters Vlaanderen Public Service Api", responseString);
}

[Fact(Skip = "Trying to fix build")]
[Fact]
[IntegrationTest]
public async Task RoutingWorks()
{
Expand Down

0 comments on commit 302af91

Please sign in to comment.