25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

139 lines
4.1 KiB

  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using DotNetCore.CAP.Abstractions;
  5. using DotNetCore.CAP.Models;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Options;
  8. using Xunit;
  9. namespace DotNetCore.CAP.Test
  10. {
  11. public class CapBuilderTest
  12. {
  13. [Fact]
  14. public void CanCreateInstanceAndGetService()
  15. {
  16. var services = new ServiceCollection();
  17. services.AddSingleton<ICapPublisher, MyProducerService>();
  18. var builder = new CapBuilder(services);
  19. Assert.NotNull(builder);
  20. var count = builder.Services.Count;
  21. Assert.Equal(1, count);
  22. var provider = services.BuildServiceProvider();
  23. var capPublisher = provider.GetService<ICapPublisher>();
  24. Assert.NotNull(capPublisher);
  25. }
  26. [Fact]
  27. public void CanAddCapService()
  28. {
  29. var services = new ServiceCollection();
  30. services.AddCap(x => { });
  31. var builder = services.BuildServiceProvider();
  32. var markService = builder.GetService<CapMarkerService>();
  33. Assert.NotNull(markService);
  34. }
  35. [Fact]
  36. public void CanOverridePublishService()
  37. {
  38. var services = new ServiceCollection();
  39. services.AddCap(x => { }).AddProducerService<MyProducerService>();
  40. var thingy = services.BuildServiceProvider()
  41. .GetRequiredService<ICapPublisher>() as MyProducerService;
  42. Assert.NotNull(thingy);
  43. }
  44. [Fact]
  45. public void CanOverrideContentSerialize()
  46. {
  47. var services = new ServiceCollection();
  48. services.AddCap(x => { }).AddContentSerializer<MyContentSerializer>();
  49. var thingy = services.BuildServiceProvider()
  50. .GetRequiredService<IContentSerializer>() as MyContentSerializer;
  51. Assert.NotNull(thingy);
  52. }
  53. [Fact]
  54. public void CanOverrideMessagePack()
  55. {
  56. var services = new ServiceCollection();
  57. services.AddCap(x => { }).AddMessagePacker<MyMessagePacker>();
  58. var thingy = services.BuildServiceProvider()
  59. .GetRequiredService<IMessagePacker>() as MyMessagePacker;
  60. Assert.NotNull(thingy);
  61. }
  62. [Fact]
  63. public void CanResolveCapOptions()
  64. {
  65. var services = new ServiceCollection();
  66. services.AddCap(x => { });
  67. var builder = services.BuildServiceProvider();
  68. var capOptions = builder.GetService<IOptions<CapOptions>>().Value;
  69. Assert.NotNull(capOptions);
  70. }
  71. private class MyMessagePacker : IMessagePacker
  72. {
  73. public string Pack(CapMessage obj)
  74. {
  75. throw new NotImplementedException();
  76. }
  77. public CapMessage UnPack(string packingMessage)
  78. {
  79. throw new NotImplementedException();
  80. }
  81. }
  82. private class MyContentSerializer : IContentSerializer
  83. {
  84. public T DeSerialize<T>(string content)
  85. {
  86. throw new NotImplementedException();
  87. }
  88. public object DeSerialize(string content, Type type)
  89. {
  90. throw new NotImplementedException();
  91. }
  92. public string Serialize<T>(T obj)
  93. {
  94. throw new NotImplementedException();
  95. }
  96. }
  97. private class MyProducerService : ICapPublisher
  98. {
  99. public IServiceProvider ServiceProvider { get; }
  100. public AsyncLocal<ICapTransaction> Transaction { get; }
  101. public Task PublishAsync<T>(string name, T contentObj, string callbackName = null,
  102. CancellationToken cancellationToken = default(CancellationToken))
  103. {
  104. throw new NotImplementedException();
  105. }
  106. public void Publish<T>(string name, T contentObj, string callbackName = null)
  107. {
  108. throw new NotImplementedException();
  109. }
  110. }
  111. }
  112. }