DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/Services/PeriodicBackgroundWorkerTest.cs

98 lines
3.2 KiB
C#
Raw Normal View History

2023-11-07 14:19:13 +05:00
using AsbCloudInfrastructure.Background;
using DocumentFormat.OpenXml.Drawing.Charts;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace AsbCloudWebApi.Tests.Services;
public class PeriodicBackgroundWorkerTest
{
private IServiceProvider provider;
private PeriodicBackgroundWorker service;
public PeriodicBackgroundWorkerTest()
{
provider = Substitute.For<IServiceProvider, ISupportRequiredService>();
var serviceScope = Substitute.For<IServiceScope>();
var serviceScopeFactory = Substitute.For<IServiceScopeFactory>();
serviceScopeFactory.CreateScope().Returns(serviceScope);
((ISupportRequiredService)provider).GetRequiredService(typeof(IServiceScopeFactory)).Returns(serviceScopeFactory);
service = new PeriodicBackgroundWorker(provider);
typeof(PeriodicBackgroundWorker)
.GetField("minDelay", BindingFlags.NonPublic | BindingFlags.Instance)?
.SetValue(service, TimeSpan.FromMilliseconds(1));
typeof(PeriodicBackgroundWorker)
.GetField("executePeriod", BindingFlags.NonPublic | BindingFlags.Instance)?
.SetValue(service, TimeSpan.FromMilliseconds(1));
}
[Fact]
public async Task WorkRunsTwice()
{
var workCount = 2;
var periodMs = 100d;
var period = TimeSpan.FromMilliseconds(periodMs);
var result = 0;
Task workAction(string id, IServiceProvider services, Action<string, double?> callback, CancellationToken token)
{
result++;
return Task.CompletedTask;
}
//act
var work = Work.CreateByDelegate("", workAction);
var stopwatch = Stopwatch.StartNew();
service.Add(work, period);
var delay = (periodMs / 20) + (periodMs * workCount) - stopwatch.ElapsedMilliseconds;
await Task.Delay(TimeSpan.FromMilliseconds(delay));
//assert
Assert.Equal(workCount, result);
}
[Fact]
public async Task Enqueue_continues_after_exceptions()
{
var expectadResult = 42;
var result = 0;
Task workAction(string id, IServiceProvider services, Action<string, double?> callback, CancellationToken token)
{
result = expectadResult;
return Task.CompletedTask;
}
var goodWork = Work.CreateByDelegate("", workAction);
Task failAction(string id, IServiceProvider services, Action<string, double?> callback, CancellationToken token)
=> throw new Exception();
var badWork = Work.CreateByDelegate("", failAction);
badWork.OnErrorAsync = (id, exception, token) => throw new Exception();
//act
service.Add(badWork, TimeSpan.FromSeconds(2));
service.Add(goodWork, TimeSpan.FromSeconds(2));
await Task.Delay(TimeSpan.FromMilliseconds(20));
//assert
Assert.Equal(expectadResult, result);
Assert.Equal(1, badWork.CountErrors);
Assert.Equal(1, goodWork.CountComplete);
Assert.Equal(1, goodWork.CountStart);
}
}