-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultTaskFlowServiceCollectionExtensionsFixture.cs
More file actions
128 lines (103 loc) · 5.27 KB
/
DefaultTaskFlowServiceCollectionExtensionsFixture.cs
File metadata and controls
128 lines (103 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
namespace TaskFlow.Extensions.Microsoft.DependencyInjection.Tests
{
using global::Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System.Threading.Tasks.Flow;
[TestFixture]
internal sealed class DefaultTaskFlowServiceCollectionExtensionsFixture
{
[Test]
public void AddTaskFlow_ShouldRegisterTaskFlowInfoAndTaskScheduler()
{
using var container = new ServiceCollection()
.AddTaskFlow()
.BuildServiceProvider();
var taskFlowInfo = container.GetRequiredService<ITaskFlowInfo>();
var taskScheduler = container.GetRequiredService<ITaskScheduler>();
Assert.That(taskFlowInfo, Is.SameAs(taskScheduler));
}
[Test]
public void AddTaskFlow_ShouldNotRegisterTaskFlow()
{
using var container = new ServiceCollection()
.AddTaskFlow()
.BuildServiceProvider();
var taskFlow = container.GetService<ITaskFlow>();
Assert.That(taskFlow, Is.Null);
}
[Test]
public void AddTaskFlow_ScopesShouldHaveOwnTaskFlow()
{
using var container = new ServiceCollection()
.AddTaskFlow()
.BuildServiceProvider();
var scopeFactory = container.GetRequiredService<IServiceScopeFactory>();
using var scope1 = scopeFactory.CreateScope();
using var scope2 = scopeFactory.CreateScope();
var taskScheduler1 = scope1.ServiceProvider.GetRequiredService<ITaskScheduler>();
var taskScheduler2 = scope2.ServiceProvider.GetRequiredService<ITaskScheduler>();
Assert.That(taskScheduler1, Is.Not.SameAs(taskScheduler2));
}
[Test]
public void AddTaskFlow_Scopes_WhenDisposeOneScopeCanEnqueueToAnotherScope()
{
using var container = new ServiceCollection()
.AddTaskFlow()
.BuildServiceProvider();
var scopeFactory = container.GetRequiredService<IServiceScopeFactory>();
using var scope1 = scopeFactory.CreateScope();
using var scope2 = scopeFactory.CreateScope();
var taskScheduler1 = scope1.ServiceProvider.GetRequiredService<ITaskScheduler>();
var taskScheduler2 = scope2.ServiceProvider.GetRequiredService<ITaskScheduler>();
scope1.Dispose();
Assert.That(() => taskScheduler1.Enqueue(() => { }), Throws.TypeOf<ObjectDisposedException>());
Assert.That(() => taskScheduler2.Enqueue(() => { }), Throws.Nothing);
}
[Test]
[Timeout(1000)]
public void AddTaskFlow_DisposeScope_ShouldCancelPendingTask()
{
using var container = new ServiceCollection()
.AddTaskFlow()
.BuildServiceProvider();
var scope = container.GetRequiredService<IServiceScopeFactory>().CreateScope();
var taskScheduler = scope.ServiceProvider.GetRequiredService<ITaskScheduler>();
var task1 = taskScheduler.Enqueue(token => Task.Delay(1000, token));
var task2 = taskScheduler.Enqueue(token => Task.Delay(1000, token));
var task3 = taskScheduler.Enqueue(token => Task.Delay(1000, token));
Assert.That(task1.IsCompleted && task2.IsCompleted && task3.IsCompleted, Is.False);
scope.Dispose();
Assert.That(() => task1.IsCanceled, Is.True.After(100, 10), task1.Status.ToString);
Assert.That(() => task2.IsCanceled, Is.True.After(100, 10), task2.Status.ToString);
Assert.That(() => task3.IsCanceled, Is.True.After(100, 10), task3.Status.ToString);
}
[Test]
[Timeout(1000)]
public async Task AddTaskFlow_DisposeScopeAsync_ShouldCancelPendingTask()
{
using var container = new ServiceCollection()
.AddTaskFlow()
.BuildServiceProvider();
var scope = container.GetRequiredService<IServiceScopeFactory>().CreateAsyncScope();
var taskScheduler = scope.ServiceProvider.GetRequiredService<ITaskScheduler>();
var task1 = taskScheduler.Enqueue(token => Task.Delay(1000, token));
var task2 = taskScheduler.Enqueue(token => Task.Delay(1000, token));
var task3 = taskScheduler.Enqueue(token => Task.Delay(1000, token));
Assert.That(task1.IsCompleted && task2.IsCompleted && task3.IsCompleted, Is.False);
await scope.DisposeAsync();
Assert.That(() => task1.IsCanceled, Is.True.After(100, 10), task1.Status.ToString);
Assert.That(() => task2.IsCanceled, Is.True.After(100, 10), task2.Status.ToString);
Assert.That(() => task3.IsCanceled, Is.True.After(100, 10), task3.Status.ToString);
}
[Test]
public void AddTaskFlow_WithOptions_ShouldRegisterTaskFlowWithOptions()
{
var options = new TaskFlowOptions { SynchronousDisposeTimeout = TimeSpan.FromDays(1) };
using var container = new ServiceCollection()
.AddTaskFlow(options)
.BuildServiceProvider();
var taskFlowInfo = container.GetRequiredService<ITaskFlowInfo>();
Assert.That(taskFlowInfo.Options, Is.EqualTo(options));
}
}
}