Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easy way to add a test container as an hosted service #68

Open
srollinet opened this issue Jan 31, 2022 · 1 comment
Open

Easy way to add a test container as an hosted service #68

srollinet opened this issue Jan 31, 2022 · 1 comment

Comments

@srollinet
Copy link
Contributor

I use TestContainers a lot in my integration tests with libs like alba that allow to build a test server.
Most of the time, the container life time is the same as the test server.

It would be great to have an easy way to setup a test container and run it as an hosted service in a generic host.

I can suggest something and make a PR if you are ok with this idea.

The basic idea would be something like that

    /// <summary>
    /// Extension methods for <see cref="IServiceCollection"/>.
    /// </summary>
    public static class GenericHostExtension
    {
        public static IServiceCollection AddTestContainer<T>(
            this IServiceCollection services,
            Action<ContainerBuilder<T>> configureDelegate)
            where T : IContainer
        {
            var builder = new ContainerBuilder<T>();
            configureDelegate(builder);

            var container = builder.Build();
            services.AddHostedService(s => new ContainerHostedService<T>(container));

            return services;
        }

        public static IServiceCollection AddAdoTestContainer<T>(
            this IServiceCollection services,
            string connectionStringName,
            Action<ContainerBuilder<T>> configureDelegate)
            where T : AdoNetContainer
        {
            var builder = new ContainerBuilder<T>();
            configureDelegate(builder);

            var container = builder.Build();
            services.AddHostedService(s => new ContainerHostedService<T>(container));

            //TODO Inject the connection string in the configuration with the key 'connectionStringName'

            return services;
        }
    }

    internal class ContainerHostedService<T> : IHostedService where T : IContainer
    {
        private readonly T _container;

        public ContainerHostedService(T container)
        {
            _container = container;
        }

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            await _container.StartAsync(cancellationToken);
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await _container.StartAsync(cancellationToken);
        }
    }

It will also be possible to inject all the logging related stuff in the container

@isen-ng
Copy link
Owner

isen-ng commented Feb 3, 2022

I think this is a great idea!
However, I think we should not couple this into the main project.
Eg we can support this in a separate project specific to hosted services.

Something like this:
https://github.com/jinhong-/testcontainers-environment-dotnet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants