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.

DefaultDispatcherTest.cs 3.0 KiB

7 years ago
7 years ago
7 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using DotNetCore.CAP.Models;
  5. using DotNetCore.CAP.Processor;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Options;
  8. using Moq;
  9. using Xunit;
  10. namespace DotNetCore.CAP.Test
  11. {
  12. public class DefaultDispatcherTest
  13. {
  14. private CancellationTokenSource _cancellationTokenSource;
  15. private ProcessingContext _context;
  16. private IServiceProvider _provider;
  17. private Mock<IStorageConnection> _mockStorageConnection;
  18. private Mock<IQueueExecutorFactory> _mockQueueExecutorFactory;
  19. private Mock<IQueueExecutor> _mockQueueExecutor;
  20. public DefaultDispatcherTest()
  21. {
  22. _mockStorageConnection = new Mock<IStorageConnection>();
  23. _mockQueueExecutorFactory = new Mock<IQueueExecutorFactory>();
  24. _mockQueueExecutor = new Mock<IQueueExecutor>();
  25. _mockQueueExecutorFactory.Setup(x => x.GetInstance(MessageType.Publish)).Returns(_mockQueueExecutor.Object);
  26. _cancellationTokenSource = new CancellationTokenSource();
  27. var services = new ServiceCollection();
  28. services.AddTransient<DefaultDispatcher>();
  29. services.AddLogging();
  30. services.Configure<IOptions<CapOptions>>(x => { });
  31. services.AddOptions();
  32. services.AddSingleton(_mockStorageConnection.Object);
  33. services.AddSingleton(_mockQueueExecutorFactory.Object);
  34. _provider = services.BuildServiceProvider();
  35. _context = new ProcessingContext(_provider, _cancellationTokenSource.Token);
  36. }
  37. [Fact]
  38. public void MockTest()
  39. {
  40. Assert.NotNull(_provider.GetServices<IStorageConnection>());
  41. }
  42. [Fact]
  43. public async void ProcessAsync_CancellationTokenCancelled_ThrowsImmediately()
  44. {
  45. // Arrange
  46. _cancellationTokenSource.Cancel();
  47. var fixture = Create();
  48. // Act
  49. await Assert.ThrowsAsync<OperationCanceledException>(() => fixture.ProcessAsync(_context));
  50. }
  51. [Fact]
  52. public async Task ProcessAsync()
  53. {
  54. // Arrange
  55. var job = new CapPublishedMessage
  56. {
  57. };
  58. var mockFetchedJob = Mock.Get(Mock.Of<IFetchedMessage>(fj => fj.MessageId == 42 && fj.MessageType == MessageType.Publish));
  59. _mockStorageConnection
  60. .Setup(m => m.FetchNextMessageAsync())
  61. .ReturnsAsync(mockFetchedJob.Object).Verifiable();
  62. _mockQueueExecutor
  63. .Setup(x => x.ExecuteAsync(_mockStorageConnection.Object, mockFetchedJob.Object))
  64. .Returns(Task.FromResult(OperateResult.Success));
  65. var fixture = Create();
  66. // Act
  67. await fixture.ProcessAsync(_context);
  68. // Assert
  69. _mockStorageConnection.VerifyAll();
  70. }
  71. private DefaultDispatcher Create()
  72. => _provider.GetService<DefaultDispatcher>();
  73. }
  74. }