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.

JobProcessingServerTest.cs 6.2 KiB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. }