Browse Source

Add unit test and update DemoApp.

pull/15282/head
maliming 4 years ago
parent
commit
e84367ceb3
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 3
      framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs
  2. 45
      framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobExecuter_Tests.cs
  3. 11
      framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyAsyncJob.cs
  4. 11
      framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyJob.cs
  5. 27
      modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/Program.cs
  6. 31
      modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/Program.cs
  7. 9
      modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/Program.cs
  8. 11
      modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs
  9. 10
      modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs

3
framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/BackgroundJobWorker.cs

@ -68,7 +68,8 @@ public class BackgroundJobWorker : AsyncPeriodicBackgroundWorkerBase, IBackgroun
var context = new JobExecutionContext( var context = new JobExecutionContext(
workerContext.ServiceProvider, workerContext.ServiceProvider,
jobConfiguration.JobType, jobConfiguration.JobType,
jobArgs); jobArgs,
workerContext.CancellationToken);
try try
{ {

45
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/BackgroundJobExecuter_Tests.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Shouldly; using Shouldly;
using Xunit; using Xunit;
@ -59,7 +60,7 @@ public class BackgroundJobExecuter_Tests : BackgroundJobsTestBase
jobObject.ExecutedValues.ShouldContain("42"); jobObject.ExecutedValues.ShouldContain("42");
} }
[Fact] [Fact]
public async Task Should_Change_TenantId_If_EventData_Is_MultiTenant() public async Task Should_Change_TenantId_If_EventData_Is_MultiTenant()
{ {
@ -77,7 +78,7 @@ public class BackgroundJobExecuter_Tests : BackgroundJobsTestBase
new MyJobArgs("42", tenantId) new MyJobArgs("42", tenantId)
) )
); );
await _backgroundJobExecuter.ExecuteAsync( await _backgroundJobExecuter.ExecuteAsync(
new JobExecutionContext( new JobExecutionContext(
ServiceProvider, ServiceProvider,
@ -91,4 +92,44 @@ public class BackgroundJobExecuter_Tests : BackgroundJobsTestBase
jobObject.TenantId.ShouldBe(tenantId); jobObject.TenantId.ShouldBe(tenantId);
asyncJobObject.TenantId.ShouldBe(tenantId); asyncJobObject.TenantId.ShouldBe(tenantId);
} }
[Fact]
public async Task Should_Cancel_Job()
{
//Arrange
var cts = new CancellationTokenSource();
cts.Cancel();
var jobObject = GetRequiredService<MyJob>();
jobObject.ExecutedValues.ShouldBeEmpty();
//Act
await _backgroundJobExecuter.ExecuteAsync(
new JobExecutionContext(
ServiceProvider,
typeof(MyJob),
new MyJobArgs("42"),
cts.Token
)
);
//Assert
jobObject.Canceled.ShouldBeTrue();
//Arrange
var asyncJobObject = GetRequiredService<MyAsyncJob>();
asyncJobObject.ExecutedValues.ShouldBeEmpty();
//Act
await _backgroundJobExecuter.ExecuteAsync(
new JobExecutionContext(
ServiceProvider,
typeof(MyAsyncJob),
new MyAsyncJobArgs("42")
)
);
//Assert
jobObject.Canceled.ShouldBeTrue();
}
} }

11
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyAsyncJob.cs

@ -10,11 +10,13 @@ namespace Volo.Abp.BackgroundJobs;
public class MyAsyncJob : AsyncBackgroundJob<MyAsyncJobArgs>, ISingletonDependency public class MyAsyncJob : AsyncBackgroundJob<MyAsyncJobArgs>, ISingletonDependency
{ {
public List<string> ExecutedValues { get; } = new List<string>(); public List<string> ExecutedValues { get; } = new List<string>();
public Guid? TenantId { get; set; } public Guid? TenantId { get; set; }
private readonly ICurrentTenant _currentTenant; private readonly ICurrentTenant _currentTenant;
public bool Canceled { get; set; }
public MyAsyncJob(ICurrentTenant currentTenant) public MyAsyncJob(ICurrentTenant currentTenant)
{ {
_currentTenant = currentTenant; _currentTenant = currentTenant;
@ -22,6 +24,11 @@ public class MyAsyncJob : AsyncBackgroundJob<MyAsyncJobArgs>, ISingletonDependen
public override Task ExecuteAsync(MyAsyncJobArgs args, CancellationToken cancellationToken = default) public override Task ExecuteAsync(MyAsyncJobArgs args, CancellationToken cancellationToken = default)
{ {
if (cancellationToken.IsCancellationRequested)
{
Canceled = true;
}
ExecutedValues.Add(args.Value); ExecutedValues.Add(args.Value);
TenantId = _currentTenant.Id; TenantId = _currentTenant.Id;
return Task.CompletedTask; return Task.CompletedTask;

11
framework/test/Volo.Abp.BackgroundJobs.Tests/Volo/Abp/BackgroundJobs/MyJob.cs

@ -9,11 +9,13 @@ namespace Volo.Abp.BackgroundJobs;
public class MyJob : BackgroundJob<MyJobArgs>, ISingletonDependency public class MyJob : BackgroundJob<MyJobArgs>, ISingletonDependency
{ {
public List<string> ExecutedValues { get; } = new List<string>(); public List<string> ExecutedValues { get; } = new List<string>();
public Guid? TenantId { get; set; } public Guid? TenantId { get; set; }
private readonly ICurrentTenant _currentTenant; private readonly ICurrentTenant _currentTenant;
public bool Canceled { get; set; }
public MyJob(ICurrentTenant currentTenant) public MyJob(ICurrentTenant currentTenant)
{ {
_currentTenant = currentTenant; _currentTenant = currentTenant;
@ -21,6 +23,11 @@ public class MyJob : BackgroundJob<MyJobArgs>, ISingletonDependency
public override void Execute(MyJobArgs args, CancellationToken cancellationToken = default) public override void Execute(MyJobArgs args, CancellationToken cancellationToken = default)
{ {
if (cancellationToken.IsCancellationRequested)
{
Canceled = true;
}
ExecutedValues.Add(args.Value); ExecutedValues.Add(args.Value);
TenantId = _currentTenant.Id; TenantId = _currentTenant.Id;
} }

27
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.HangFire/Program.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Hangfire; using Hangfire;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs;
@ -9,33 +10,31 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire;
class Program class Program
{ {
static void Main(string[] args) async static Task Main(string[] args)
{ {
using (var application = AbpApplicationFactory.Create<DemoAppHangfireModule>(options => using (var application = await AbpApplicationFactory.CreateAsync<DemoAppHangfireModule>(options =>
{ {
options.UseAutofac(); options.UseAutofac();
})) }))
{ {
application.Initialize(); await application.InitializeAsync();
await CancelableBackgroundJobAsync(application.ServiceProvider);
CancelableBackgroundJob(application.ServiceProvider);
Console.WriteLine("Started: " + typeof(Program).Namespace); Console.WriteLine("Started: " + typeof(Program).Namespace);
Console.WriteLine("Press ENTER to stop the application..!"); Console.WriteLine("Press ENTER to stop the application..!");
Console.ReadLine(); Console.ReadLine();
application.Shutdown(); await application.ShutdownAsync();
} }
} }
private static void CancelableBackgroundJob(IServiceProvider serviceProvider) private async static Task CancelableBackgroundJobAsync(IServiceProvider serviceProvider)
{ {
AsyncHelper.RunSync(async () => var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>();
{ var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-1" });
var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>(); await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" });
var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-1" }); Thread.Sleep(1000);
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" }); BackgroundJob.Delete(jobId);
Thread.Sleep(1000);
BackgroundJob.Delete(jobId);
});
} }
} }

31
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.Quartz/Program.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Quartz; using Quartz;
using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs; using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs;
@ -9,34 +10,32 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Quartz;
class Program class Program
{ {
static void Main(string[] args) async static Task Main(string[] args)
{ {
using (var application = AbpApplicationFactory.Create<DemoAppQuartzModule>(options => using (var application = await AbpApplicationFactory.CreateAsync<DemoAppQuartzModule>(options =>
{ {
options.UseAutofac(); options.UseAutofac();
})) }))
{ {
application.Initialize(); await application.InitializeAsync();
await CancelableBackgroundJobAsync(application.ServiceProvider);
CancelableBackgroundJob(application.ServiceProvider);
Console.WriteLine("Started: " + typeof(Program).Namespace); Console.WriteLine("Started: " + typeof(Program).Namespace);
Console.WriteLine("Press ENTER to stop the application..!"); Console.WriteLine("Press ENTER to stop the application..!");
Console.ReadLine(); Console.ReadLine();
application.Shutdown(); await application.ShutdownAsync();
} }
} }
private static void CancelableBackgroundJob(IServiceProvider serviceProvider) private async static Task CancelableBackgroundJobAsync(IServiceProvider serviceProvider)
{ {
AsyncHelper.RunSync(async () => var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>();
{ var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs {Value = "test-1"});
var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>(); await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" });
var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-1" }); Thread.Sleep(1000);
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" }); var scheduler = serviceProvider.GetRequiredService<IScheduler>();
Thread.Sleep(1000); await scheduler.Interrupt(new JobKey(jobId.Split('.')[1],jobId.Split('.')[0]));
var scheduler = serviceProvider.GetRequiredService<IScheduler>();
await scheduler.Interrupt(new JobKey(jobId.Split('.')[1],jobId.Split('.')[0]));
});
} }
} }

9
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp.RabbitMq/Program.cs

@ -1,23 +1,24 @@
using System; using System;
using System.Threading.Tasks;
namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq; namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq;
class Program class Program
{ {
static void Main(string[] args) async static Task Main(string[] args)
{ {
using (var application = AbpApplicationFactory.Create<DemoAppRabbitMqModule>(options => using (var application = await AbpApplicationFactory.CreateAsync<DemoAppRabbitMqModule>(options =>
{ {
options.UseAutofac(); options.UseAutofac();
})) }))
{ {
application.Initialize(); await application.InitializeAsync();
Console.WriteLine("Started: " + typeof(Program).Namespace); Console.WriteLine("Started: " + typeof(Program).Namespace);
Console.WriteLine("Press ENTER to stop the application..!"); Console.WriteLine("Press ENTER to stop the application..!");
Console.ReadLine(); Console.ReadLine();
application.Shutdown(); await application.ShutdownAsync();
} }
} }
} }

11
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/DemoAppModule.cs

@ -1,4 +1,5 @@
using Volo.Abp.Autofac; using System.Threading.Tasks;
using Volo.Abp.Autofac;
using Volo.Abp.BackgroundJobs.DemoApp.Shared; using Volo.Abp.BackgroundJobs.DemoApp.Shared;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore; using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore;
@ -27,19 +28,21 @@ public class DemoAppModule : AbpModule
Configure<AbpBackgroundJobWorkerOptions>(options => Configure<AbpBackgroundJobWorkerOptions>(options =>
{ {
//Configure for fast running //Configure for fast running
options.JobPollPeriod = 1000; options.JobPollPeriod = 1000;
options.DefaultFirstWaitDuration = 1; options.DefaultFirstWaitDuration = 1;
options.DefaultWaitFactor = 1; options.DefaultWaitFactor = 1;
}); });
} }
public override void OnApplicationInitialization(ApplicationInitializationContext context) public override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{ {
//TODO: Configure console logging //TODO: Configure console logging
//context //context
// .ServiceProvider // .ServiceProvider
// .GetRequiredService<ILoggerFactory>() // .GetRequiredService<ILoggerFactory>()
// .AddConsole(LogLevel.Debug); // .AddConsole(LogLevel.Debug);
return Task.CompletedTask;
} }
} }

10
modules/background-jobs/app/Volo.Abp.BackgroundJobs.DemoApp/Program.cs

@ -1,23 +1,23 @@
using System; using System;
using System.Threading.Tasks;
namespace Volo.Abp.BackgroundJobs.DemoApp; namespace Volo.Abp.BackgroundJobs.DemoApp;
class Program class Program
{ {
static void Main(string[] args) async static Task Main(string[] args)
{ {
using (var application = AbpApplicationFactory.Create<DemoAppModule>(options => using (var application = await AbpApplicationFactory.CreateAsync<DemoAppModule>(options =>
{ {
options.UseAutofac(); options.UseAutofac();
})) }))
{ {
application.Initialize(); await application.InitializeAsync();
Console.WriteLine("Started: " + typeof(Program).Namespace); Console.WriteLine("Started: " + typeof(Program).Namespace);
Console.WriteLine("Press ENTER to stop the application..!"); Console.WriteLine("Press ENTER to stop the application..!");
Console.ReadLine(); Console.ReadLine();
application.Shutdown(); await application.ShutdownAsync();
} }
} }
} }

Loading…
Cancel
Save