Commit 3c2f51e8 authored by yangxiaodong's avatar yangxiaodong

Add unit test

parent d477e602
using System;
using System.Data.SqlClient;
namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public static class ConnectionUtil
{
private const string DatabaseVariable = "Cap_SqlServer_DatabaseName";
private const string ConnectionStringTemplateVariable = "Cap_SqlServer_ConnectionStringTemplate";
private const string MasterDatabaseName = "master";
private const string DefaultDatabaseName = @"DotNetCore.CAP.EntityFrameworkCore.Test";
private const string DefaultConnectionStringTemplate = @"Server=.\sqlexpress;Database={0};Trusted_Connection=True;";
public static string GetDatabaseName()
{
return Environment.GetEnvironmentVariable(DatabaseVariable) ?? DefaultDatabaseName;
}
public static string GetMasterConnectionString()
{
return string.Format(GetConnectionStringTemplate(), MasterDatabaseName);
}
public static string GetConnectionString()
{
return string.Format(GetConnectionStringTemplate(), GetDatabaseName());
}
private static string GetConnectionStringTemplate()
{
return
Environment.GetEnvironmentVariable(ConnectionStringTemplateVariable) ??
DefaultConnectionStringTemplate;
}
public static SqlConnection CreateConnection(string connectionString = null)
{
connectionString = connectionString ?? GetConnectionString();
var connection = new SqlConnection(connectionString);
connection.Open();
return connection;
}
}
}
using System.Data;
using Dapper;
using Microsoft.EntityFrameworkCore;
namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public abstract class DatabaseTestHost : TestHost
{
private static bool _sqlObjectInstalled;
protected override void PostBuildServices()
{
base.PostBuildServices();
InitializeDatabase();
}
public override void Dispose()
{
DeleteAllData();
base.Dispose();
}
private void InitializeDatabase()
{
if (!_sqlObjectInstalled)
{
using (CreateScope())
{
var context = GetService<CapDbContext>();
context.Database.EnsureDeleted();
context.Database.Migrate();
_sqlObjectInstalled = true;
}
}
}
private void DeleteAllData()
{
using (CreateScope())
{
var context = GetService<CapDbContext>();
var commands = new[]
{
"DISABLE TRIGGER ALL ON ?",
"ALTER TABLE ? NOCHECK CONSTRAINT ALL",
"DELETE FROM ?",
"ALTER TABLE ? CHECK CONSTRAINT ALL",
"ENABLE TRIGGER ALL ON ?"
};
foreach (var command in commands)
{
context.Database.GetDbConnection().Execute(
"sp_MSforeachtable",
new { command1 = command },
commandType: CommandType.StoredProcedure);
}
}
}
}
}
...@@ -7,7 +7,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test ...@@ -7,7 +7,7 @@ namespace DotNetCore.CAP.EntityFrameworkCore.Test
public static class DbUtil public static class DbUtil
{ {
public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null) { public static IServiceCollection ConfigureDbServices(string connectionString, IServiceCollection services = null) {
return ConfigureDbServices<ConsistencyDbContext>(connectionString, services); return ConfigureDbServices<CapDbContext>(connectionString, services);
} }
public static IServiceCollection ConfigureDbServices<TContext>(string connectionString, IServiceCollection services = null) where TContext : DbContext { public static IServiceCollection ConfigureDbServices<TContext>(string connectionString, IServiceCollection services = null) where TContext : DbContext {
......
//using System.Threading.Tasks; using System.Threading.Tasks;
//using DotNetCore.CAP.Infrastructure; using DotNetCore.CAP.Infrastructure;
//using DotNetCore.CAP.Store; using Microsoft.AspNetCore.Builder.Internal;
//using Microsoft.AspNetCore.Builder.Internal; using Microsoft.AspNetCore.Testing.xunit;
//using Microsoft.AspNetCore.Testing.xunit; using Microsoft.EntityFrameworkCore;
//using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.DependencyInjection; using Xunit;
//using Xunit;
namespace DotNetCore.CAP.EntityFrameworkCore.Test
//namespace DotNetCore.CAP.EntityFrameworkCore.Test {
//{ public class DefaultPocoTest : IClassFixture<ScratchDatabaseFixture>
// public class DefaultPocoTest : IClassFixture<ScratchDatabaseFixture> {
// { private readonly ApplicationBuilder _builder;
// private readonly ApplicationBuilder _builder;
public DefaultPocoTest(ScratchDatabaseFixture fixture)
// public DefaultPocoTest(ScratchDatabaseFixture fixture) { {
// var services = new ServiceCollection(); var services = new ServiceCollection();
// services services
// .AddDbContext<ConsistencyDbContext>(o => o.UseSqlServer(fixture.ConnectionString)) .AddDbContext<CapDbContext>(o => o.UseSqlServer(fixture.ConnectionString))
// .AddConsistency() .AddConsistency()
// .AddEntityFrameworkStores<ConsistencyDbContext>(); .AddEntityFrameworkStores<CapDbContext>();
// services.AddLogging(); services.AddLogging();
// var provider = services.BuildServiceProvider(); var provider = services.BuildServiceProvider();
// _builder = new ApplicationBuilder(provider); _builder = new ApplicationBuilder(provider);
// using (var scoped = provider.GetRequiredService<IServiceScopeFactory>().CreateScope()) using (var scoped = provider.GetRequiredService<IServiceScopeFactory>().CreateScope())
// using (var db = scoped.ServiceProvider.GetRequiredService<ConsistencyDbContext>()) { using (var db = scoped.ServiceProvider.GetRequiredService<CapDbContext>())
// db.Database.EnsureCreated(); {
// } db.Database.EnsureCreated();
// } }
}
// [ConditionalFact]
// [FrameworkSkipCondition(RuntimeFrameworks.Mono)] [ConditionalFact]
// [OSSkipCondition(OperatingSystems.Linux)] [FrameworkSkipCondition(RuntimeFrameworks.Mono)]
// [OSSkipCondition(OperatingSystems.MacOSX)] [OSSkipCondition(OperatingSystems.Linux)]
// public async Task EnsureStartupUsageWorks() { [OSSkipCondition(OperatingSystems.MacOSX)]
// var messageStore = _builder.ApplicationServices.GetRequiredService<IConsistencyMessageStore>(); public async Task EnsureStartupUsageWorks()
// var messageManager = _builder.ApplicationServices.GetRequiredService<IConsistencyMessageStore >(); {
var messageStore = _builder.ApplicationServices.GetRequiredService<ICapMessageStore>();
// Assert.NotNull(messageStore); var messageManager = _builder.ApplicationServices.GetRequiredService<ICapMessageStore>();
// Assert.NotNull(messageManager);
Assert.NotNull(messageStore);
// var user = new ConsistencyMessage(); Assert.NotNull(messageManager);
// var operateResult = await messageManager.CreateAsync(user); var message = new CapSentMessage();
// Assert.True(operateResult.Succeeded);
var operateResult = await messageManager.StoreSentMessageAsync(message);
// operateResult = await messageManager.DeleteAsync(user); Assert.True(operateResult.Succeeded);
// Assert.True(operateResult.Succeeded);
// } operateResult = await messageManager.RemoveSentMessageAsync(message);
// } Assert.True(operateResult.Succeeded);
//} }
\ No newline at end of file }
}
\ No newline at end of file
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Dapper" Version="1.50.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="xunit" Version="2.2.0" /> <PackageReference Include="xunit" Version="2.2.0" />
......
using Xunit;
namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public class EnsuranceTest : DatabaseTestHost
{
[Fact]
public void Ensure()
{
}
}
}
//using System; using System;
//using System.Linq; using System.Linq;
//using System.Threading.Tasks; using System.Threading.Tasks;
//using DotNetCore.CAP.Infrastructure; using DotNetCore.CAP.Infrastructure;
//using DotNetCore.CAP.Store; using DotNetCore.CAP.Test;
//using DotNetCore.CAP.Test; using Microsoft.AspNetCore.Testing;
//using Microsoft.AspNetCore.Testing; using Microsoft.AspNetCore.Testing.xunit;
//using Microsoft.AspNetCore.Testing.xunit; using Microsoft.EntityFrameworkCore;
//using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.DependencyInjection; using Xunit;
//using Xunit;
//namespace DotNetCore.CAP.EntityFrameworkCore.Test namespace DotNetCore.CAP.EntityFrameworkCore.Test
//{ {
// public class MessageStoreTest : MessageManagerTestBase<ConsistencyMessage>, IClassFixture<ScratchDatabaseFixture> public class MessageStoreTest : MessageManagerTestBase, IClassFixture<ScratchDatabaseFixture>
// { {
// private readonly ScratchDatabaseFixture _fixture; private readonly ScratchDatabaseFixture _fixture;
// public MessageStoreTest(ScratchDatabaseFixture fixture) { public MessageStoreTest(ScratchDatabaseFixture fixture)
// _fixture = fixture; {
// } _fixture = fixture;
}
// protected override bool ShouldSkipDbTests() { protected override bool ShouldSkipDbTests()
// return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows; {
// } return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows;
}
// public class ApplicationDbContext : ConsistencyDbContext public class ApplicationDbContext : CapDbContext
// { {
// public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
// } {
// } }
}
// [ConditionalFact] [ConditionalFact]
// [FrameworkSkipCondition(RuntimeFrameworks.Mono)] [FrameworkSkipCondition(RuntimeFrameworks.Mono)]
// [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.Linux)]
// [OSSkipCondition(OperatingSystems.MacOSX)] [OSSkipCondition(OperatingSystems.MacOSX)]
// public void CanCreateMessageUsingEF() { public void CanCreateSentMessageUsingEF()
// using (var db = CreateContext()) { {
// var guid = Guid.NewGuid().ToString(); using (var db = CreateContext())
// db.Messages.Add(new ConsistencyMessage { {
// Id = guid, var guid = Guid.NewGuid().ToString();
// Payload = "this is message body", db.CapSentMessages.Add(new CapSentMessage
// Status = MessageStatus.WaitForSend, {
// SendTime = DateTime.Now, Id = guid,
// UpdateTime = DateTime.Now Content = "this is message body",
// }); StateName = StateName.Enqueued
});
// db.SaveChanges(); db.SaveChanges();
// Assert.True(db.Messages.Any(u => u.Id == guid)); Assert.True(db.CapSentMessages.Any(u => u.Id == guid));
// Assert.NotNull(db.Messages.FirstOrDefault(u => u.Status == MessageStatus.WaitForSend)); Assert.NotNull(db.CapSentMessages.FirstOrDefault(u => u.StateName == StateName.Enqueued));
// } }
// } }
// [ConditionalFact] [ConditionalFact]
// [FrameworkSkipCondition(RuntimeFrameworks.Mono)] [FrameworkSkipCondition(RuntimeFrameworks.Mono)]
// [OSSkipCondition(OperatingSystems.Linux)] [OSSkipCondition(OperatingSystems.Linux)]
// [OSSkipCondition(OperatingSystems.MacOSX)] [OSSkipCondition(OperatingSystems.MacOSX)]
// public async Task CanCreateUsingManager() { public async Task CanCreateUsingManager()
// var manager = CreateManager(); {
// var guid = Guid.NewGuid().ToString(); var manager = CreateManager();
// var message = new ConsistencyMessage { var guid = Guid.NewGuid().ToString();
// Id = guid, var message = new CapSentMessage
// Payload = "this is message body", {
// Status = MessageStatus.WaitForSend, Id = guid,
// SendTime = DateTime.Now, Content = "this is message body",
// UpdateTime = DateTime.Now StateName = StateName.Enqueued,
// }; };
// var result = await manager.CreateAsync(message); var result = await manager.StoreSentMessageAsync(message);
// Assert.NotNull(result); Assert.NotNull(result);
// Assert.True(result.Succeeded); Assert.True(result.Succeeded);
// result = await manager.DeleteAsync(message); result = await manager.RemoveSentMessageAsync(message);
// Assert.NotNull(result); Assert.NotNull(result);
// Assert.True(result.Succeeded); Assert.True(result.Succeeded);
// } }
// public ConsistencyDbContext CreateContext(bool delete = false) { public CapDbContext CreateContext(bool delete = false)
// var db = DbUtil.Create<ConsistencyDbContext>(_fixture.ConnectionString); {
// if (delete) { var db = DbUtil.Create<CapDbContext>(_fixture.ConnectionString);
// db.Database.EnsureDeleted(); if (delete)
// } {
// db.Database.EnsureCreated(); db.Database.EnsureDeleted();
// return db; }
// } db.Database.EnsureCreated();
return db;
}
// protected override object CreateTestContext() { protected override object CreateTestContext()
// return CreateContext(); {
// } return CreateContext();
}
// protected override ConsistencyMessage CreateTestMessage(string payload = "") { protected override void AddMessageStore(IServiceCollection services, object context = null)
// return new ConsistencyMessage { {
// Payload = payload services.AddSingleton<ICapMessageStore>(new CapMessageStore<CapDbContext>((CapDbContext)context));
// }; }
// }
// protected override void AddMessageStore(IServiceCollection services, object context = null) { protected override CapSentMessage CreateTestSentMessage(string content = "")
// services.AddSingleton<IConsistencyMessageStore>(new ConsistencyMessageStore<ConsistencyDbContext>((ConsistencyDbContext)context)); {
// } return new CapSentMessage
// } {
Content = content
};
}
// public class ApplicationMessage : ConsistencyMessage { } protected override CapReceivedMessage CreateTestReceivedMessage(string content = "")
//} {
\ No newline at end of file return new CapReceivedMessage()
{
Content = content
};
}
}
}
\ No newline at end of file
//using System;
//using DotNetCore.CAP.Infrastructure;
//using DotNetCore.CAP.Store;
//using DotNetCore.CAP.Test;
//using Microsoft.AspNetCore.Testing;
//using Microsoft.Extensions.DependencyInjection;
//using Xunit;
//namespace DotNetCore.CAP.EntityFrameworkCore.Test
//{
// public class MessageStoreWithGenericsTest : MessageManagerTestBase<MessageWithGenerics, string>, IClassFixture<ScratchDatabaseFixture>
// {
// private readonly ScratchDatabaseFixture _fixture;
// public MessageStoreWithGenericsTest(ScratchDatabaseFixture fixture) {
// _fixture = fixture;
// }
// protected override void AddMessageStore(IServiceCollection services, object context = null) {
// services.AddSingleton<IConsistencyMessageStore>(new MessageStoreWithGenerics((ContextWithGenerics)context));
// }
// protected override object CreateTestContext() {
// return CreateContext();
// }
// public ContextWithGenerics CreateContext() {
// var db = DbUtil.Create<ContextWithGenerics>(_fixture.ConnectionString);
// db.Database.EnsureCreated();
// return db;
// }
// protected override MessageWithGenerics CreateTestMessage(string payload = "") {
// return new MessageWithGenerics() {
// Payload = payload,
// SendTime = DateTime.Now,
// Status = MessageStatus.WaitForSend,
// UpdateTime = DateTime.Now
// };
// }
// protected override bool ShouldSkipDbTests() {
// return TestPlatformHelper.IsMono || !TestPlatformHelper.IsWindows;
// }
// }
// public class MessageWithGenerics : ConsistencyMessage
// {
// }
// public class MessageStoreWithGenerics : ConsistencyMessageStore<ContextWithGenerics>
// {
// public MessageStoreWithGenerics(ContextWithGenerics context) : base(context) {
// }
// }
// public class ContextWithGenerics : ConsistencyDbContext
// {
// public ContextWithGenerics() {
// }
// }
//}
\ No newline at end of file
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace DotNetCore.CAP.EntityFrameworkCore.Test
{
public abstract class TestHost : IDisposable
{
protected IServiceCollection _services;
private IServiceProvider _provider;
private IServiceProvider _scopedProvider;
public TestHost()
{
CreateServiceCollection();
PreBuildServices();
BuildServices();
PostBuildServices();
}
protected IServiceProvider Provider => _scopedProvider ?? _provider;
private void CreateServiceCollection()
{
var services = new ServiceCollection();
services.AddOptions();
services.AddLogging();
var connectionString = ConnectionUtil.GetConnectionString();
//services.AddSingleton(new SqlServerOptions { ConnectionString = connectionString });
services.AddDbContext<CapDbContext>(options => options.UseSqlServer(connectionString));
_services = services;
}
protected virtual void PreBuildServices()
{
}
private void BuildServices()
{
_provider = _services.BuildServiceProvider();
}
protected virtual void PostBuildServices()
{
}
public IDisposable CreateScope()
{
var scope = CreateScope(_provider);
var loc = scope.ServiceProvider;
_scopedProvider = loc;
return new DelegateDisposable(() =>
{
if (_scopedProvider == loc)
{
_scopedProvider = null;
}
scope.Dispose();
});
}
public IServiceScope CreateScope(IServiceProvider provider)
{
var scope = provider.GetService<IServiceScopeFactory>().CreateScope();
return scope;
}
public T GetService<T>() => Provider.GetService<T>();
public T Ensure<T>(ref T service)
where T : class
=> service ?? (service = GetService<T>());
public virtual void Dispose()
{
(_provider as IDisposable)?.Dispose();
}
private class DelegateDisposable : IDisposable
{
private Action _dispose;
public DelegateDisposable(Action dispose)
{
_dispose = dispose;
}
public void Dispose()
{
_dispose();
}
}
}
}
{ {
"Test": { "Test": {
"SqlServer": { "SqlServer": {
"DefaultConnectionString": "Server=(localdb)\\MSSqlLocaldb;Integrated Security=true;MultipleActiveResultSets=true;Connect Timeout=30" "DefaultConnectionString": "Server=192.168.2.206;Initial Catalog=Test;User Id=cmswuliu;Password=h7xY81agBn*Veiu3;MultipleActiveResultSets=True"
} }
} }
} }
\ No newline at end of file
...@@ -32,8 +32,4 @@ ...@@ -32,8 +32,4 @@
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Job\" />
</ItemGroup>
</Project> </Project>
using System;
using System.Collections.Generic;
using System.Text;
using DotNetCore.CAP.Job;
using Xunit;
namespace DotNetCore.CAP.Test.Job
{
public class ComputedJobTest
{
[Fact]
public void UpdateNext_LastRunNever_SchedulesNow()
{
// Arrange
var now = new DateTime(2000, 1, 1, 8, 0, 0);
var cronJob = new CronJob(Cron.Daily());
var computed = new ComputedCronJob(cronJob);
// Act
computed.UpdateNext(now);
// Assert
Assert.Equal(computed.Next, now);
}
[Fact]
public void UpdateNext_LastRun_BeforePrev_SchedulesNow()
{
// Arrange
var now = new DateTime(2000, 1, 1, 8, 0, 0);
var cronJob = new CronJob(Cron.Daily(), now.Subtract(TimeSpan.FromDays(2)));
var computed = new ComputedCronJob(cronJob);
// Act
computed.UpdateNext(now);
// Assert
Assert.Equal(computed.Next, now);
}
[Fact]
public void UpdateNext_LastRun_AfterPrev_SchedulesNormal()
{
// Arrange
var now = new DateTime(2000, 1, 1, 8, 0, 0);
var cronJob = new CronJob(Cron.Daily(), now.Subtract(TimeSpan.FromSeconds(5)));
var computed = new ComputedCronJob(cronJob);
// Act
computed.UpdateNext(now);
// Assert
Assert.True(computed.Next > now);
}
}
}
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(); }
//}
}
}
//using System; using System;
//using System.Collections.Generic; using System.Threading.Tasks;
//using System.Linq; using DotNetCore.CAP.Infrastructure;
//using System.Linq.Expressions; using Microsoft.AspNetCore.Http;
//using System.Security.Claims; using Microsoft.Extensions.DependencyInjection;
//using System.Threading.Tasks; using Microsoft.Extensions.Logging;
//using DotNetCore.CAP.Infrastructure; using Xunit;
//using DotNetCore.CAP.Store;
//using Microsoft.AspNetCore.Builder; namespace DotNetCore.CAP.Test
//using Microsoft.AspNetCore.Http; {
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Logging; public abstract class MessageManagerTestBase
//using Xunit; {
private const string NullValue = "(null)";
//namespace DotNetCore.CAP.Test
//{ protected virtual bool ShouldSkipDbTests()
// public abstract class MessageManagerTestBase<TMessage> : MessageManagerTestBase<TMessage, string> {
// where TMessage : ConsistencyMessage return false;
// { }
// }
protected virtual void SetupMessageServices(IServiceCollection services, object context = null)
// public abstract class MessageManagerTestBase<TMessage, TKey> {
// where TMessage : ConsistencyMessage services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// where TKey : IEquatable<TKey> services.AddConsistency();
// { AddMessageStore(services, context);
// private const string NullValue = "(null)";
services.AddSingleton<ILogger<ICapMessageStore>>(new TestLogger<ICapMessageStore>());
// protected virtual bool ShouldSkipDbTests() { }
// return false;
// } protected virtual ICapMessageStore CreateManager(object context = null, IServiceCollection services = null, Action<IServiceCollection> configureServices = null)
{
// protected virtual void SetupMessageServices(IServiceCollection services, object context = null) { if (services == null)
// services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); {
// services.AddConsistency(); services = new ServiceCollection();
// AddMessageStore(services, context); }
if (context == null)
// services.AddSingleton<ILogger<IConsistencyMessageStore >>(new TestLogger<IConsistencyMessageStore >()); {
// } context = CreateTestContext();
}
// protected virtual IConsistencyMessageStore CreateManager(object context = null, IServiceCollection services = null, Action<IServiceCollection> configureServices = null) { SetupMessageServices(services, context);
// if (services == null) {
// services = new ServiceCollection(); configureServices?.Invoke(services);
// }
// if (context == null) { return services.BuildServiceProvider().GetService<ICapMessageStore>();
// context = CreateTestContext(); }
// }
// SetupMessageServices(services, context); protected abstract object CreateTestContext();
// configureServices?.Invoke(services); protected abstract CapSentMessage CreateTestSentMessage(string content = "");
protected abstract CapReceivedMessage CreateTestReceivedMessage(string content = "");
// return services.BuildServiceProvider().GetService<IConsistencyMessageStore >();
// } protected abstract void AddMessageStore(IServiceCollection services, object context = null);
// protected abstract object CreateTestContext(); [Fact]
public async Task CanDeleteSentMessage()
// protected abstract TMessage CreateTestMessage(string payload = ""); {
if (ShouldSkipDbTests())
// protected abstract void AddMessageStore(IServiceCollection services, object context = null); {
return;
// [Fact] }
// public async Task CanDeleteMessage() {
// if (ShouldSkipDbTests()) { var manager = CreateManager();
// return; var message = CreateTestSentMessage();
// } var operateResult = await manager.StoreSentMessageAsync(message);
Assert.NotNull(operateResult);
// var manager = CreateManager(); Assert.True(operateResult.Succeeded);
// var message = CreateTestMessage();
// var operateResult = await manager.CreateAsync(message); operateResult = await manager.RemoveSentMessageAsync(message);
// Assert.NotNull(operateResult); Assert.NotNull(operateResult);
// Assert.True(operateResult.Succeeded); Assert.True(operateResult.Succeeded);
}
// var messageId = await manager.GeConsistencyMessageIdAsync(message);
// operateResult = await manager.DeleteAsync(message); [Fact]
// Assert.Null(await manager.FindByIdAsync(messageId)); public async Task CanUpdateReceivedMessage()
// } {
if (ShouldSkipDbTests())
// [Fact] {
// public async Task CanFindById() { return;
// if (ShouldSkipDbTests()) { }
// return;
// } var manager = CreateManager();
// var manager = CreateManager(); var message = CreateTestReceivedMessage();
// var message = CreateTestMessage(); var operateResult = await manager.StoreReceivedMessageAsync(message);
Assert.NotNull(operateResult);
// var operateResult = await manager.CreateAsync(message); Assert.True(operateResult.Succeeded);
// Assert.NotNull(operateResult);
// Assert.True(operateResult.Succeeded); message.StateName = StateName.Processing;
operateResult = await manager.UpdateReceivedMessageAsync(message);
// var messageId = await manager.GeConsistencyMessageIdAsync(message); Assert.NotNull(operateResult);
// Assert.NotNull(await manager.FindByIdAsync(messageId)); Assert.True(operateResult.Succeeded);
// } }
// }
[Fact]
//} public async Task CanGetNextSendMessage()
\ No newline at end of file {
if (ShouldSkipDbTests())
{
return;
}
var manager = CreateManager();
var message = CreateTestSentMessage();
var operateResult = await manager.StoreSentMessageAsync(message);
Assert.NotNull(operateResult);
Assert.True(operateResult.Succeeded);
var storeMessage = await manager.GetNextSentMessageToBeEnqueuedAsync();
Assert.Equal(message, storeMessage);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment