61 lines
1.9 KiB
C#
61 lines
1.9 KiB
C#
|
using Falcon.SugarApi.BackTask;
|
|||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|||
|
using System.Threading;
|
|||
|
using System;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using Microsoft.Extensions.Hosting;
|
|||
|
|
|||
|
namespace Falcon.SugarApi.Test
|
|||
|
{
|
|||
|
[TestClass]
|
|||
|
public class BackTaskTest
|
|||
|
{
|
|||
|
[TestMethod]
|
|||
|
public void BackgroundLongTaskTestMethod() {
|
|||
|
var task = new BackTaskObject();
|
|||
|
Assert.IsTrue(task.State == 0, "<22><>ʼ<EFBFBD><CABC>״̬<D7B4><CCAC><EFBFBD><EFBFBD>");
|
|||
|
var token = new CancellationTokenSource();
|
|||
|
task.StartAsync(token.Token);
|
|||
|
Thread.Sleep(1 * 1000);
|
|||
|
Assert.IsTrue(task.State == 1, "<22><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD>");
|
|||
|
Thread.Sleep(5 * 1000);
|
|||
|
task.StopAsync(token.Token).Wait();
|
|||
|
Thread.Sleep(2 * 1000);
|
|||
|
Assert.IsTrue(task.State == 2, "ֹͣ״̬<D7B4><CCAC><EFBFBD><EFBFBD>");
|
|||
|
Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss")}:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public class BackTaskObject : BackgroundLongTask
|
|||
|
{
|
|||
|
public override float RunTimespan => 1;
|
|||
|
public ILogger Log { get; set; }
|
|||
|
public int State { get; set; } = 0;
|
|||
|
|
|||
|
public BackTaskObject() {
|
|||
|
this.Log = new TestLog();
|
|||
|
}
|
|||
|
|
|||
|
protected override void OnStop(BackgroundLongTask t) {
|
|||
|
State = 2;
|
|||
|
this.Log.LogInformation($"{DateTime.Now.ToString("HH:mm:ss")}:Test OnStop!");
|
|||
|
base.OnStop(t);
|
|||
|
}
|
|||
|
|
|||
|
protected override void OnStart(BackgroundLongTask t) {
|
|||
|
State = 1;
|
|||
|
this.Log.LogInformation($"{DateTime.Now.ToString("HH:mm:ss")}:Test OnStart!");
|
|||
|
base.OnStart(t);
|
|||
|
}
|
|||
|
|
|||
|
protected override void OnCompleted(BackgroundLongTask t) {
|
|||
|
this.Log.LogInformation($"{DateTime.Now.ToString("HH:mm:ss")}:Test OnCompleted!");
|
|||
|
base.OnCompleted(t);
|
|||
|
}
|
|||
|
|
|||
|
protected override void Run() {
|
|||
|
this.Log.LogInformation($"{DateTime.Now.ToString("HH:mm:ss")}:Test Run!");
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|