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.
 
 
 

93 lines
3.2 KiB

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