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.
 
 
 

94 lines
2.9 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.Extensions.Options;
  7. using Xunit;
  8. namespace DotNetCore.CAP.Test
  9. {
  10. public class CapBuilderTest
  11. {
  12. [Fact]
  13. public void CanCreateInstanceAndGetService()
  14. {
  15. var services = new ServiceCollection();
  16. services.AddSingleton<ICapPublisher, MyProducerService>();
  17. var builder = new CapBuilder(services);
  18. Assert.NotNull(builder);
  19. var count = builder.Services.Count;
  20. Assert.Equal(1, count);
  21. var provider = services.BuildServiceProvider();
  22. var capPublisher = provider.GetService<ICapPublisher>();
  23. Assert.NotNull(capPublisher);
  24. }
  25. [Fact]
  26. public void CanAddCapService()
  27. {
  28. var services = new ServiceCollection();
  29. services.AddCap(x => { });
  30. var builder = services.BuildServiceProvider();
  31. var markService = builder.GetService<CapMarkerService>();
  32. Assert.NotNull(markService);
  33. }
  34. [Fact]
  35. public void CanOverridePublishService()
  36. {
  37. var services = new ServiceCollection();
  38. services.AddCap(x => { }).AddProducerService<MyProducerService>();
  39. var thingy = services.BuildServiceProvider()
  40. .GetRequiredService<ICapPublisher>() as MyProducerService;
  41. Assert.NotNull(thingy);
  42. }
  43. [Fact]
  44. public void CanResolveCapOptions()
  45. {
  46. var services = new ServiceCollection();
  47. services.AddCap(x => { });
  48. var builder = services.BuildServiceProvider();
  49. var capOptions = builder.GetService<IOptions<CapOptions>>().Value;
  50. Assert.NotNull(capOptions);
  51. }
  52. private class MyProducerService : ICapPublisher
  53. {
  54. public IServiceProvider ServiceProvider { get; }
  55. public AsyncLocal<ICapTransaction> Transaction { get; }
  56. public Task PublishAsync<T>(string name, T contentObj, string callbackName = null,
  57. CancellationToken cancellationToken = default(CancellationToken))
  58. {
  59. throw new NotImplementedException();
  60. }
  61. public Task PublishAsync<T>(string name, T contentObj, IDictionary<string, string> optionHeaders = null,
  62. CancellationToken cancellationToken = default)
  63. {
  64. throw new NotImplementedException();
  65. }
  66. public void Publish<T>(string name, T contentObj, string callbackName = null)
  67. {
  68. throw new NotImplementedException();
  69. }
  70. public void Publish<T>(string name, T contentObj, IDictionary<string, string> headers)
  71. {
  72. throw new NotImplementedException();
  73. }
  74. }
  75. }
  76. }