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.
 
 
 

146 regels
4.4 KiB

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