Skip to content

Commit

Permalink
Fix problems with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawxy committed Feb 1, 2019
1 parent 1524cfe commit 86501ed
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 53 deletions.
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Rebus.Activation;
using Rebus.Bus;
using Rebus.Config;
Expand All @@ -17,24 +15,20 @@ public class NetCoreServiceProviderActivationContext : IActivationContext
{
public IHandlerActivator CreateActivator(Action<IHandlerRegistry> handlerConfig, out IActivatedContainer container)
{
var services = new ServiceCollection();
var services = new ServiceCollection().AddSingleton(p => new NetCoreServiceProviderContainerAdapter(p));
handlerConfig.Invoke(new HandlerRegistry(services));
services.AddSingleton<IApplicationLifetime>(new TestLifetime());

var provider = services.BuildServiceProvider();

var adapter = new NetCoreServiceProviderContainerAdapter(provider);
var provider = services.BuildServiceProvider();

container = new ActivatedContainer(provider);

return adapter;
return provider.GetRequiredService<NetCoreServiceProviderContainerAdapter>();
}

public IBus CreateBus(Action<IHandlerRegistry> handlerConfig, Func<RebusConfigurer, RebusConfigurer> configureBus, out IActivatedContainer container)
{
var services = new ServiceCollection();
handlerConfig.Invoke(new HandlerRegistry(services));
services.AddSingleton<IApplicationLifetime>(new TestLifetime());

services.AddRebus(configureBus);

Expand Down Expand Up @@ -73,9 +67,9 @@ static IEnumerable<Type> GetHandlerInterfaces(Type type)

class ActivatedContainer : IActivatedContainer
{
readonly IServiceProvider _provider;
readonly Microsoft.Extensions.DependencyInjection.ServiceProvider _provider;

public ActivatedContainer(IServiceProvider provider)
public ActivatedContainer(Microsoft.Extensions.DependencyInjection.ServiceProvider provider)
{
_provider = provider;
}
Expand All @@ -87,45 +81,7 @@ public IBus ResolveBus()

public void Dispose()
{
_provider.GetRequiredService<IApplicationLifetime>().StopApplication();
}
}

class TestLifetime : IApplicationLifetime
{
readonly CancellationTokenSource _stoppingSource;
readonly CancellationTokenSource _stoppedSource;

public TestLifetime()
{
var source = new CancellationTokenSource();
ApplicationStarted = source.Token;
source.Cancel();

_stoppingSource = new CancellationTokenSource();
_stoppedSource = new CancellationTokenSource();

ApplicationStopping = _stoppingSource.Token;
ApplicationStopped = _stoppedSource.Token;
}

public CancellationToken ApplicationStarted { get; }

public CancellationToken ApplicationStopping { get; }

public CancellationToken ApplicationStopped { get; }

public void StopApplication()
{
_stoppingSource.Cancel();

var allHandlersStopped = new CancellationTokenSource();
_stoppedSource.Token.Register(() => allHandlersStopped.Cancel());

_stoppedSource.Cancel();

// make sure we block untill all the handlers have finished.
allHandlersStopped.Token.WaitHandle.WaitOne();
_provider.Dispose();
}
}
}
Expand Down
Expand Up @@ -28,7 +28,6 @@
<ItemGroup>
<ProjectReference Include="..\Rebus.ServiceProvider\Rebus.ServiceProvider.csproj" />
<PackageReference Include="FluentAssertions" Version="5.6.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.12.0" />
Expand Down
Expand Up @@ -16,7 +16,7 @@ namespace Rebus.ServiceProvider
/// Implementation of <see cref="IContainerAdapter"/> that is backed by a ServiceProvider
/// </summary>
/// <seealso cref="IContainerAdapter" />
public class NetCoreServiceProviderContainerAdapter : IContainerAdapter
public class NetCoreServiceProviderContainerAdapter : IContainerAdapter, IDisposable
{
readonly IServiceProvider _provider;

Expand Down
1 change: 0 additions & 1 deletion Rebus.ServiceProvider/Rebus.ServiceProvider.csproj
Expand Up @@ -43,7 +43,6 @@
<None Include="ServiceCollectionExtensions.Bus.cs" />
</ItemGroup>
<ItemGroup>

<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Rebus" Version="5.2.1" />
</ItemGroup>
Expand Down

0 comments on commit 86501ed

Please sign in to comment.