|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using DotNetCore.CAP.Infrastructure;
- using DotNetCore.CAP.Job;
- using Microsoft.Extensions.DependencyInjection;
- using Moq;
- using Xunit;
-
- namespace DotNetCore.CAP.Test.Job
- {
- public class JobProcessingServerTest
- {
- private CancellationTokenSource _cancellationTokenSource;
- private ProcessingContext _context;
- private CapOptions _options;
- private IServiceProvider _provider;
- private Mock<ICapMessageStore> _mockStorage;
-
- public JobProcessingServerTest()
- {
- _options = new CapOptions()
- {
- PollingDelay = 0
- };
- _mockStorage = new Mock<ICapMessageStore>();
- _cancellationTokenSource = new CancellationTokenSource();
-
- var services = new ServiceCollection();
- services.AddTransient<JobProcessingServer>();
- services.AddTransient<DefaultCronJobRegistry>();
- services.AddLogging();
- services.AddSingleton(_options);
- services.AddSingleton(_mockStorage.Object);
- _provider = services.BuildServiceProvider();
-
- _context = new ProcessingContext(_provider, null, _cancellationTokenSource.Token);
- }
-
- //[Fact]
- //public async Task ProcessAsync_CancellationTokenCancelled_ThrowsImmediately()
- //{
- // // Arrange
- // _cancellationTokenSource.Cancel();
- // var fixture = Create();
-
- // // Act
- // await Assert.ThrowsAsync<OperationCanceledException>(() => fixture.s(_context));
- //}
-
- //[Fact]
- //public async Task ProcessAsync()
- //{
- // // Arrange
- // var job = new CronJob(
- // InvocationData.Serialize(
- // MethodInvocation.FromExpression(() => Method())).Serialize());
-
- // var mockFetchedJob = Mock.Get(Mock.Of<IFetchedJob>(fj => fj.JobId == 42));
-
- // _mockStorageConnection
- // .Setup(m => m.FetchNextJobAsync())
- // .ReturnsAsync(mockFetchedJob.Object).Verifiable();
-
- // _mockStorageConnection
- // .Setup(m => m.GetJobAsync(42))
- // .ReturnsAsync(job).Verifiable();
-
- // var fixture = Create();
-
- // // Act
- // fixture.Start();
-
- // // Assert
- // _mockStorageConnection.VerifyAll();
- // _mockStateChanger.Verify(m => m.ChangeState(job, It.IsAny<SucceededState>(), It.IsAny<IStorageTransaction>()));
- // mockFetchedJob.Verify(m => m.Requeue(), Times.Never);
- // mockFetchedJob.Verify(m => m.RemoveFromQueue());
- //}
-
- //[Fact]
- //public async Task ProcessAsync_Exception()
- //{
- // // Arrange
- // var job = new Job(
- // InvocationData.Serialize(
- // MethodInvocation.FromExpression(() => Throw())).Serialize());
-
- // var mockFetchedJob = Mock.Get(Mock.Of<IFetchedJob>(fj => fj.JobId == 42));
-
- // _mockStorageConnection
- // .Setup(m => m.FetchNextJobAsync())
- // .ReturnsAsync(mockFetchedJob.Object);
-
- // _mockStorageConnection
- // .Setup(m => m.GetJobAsync(42))
- // .ReturnsAsync(job);
-
- // _mockStateChanger.Setup(m => m.ChangeState(job, It.IsAny<IState>(), It.IsAny<IStorageTransaction>()))
- // .Throws<Exception>();
-
- // var fixture = Create();
-
- // // Act
- // await fixture.ProcessAsync(_context);
-
- // // Assert
- // job.Retries.Should().Be(0);
- // mockFetchedJob.Verify(m => m.Requeue());
- //}
-
- //[Fact]
- //public async Task ProcessAsync_JobThrows()
- //{
- // // Arrange
- // var job = new Job(
- // InvocationData.Serialize(
- // MethodInvocation.FromExpression(() => Throw())).Serialize());
-
- // var mockFetchedJob = Mock.Get(Mock.Of<IFetchedJob>(fj => fj.JobId == 42));
-
- // _mockStorageConnection
- // .Setup(m => m.FetchNextJobAsync())
- // .ReturnsAsync(mockFetchedJob.Object).Verifiable();
-
- // _mockStorageConnection
- // .Setup(m => m.GetJobAsync(42))
- // .ReturnsAsync(job).Verifiable();
-
- // var fixture = Create();
-
- // // Act
- // await fixture.ProcessAsync(_context);
-
- // // Assert
- // job.Retries.Should().Be(1);
- // _mockStorageTransaction.Verify(m => m.UpdateJob(job));
- // _mockStorageConnection.VerifyAll();
- // _mockStateChanger.Verify(m => m.ChangeState(job, It.IsAny<ScheduledState>(), It.IsAny<IStorageTransaction>()));
- // mockFetchedJob.Verify(m => m.RemoveFromQueue());
- //}
-
- //[Fact]
- //public async Task ProcessAsync_JobThrows_WithNoRetry()
- //{
- // // Arrange
- // var job = new Job(
- // InvocationData.Serialize(
- // MethodInvocation.FromExpression<NoRetryJob>(j => j.Throw())).Serialize());
-
- // var mockFetchedJob = Mock.Get(Mock.Of<IFetchedJob>(fj => fj.JobId == 42));
-
- // _mockStorageConnection
- // .Setup(m => m.FetchNextJobAsync())
- // .ReturnsAsync(mockFetchedJob.Object);
-
- // _mockStorageConnection
- // .Setup(m => m.GetJobAsync(42))
- // .ReturnsAsync(job);
-
- // var fixture = Create();
-
- // // Act
- // await fixture.ProcessAsync(_context);
-
- // // Assert
- // _mockStateChanger.Verify(m => m.ChangeState(job, It.IsAny<FailedState>(), It.IsAny<IStorageTransaction>()));
- //}
-
- private JobProcessingServer Create()
- => _provider.GetService<JobProcessingServer>();
-
- //public static void Method() { }
-
- //public static void Throw() { throw new Exception(); }
-
- //private class NoRetryJob : IRetryable
- //{
- // public RetryBehavior RetryBehavior => new RetryBehavior(false);
- // public void Throw() { throw new Exception(); }
- //}
- }
- }
|