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.
 
 
 

186 lines
6.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.Job;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Moq;
  10. using Xunit;
  11. namespace DotNetCore.CAP.Test.Job
  12. {
  13. public class JobProcessingServerTest
  14. {
  15. private CancellationTokenSource _cancellationTokenSource;
  16. private ProcessingContext _context;
  17. private CapOptions _options;
  18. private IServiceProvider _provider;
  19. private Mock<ICapMessageStore> _mockStorage;
  20. public JobProcessingServerTest()
  21. {
  22. _options = new CapOptions()
  23. {
  24. PollingDelay = 0
  25. };
  26. _mockStorage = new Mock<ICapMessageStore>();
  27. _cancellationTokenSource = new CancellationTokenSource();
  28. var services = new ServiceCollection();
  29. services.AddTransient<JobProcessingServer>();
  30. services.AddTransient<DefaultCronJobRegistry>();
  31. services.AddLogging();
  32. services.AddSingleton(_options);
  33. services.AddSingleton(_mockStorage.Object);
  34. _provider = services.BuildServiceProvider();
  35. _context = new ProcessingContext(_provider, null, _cancellationTokenSource.Token);
  36. }
  37. //[Fact]
  38. //public async Task ProcessAsync_CancellationTokenCancelled_ThrowsImmediately()
  39. //{
  40. // // Arrange
  41. // _cancellationTokenSource.Cancel();
  42. // var fixture = Create();
  43. // // Act
  44. // await Assert.ThrowsAsync<OperationCanceledException>(() => fixture.s(_context));
  45. //}
  46. //[Fact]
  47. //public async Task ProcessAsync()
  48. //{
  49. // // Arrange
  50. // var job = new CronJob(
  51. // InvocationData.Serialize(
  52. // MethodInvocation.FromExpression(() => Method())).Serialize());
  53. // var mockFetchedJob = Mock.Get(Mock.Of<IFetchedJob>(fj => fj.JobId == 42));
  54. // _mockStorageConnection
  55. // .Setup(m => m.FetchNextJobAsync())
  56. // .ReturnsAsync(mockFetchedJob.Object).Verifiable();
  57. // _mockStorageConnection
  58. // .Setup(m => m.GetJobAsync(42))
  59. // .ReturnsAsync(job).Verifiable();
  60. // var fixture = Create();
  61. // // Act
  62. // fixture.Start();
  63. // // Assert
  64. // _mockStorageConnection.VerifyAll();
  65. // _mockStateChanger.Verify(m => m.ChangeState(job, It.IsAny<SucceededState>(), It.IsAny<IStorageTransaction>()));
  66. // mockFetchedJob.Verify(m => m.Requeue(), Times.Never);
  67. // mockFetchedJob.Verify(m => m.RemoveFromQueue());
  68. //}
  69. //[Fact]
  70. //public async Task ProcessAsync_Exception()
  71. //{
  72. // // Arrange
  73. // var job = new Job(
  74. // InvocationData.Serialize(
  75. // MethodInvocation.FromExpression(() => Throw())).Serialize());
  76. // var mockFetchedJob = Mock.Get(Mock.Of<IFetchedJob>(fj => fj.JobId == 42));
  77. // _mockStorageConnection
  78. // .Setup(m => m.FetchNextJobAsync())
  79. // .ReturnsAsync(mockFetchedJob.Object);
  80. // _mockStorageConnection
  81. // .Setup(m => m.GetJobAsync(42))
  82. // .ReturnsAsync(job);
  83. // _mockStateChanger.Setup(m => m.ChangeState(job, It.IsAny<IState>(), It.IsAny<IStorageTransaction>()))
  84. // .Throws<Exception>();
  85. // var fixture = Create();
  86. // // Act
  87. // await fixture.ProcessAsync(_context);
  88. // // Assert
  89. // job.Retries.Should().Be(0);
  90. // mockFetchedJob.Verify(m => m.Requeue());
  91. //}
  92. //[Fact]
  93. //public async Task ProcessAsync_JobThrows()
  94. //{
  95. // // Arrange
  96. // var job = new Job(
  97. // InvocationData.Serialize(
  98. // MethodInvocation.FromExpression(() => Throw())).Serialize());
  99. // var mockFetchedJob = Mock.Get(Mock.Of<IFetchedJob>(fj => fj.JobId == 42));
  100. // _mockStorageConnection
  101. // .Setup(m => m.FetchNextJobAsync())
  102. // .ReturnsAsync(mockFetchedJob.Object).Verifiable();
  103. // _mockStorageConnection
  104. // .Setup(m => m.GetJobAsync(42))
  105. // .ReturnsAsync(job).Verifiable();
  106. // var fixture = Create();
  107. // // Act
  108. // await fixture.ProcessAsync(_context);
  109. // // Assert
  110. // job.Retries.Should().Be(1);
  111. // _mockStorageTransaction.Verify(m => m.UpdateJob(job));
  112. // _mockStorageConnection.VerifyAll();
  113. // _mockStateChanger.Verify(m => m.ChangeState(job, It.IsAny<ScheduledState>(), It.IsAny<IStorageTransaction>()));
  114. // mockFetchedJob.Verify(m => m.RemoveFromQueue());
  115. //}
  116. //[Fact]
  117. //public async Task ProcessAsync_JobThrows_WithNoRetry()
  118. //{
  119. // // Arrange
  120. // var job = new Job(
  121. // InvocationData.Serialize(
  122. // MethodInvocation.FromExpression<NoRetryJob>(j => j.Throw())).Serialize());
  123. // var mockFetchedJob = Mock.Get(Mock.Of<IFetchedJob>(fj => fj.JobId == 42));
  124. // _mockStorageConnection
  125. // .Setup(m => m.FetchNextJobAsync())
  126. // .ReturnsAsync(mockFetchedJob.Object);
  127. // _mockStorageConnection
  128. // .Setup(m => m.GetJobAsync(42))
  129. // .ReturnsAsync(job);
  130. // var fixture = Create();
  131. // // Act
  132. // await fixture.ProcessAsync(_context);
  133. // // Assert
  134. // _mockStateChanger.Verify(m => m.ChangeState(job, It.IsAny<FailedState>(), It.IsAny<IStorageTransaction>()));
  135. //}
  136. private JobProcessingServer Create()
  137. => _provider.GetService<JobProcessingServer>();
  138. //public static void Method() { }
  139. //public static void Throw() { throw new Exception(); }
  140. //private class NoRetryJob : IRetryable
  141. //{
  142. // public RetryBehavior RetryBehavior => new RetryBehavior(false);
  143. // public void Throw() { throw new Exception(); }
  144. //}
  145. }
  146. }