Skip to content

Commit

Permalink
fixes issue #257
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotoffia committed Jun 23, 2022
1 parent 625be7e commit d469a12
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
41 changes: 39 additions & 2 deletions Ductus.FluentDocker.MsTest/FluentDockerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ public abstract class FluentDockerTestBase
[TestInitialize]
public void Initialize()
{
Container = Build().Build();
var containerBuilder = Build();

this.OnBeforeContainerBuild(containerBuilder);

Container = containerBuilder.Build();

try
{
this.OnBeforeContainerStart();
Container.Start();
}
catch
catch (Exception ex)
{
this.OnBeforeDispose(Container, ex);
Container.Dispose();
throw;
}
Expand All @@ -37,6 +44,7 @@ public void TeardownContainer()
Container = null;
try
{
this.OnBeforeDispose(c, null);
c?.Dispose();
}
catch
Expand All @@ -52,6 +60,35 @@ protected virtual void OnContainerTearDown()
{
}

/// <summary>
/// Invoked just before the container is built.
/// </summary>
/// <param name="containerBuilder">The <see cref="ContainerBuilder"/> that is about to be built.</param>
protected virtual void OnBeforeContainerBuild(ContainerBuilder containerBuilder)
{
}

/// <summary>
/// Invoked just after the container is built and before starting it.
/// </summary>
protected virtual void OnBeforeContainerStart()
{
}

/// <summary>
/// Invoked just before the container is <see cref="System.IDisposable"/>ed.
/// </summary>
/// <param name="container">The <see cref="IContainerService"/> that is about to be disposed.</param>
/// <param name="throwable">The <see cref="Exception"/> that caused the container to be disposed (when starting up).</param>
/// <remarks>
/// This method is invoked either when the container fails to start. In such situation the
/// <paramref name="throwable"/> is not null. It is also called when the test is cleaning up and thus the
/// <paramref name="throwable"/> is null. The <paramref name="container"> is always set since the teardown
/// will clear the <see cref="Container"/> field. Note that the <paramref name="container"/> may still be null!
/// </remarks>
protected virtual void OnBeforeDispose(IContainerService container, Exception throwable)
{
}
/// <summary>
/// Invoked after a container has been created and started.
/// </summary>
Expand Down
41 changes: 39 additions & 2 deletions Ductus.FluentDocker.XUnit/FluentDockerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ public abstract class FluentDockerTestBase : IDisposable

public FluentDockerTestBase()
{
Container = Build().Build();
var containerBuilder = Build();

this.OnBeforeContainerBuild(containerBuilder);

Container = containerBuilder.Build();

try
{
this.OnBeforeContainerStart();
Container.Start();
}
catch
catch (Exception ex)
{
this.OnBeforeDispose(Container, ex);
Container.Dispose();
throw;
}
Expand All @@ -34,6 +41,7 @@ public void Dispose()
Container = null;
try
{
this.OnBeforeDispose(c, null);
c?.Dispose();
}
catch
Expand All @@ -49,6 +57,35 @@ protected virtual void OnContainerTearDown()
{
}

/// <summary>
/// Invoked just before the container is built.
/// </summary>
/// <param name="containerBuilder">The <see cref="ContainerBuilder"/> that is about to be built.</param>
protected virtual void OnBeforeContainerBuild(ContainerBuilder containerBuilder)
{
}

/// <summary>
/// Invoked just after the container is built and before starting it.
/// </summary>
protected virtual void OnBeforeContainerStart()
{
}

/// <summary>
/// Invoked just before the container is <see cref="System.IDisposable"/>ed.
/// </summary>
/// <param name="container">The <see cref="IContainerService"/> that is about to be disposed.</param>
/// <param name="throwable">The <see cref="Exception"/> that caused the container to be disposed (when starting up).</param>
/// <remarks>
/// This method is invoked either when the container fails to start. In such situation the
/// <paramref name="throwable"/> is not null. It is also called when the test is cleaning up and thus the
/// <paramref name="throwable"/> is null. The <paramref name="container"> is always set since the teardown
/// will clear the <see cref="Container"/> field. Note that the <paramref name="container"/> may still be null!
/// </remarks>
protected virtual void OnBeforeDispose(IContainerService container, Exception throwable)
{
}
/// <summary>
/// Invoked after a container has been created and started.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/global",
"sdk": {
"version": "6.0.100",
"version": "5.0.200",
"allowPrerelease": false,
"rollForward": "latestMinor"
}
Expand Down

0 comments on commit d469a12

Please sign in to comment.