Skip to content

Commit

Permalink
bt-skyrise#15 use options object in PostgresRunner API
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbudny authored and Rafal Zabrowarny committed Feb 7, 2018
1 parent 92ac1ce commit e009874
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 78 deletions.
10 changes: 3 additions & 7 deletions src/Postgres2Go.Samples/when_using_as_class_fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ public class PgFixture : IDisposable
public PgFixture()
{
_pgRunner = PostgresRunner
.Start(postgresBinariesSearchPattern: GetPgBinariesRelativePath());
.Start(new PostgresRunnerOptions{ BinariesSearchPattern = GetPgBinariesRelativePath()});
}

public void Dispose()
{
if (_pgRunner is IDisposable)
(_pgRunner as IDisposable).Dispose();
}
public void Dispose() => _pgRunner?.Dispose();

public string ConnectionString => _pgRunner.ConnectionString;
public string ConnectionString => _pgRunner.GetConnectionString();

private string GetPgBinariesRelativePath()
{
Expand Down
10 changes: 0 additions & 10 deletions src/Postgres2Go/Helper/FileSystem/TempDirectory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ namespace Postgres2Go.Helper.FileSystem
{
internal class TempDirectory
{
internal static string Create()
{
var tempDirPath = GetUnusedPath();

FileSystem
.CreateFolder(tempDirPath);

return tempDirPath;
}

internal static string GetUnusedPath()
{
bool alreadyExists = false;
Expand Down
8 changes: 2 additions & 6 deletions src/Postgres2Go/Postgres2Go.csproj
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Helper\PostgresProcess.IDisposable.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Diagnostics.Process" Version="4.3.0" />
<PackageReference Include="System.Net.NetworkInformation" Version="4.3.0" />
<PackageReference Include="System.Threading" Version="4.3.0" />
</ItemGroup>

</Project>
</Project>
6 changes: 2 additions & 4 deletions src/Postgres2Go/PostgresRunner.IDisposable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ private void Dispose(bool disposing)
return;
}

PostgresStoperProcess
.Exec(_pgBin.Directory, _dataDirectory);
PostgresStoperProcess.Exec(_binDirectory, _instanceDirectory);

FileSystem
.DeleteFolder(_dataDirectory);
FileSystem.DeleteFolder(_instanceDirectory);

State = State.Stopped;

Expand Down
73 changes: 22 additions & 51 deletions src/Postgres2Go/PostgresRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,27 @@ namespace Postgres2Go
{
public partial class PostgresRunner
{
private string _dataDirectory;
private int _port;
private PostgresBinaryLocator _pgBin;
private readonly PostgresRunnerOptions _options;
private string _instanceDirectory, _binDirectory;

/// <summary>
/// State of the current Postgres instance
/// </summary>
public State State { get; private set; }

/// <summary>
/// Connections string that should be used to establish a connection the Postgres instance
/// </summary>
public string ConnectionString { get; private set; }

/// <summary>
/// Starts Multiple Postgres instances with each call
/// On dispose: kills them and deletes their data directory
/// Starts a new Postgres instance with each call
/// </summary>
/// <param name="options">Runner options</param>
/// <remarks>Should be used for integration tests</remarks>
/// <param name="dataDirectory">Working directory where Postgres cluster will be initialized</param>
/// <param name="postgresBinariesSearchPattern">Pattern of path where postgres binaries should be located</param>
/// <param name="databaseName">Name of database used within of connection string</param>
public static PostgresRunner Start(string dataDirectory = null, string postgresBinariesSearchPattern = null, string databaseName = "postgres")
public static PostgresRunner Start(PostgresRunnerOptions options = null)
{
if (dataDirectory == null)
{
dataDirectory = TempDirectory.Create();
}

// this is required to support multiple instances to run in parallel
var instanceDataDirectory = Path.Combine(dataDirectory, GetUniqueHash());

PostgresRunner pgRunner = null;

try
{
pgRunner = new PostgresRunner().Run(
PortPool.GetInstance,
new PostgresBinaryLocator(postgresBinariesSearchPattern),
dataDirectory,
databaseName
);
pgRunner = new PostgresRunner(options).Run();

return pgRunner;
}
Expand All @@ -62,44 +41,36 @@ public static PostgresRunner Start(string dataDirectory = null, string postgresB

}

private PostgresRunner Run(
IPortPool portPool,
PostgresBinaryLocator pgBin,
string dataDirectory,
string databaseName
)
private PostgresRunner(PostgresRunnerOptions options) => _options = options ?? new PostgresRunnerOptions();

private PostgresRunner Run()
{
_port = portPool.GetNextOpenPort();
_pgBin = pgBin;

if (dataDirectory == null)
{
dataDirectory = TempDirectory.Create();
}
else
{
FileSystem.CreateFolder(dataDirectory);
}

_dataDirectory = dataDirectory;
_instanceDirectory = Path.Combine(_options.DataDirectory ?? TempDirectory.GetUnusedPath(), GetUniqueHash());
FileSystem.CreateFolder(_instanceDirectory);

_port = _options.Port ?? PortPool.GetInstance.GetNextOpenPort();
_binDirectory = new PostgresBinaryLocator(_options.BinariesSearchPattern).Directory;


PostgresBinaries
.AssertCanExecute(_pgBin.Directory);
.AssertCanExecute(_binDirectory);

PostgresInitializatorProcess
.Exec(_pgBin.Directory, _dataDirectory, PostgresDefaults.User);
.Exec(_binDirectory, _instanceDirectory, PostgresDefaults.User);

PostgresStarterProcess
.Exec(_pgBin.Directory, _dataDirectory, _port);
.Exec(_binDirectory, _instanceDirectory, _port);

ConnectionString = $"Server=localhost;Port={_port};User Id={PostgresDefaults.User};Database={databaseName}";
State = State.Running;

return this;
}


private static string GetUniqueHash() => Guid.NewGuid().ToString().GetHashCode().ToString("x");

public string GetConnectionString(string databaseName = "postgres") =>
$"Server=localhost;Port={_port};User Id={PostgresDefaults.User};Database={databaseName}";
}


}

0 comments on commit e009874

Please sign in to comment.