-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share the lifetime implementation between ContainerFixture and Contai…
…nerTest
- Loading branch information
Showing
3 changed files
with
72 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
namespace Testcontainers.Xunit; | ||
|
||
/// <summary> | ||
/// Base class managing the lifetime of a container. | ||
/// </summary> | ||
/// <typeparam name="TBuilderEntity">The builder entity.</typeparam> | ||
/// <typeparam name="TContainerEntity">The container entity.</typeparam> | ||
public abstract class ContainerLifetime<TBuilderEntity, TContainerEntity> : IAsyncLifetime | ||
where TBuilderEntity : IContainerBuilder<TBuilderEntity, TContainerEntity>, new() | ||
where TContainerEntity : IContainer | ||
{ | ||
private readonly Lazy<TContainerEntity> _container; | ||
|
||
/// <summary> | ||
/// The logger. | ||
/// </summary> | ||
protected abstract ILogger Logger { get; } | ||
|
||
protected ContainerLifetime() | ||
{ | ||
_container = new Lazy<TContainerEntity>(() => | ||
{ | ||
var containerBuilder = new TBuilderEntity().WithLogger(Logger); | ||
return Configure(containerBuilder).Build(); | ||
}); | ||
} | ||
|
||
/// <summary> | ||
/// Extension point to further configure the container instance. | ||
/// </summary> | ||
/// <example> | ||
/// <code> | ||
/// public class MariaDbRootUserFixture(IMessageSink messageSink) : DbContainerFixture<MariaDbBuilder, MariaDbContainer>(messageSink) | ||
/// { | ||
/// public override DbProviderFactory DbProviderFactory => MySqlConnectorFactory.Instance; | ||
/// <br /> | ||
/// protected override MariaDbBuilder Configure(MariaDbBuilder builder) | ||
/// { | ||
/// return builder.WithUsername("root"); | ||
/// } | ||
/// } | ||
/// </code> | ||
/// </example> | ||
/// <param name="builder">The container builder.</param> | ||
/// <returns>A configured instance of <typeparamref name="TBuilderEntity" />.</returns> | ||
protected virtual TBuilderEntity Configure(TBuilderEntity builder) => builder; | ||
|
||
/// <summary> | ||
/// The container instance. | ||
/// </summary> | ||
public TContainerEntity Container => _container.Value; | ||
|
||
/// <inheritdoc /> | ||
Task IAsyncLifetime.InitializeAsync() => InitializeAsync(); | ||
|
||
/// <inheritdoc cref="IAsyncLifetime.InitializeAsync()" /> | ||
protected virtual Task InitializeAsync() => Container.StartAsync(); | ||
|
||
/// <inheritdoc /> | ||
Task IAsyncLifetime.DisposeAsync() => DisposeAsync(); | ||
|
||
/// <inheritdoc cref="IAsyncLifetime.DisposeAsync()" /> | ||
protected virtual Task DisposeAsync() => Container.DisposeAsync().AsTask(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters