Add unit test and update DemoApp.

pull/15282/head
maliming 3 years ago
parent a96a1f7ad1
commit e84367ceb3
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

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

@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Shouldly;
using Xunit;
@ -59,7 +60,7 @@ public class BackgroundJobExecuter_Tests : BackgroundJobsTestBase
jobObject.ExecutedValues.ShouldContain("42");
}
[Fact]
public async Task Should_Change_TenantId_If_EventData_Is_MultiTenant()
{
@ -77,7 +78,7 @@ public class BackgroundJobExecuter_Tests : BackgroundJobsTestBase
new MyJobArgs("42", tenantId)
)
);
await _backgroundJobExecuter.ExecuteAsync(
new JobExecutionContext(
ServiceProvider,
@ -91,4 +92,44 @@ public class BackgroundJobExecuter_Tests : BackgroundJobsTestBase
jobObject.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();
}
}

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

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

@ -1,5 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Hangfire;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs;
@ -9,33 +10,31 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.HangFire;
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();
}))
{
application.Initialize();
await application.InitializeAsync();
await CancelableBackgroundJobAsync(application.ServiceProvider);
CancelableBackgroundJob(application.ServiceProvider);
Console.WriteLine("Started: " + typeof(Program).Namespace);
Console.WriteLine("Press ENTER to stop the application..!");
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" });
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" });
Thread.Sleep(1000);
BackgroundJob.Delete(jobId);
});
var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>();
var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-1" });
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" });
Thread.Sleep(1000);
BackgroundJob.Delete(jobId);
}
}

@ -1,5 +1,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Quartz;
using Volo.Abp.BackgroundJobs.DemoApp.Shared.Jobs;
@ -9,34 +10,32 @@ namespace Volo.Abp.BackgroundJobs.DemoApp.Quartz;
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();
}))
{
application.Initialize();
await application.InitializeAsync();
await CancelableBackgroundJobAsync(application.ServiceProvider);
CancelableBackgroundJob(application.ServiceProvider);
Console.WriteLine("Started: " + typeof(Program).Namespace);
Console.WriteLine("Press ENTER to stop the application..!");
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" });
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" });
Thread.Sleep(1000);
var scheduler = serviceProvider.GetRequiredService<IScheduler>();
await scheduler.Interrupt(new JobKey(jobId.Split('.')[1],jobId.Split('.')[0]));
});
var backgroundJobManager = serviceProvider.GetRequiredService<IBackgroundJobManager>();
var jobId = await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs {Value = "test-1"});
await backgroundJobManager.EnqueueAsync(new LongRunningJobArgs { Value = "test-2" });
Thread.Sleep(1000);
var scheduler = serviceProvider.GetRequiredService<IScheduler>();
await scheduler.Interrupt(new JobKey(jobId.Split('.')[1],jobId.Split('.')[0]));
}
}

@ -1,23 +1,24 @@
using System;
using System.Threading.Tasks;
namespace Volo.Abp.BackgroundJobs.DemoApp.RabbitMq;
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();
}))
{
application.Initialize();
await application.InitializeAsync();
Console.WriteLine("Started: " + typeof(Program).Namespace);
Console.WriteLine("Press ENTER to stop the application..!");
Console.ReadLine();
application.Shutdown();
await application.ShutdownAsync();
}
}
}

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

@ -1,23 +1,23 @@
using System;
using System.Threading.Tasks;
namespace Volo.Abp.BackgroundJobs.DemoApp;
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();
}))
{
application.Initialize();
await application.InitializeAsync();
Console.WriteLine("Started: " + typeof(Program).Namespace);
Console.WriteLine("Press ENTER to stop the application..!");
Console.ReadLine();
application.Shutdown();
await application.ShutdownAsync();
}
}
}

Loading…
Cancel
Save