Skip to content

Commit

Permalink
Fixed bug (Issue #86) where hooks was not executed when container reused
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Toffia committed May 31, 2019
1 parent 4fe5c72 commit e62ea38
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 13 deletions.
1 change: 1 addition & 0 deletions Ductus.FluentDocker.Tests/Ductus.FluentDocker.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.1.11" />
<PackageReference Include="MSTest.TestFramework" Version="1.1.11" />
<PackageReference Include="Npgsql" Version="4.0.7" />
</ItemGroup>

<ItemGroup>
Expand Down
91 changes: 91 additions & 0 deletions Ductus.FluentDocker.Tests/FluentApiTests/WaitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using Ductus.FluentDocker.Common;
using Ductus.FluentDocker.Services;
using Ductus.FluentDocker.Services.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Npgsql;
using static Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

namespace Ductus.FluentDocker.Tests.FluentApiTests
{
[TestClass]
public class WaitTests
{
private static bool _checkConnectionInvoked;

[TestMethod]
public void SingleWaitLambdaShallGetInvoked()
{
_checkConnectionInvoked = false;
using (var container = Fd.UseContainer()
.UseImage("postgres:9.6-alpine")
.ExposePort(5432)
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
.Wait("", (service, count) => CheckConnection(count, service))
.Build()
.Start())
{
IsTrue(_checkConnectionInvoked, "Invoked since container was created and thus run phase is executed");
}
}

[TestMethod]
public void WaitLambdaWithReusedContainerShallGetInvoked()
{
_checkConnectionInvoked = false;
using (var c1 = Fd.UseContainer()
.UseImage("postgres:9.6-alpine")
.WithName("postgres")
.ExposePort(5432)
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
.ReuseIfExists()
.WaitForPort("5432/tcp", TimeSpan.FromSeconds(30))
.Build()
.Start())
{
// Make sure to have named container running
var config = c1.GetConfiguration();
AreEqual(ServiceRunningState.Running, c1.State);

using (var c2 = Fd.UseContainer()
.UseImage("postgres:9.6-alpine")
.WithName("postgres")
.ExposePort(5432)
.WithEnvironment("POSTGRES_PASSWORD=mysecretpassword")
.ReuseIfExists()
.Wait("", (service, count) => CheckConnection(count, service))
.Build()
.Start())
{
IsTrue(_checkConnectionInvoked,
"Is invoked even if reused container since Start drives the container state eventually to running");
}
}
}

private static int CheckConnection(int count, IContainerService service)
{
_checkConnectionInvoked = true;

if (count > 10) throw new FluentDockerException("Failed to wait for sql server");

var ep = service.ToHostExposedEndpoint("5432/tcp");
var str = $"Server={ep.Address};Port={ep.Port};Userid=postgres;" +
"Password=mysecretpassword;Pooling=false;MinPoolSize=1;" +
"MaxPoolSize=20;Timeout=15;SslMode=Disable;Database=postgres";

try
{
using (var conn = new NpgsqlConnection(str))
{
conn.Open();
return 0;
}
}
catch
{
return 500 /*ms*/;
}
}
}
}
29 changes: 16 additions & 13 deletions Ductus.FluentDocker/Builders/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public override IContainerService Build()
{
existing.RemoveOnDispose = _config.DeleteOnDispose;
existing.StopOnDispose = _config.StopOnDispose;

// Run hooks will not be run since they have already met the requirements
// (since container was found running).
AddHooks(existing);

return existing;
}
Expand Down Expand Up @@ -546,18 +550,6 @@ private void AddHooks(IService container)
}, service, "Wait for HTTP");
});

// Wait for lambda when started
if (null != _config.WaitLambda && 0 != _config.WaitLambda.Count)
container.AddHook(ServiceRunningState.Running, service =>
{
Fd.DisposeOnException(src =>
{
foreach (var continuation in _config.WaitLambda)
((IContainerService) service).Wait(continuation);
}, service, "Wait for lambda");
});


// Wait for process when started
if (null != _config.WaitForProcess)
container.AddHook(ServiceRunningState.Running,
Expand All @@ -567,8 +559,19 @@ private void AddHooks(IService container)
((IContainerService) service).WaitForProcess(_config.WaitForProcess.Item1,
_config.WaitForProcess.Item2),
service, "Wait for process");
});
});

// Wait for lambda when started
if (null != _config.WaitLambda && 0 != _config.WaitLambda.Count)
container.AddHook(ServiceRunningState.Running, service =>
{
Fd.DisposeOnException(src =>
{
foreach (var continuation in _config.WaitLambda)
((IContainerService) service).Wait(continuation);
}, service, "Wait for lambda");
});

// docker execute on running
if (null != _config.ExecuteOnRunningArguments && _config.ExecuteOnRunningArguments.Count > 0)
container.AddHook(ServiceRunningState.Running, service =>
Expand Down

0 comments on commit e62ea38

Please sign in to comment.