You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

41 lines
1.4 KiB

  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Threading;
  4. using DotNetCore.CAP.Test.Helpers;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Xunit.Abstractions;
  7. namespace DotNetCore.CAP.Test.IntegrationTests
  8. {
  9. public abstract class IntegrationTestBase : IDisposable
  10. {
  11. protected readonly CancellationTokenSource CancellationTokenSource = new(TimeSpan.FromSeconds(10));
  12. protected readonly ServiceProvider Container;
  13. protected readonly ObservableCollection<object> HandledMessages = new();
  14. protected readonly ICapPublisher Publisher;
  15. protected IntegrationTestBase(ITestOutputHelper testOutput)
  16. {
  17. var services = new ServiceCollection();
  18. services.AddTestSetup(testOutput);
  19. services.AddSingleton(sp => new TestMessageCollector(HandledMessages));
  20. ConfigureServices(services);
  21. Container = services.BuildTestContainer(CancellationToken);
  22. Scope = Container.CreateScope();
  23. Publisher = Scope.ServiceProvider.GetRequiredService<ICapPublisher>();
  24. }
  25. protected IServiceScope Scope { get; }
  26. protected CancellationToken CancellationToken => CancellationTokenSource.Token;
  27. public void Dispose()
  28. {
  29. Scope?.Dispose();
  30. Container?.Dispose();
  31. }
  32. protected abstract void ConfigureServices(IServiceCollection services);
  33. }
  34. }