-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionDriverTests.cs
131 lines (107 loc) · 3.58 KB
/
ActionDriverTests.cs
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
129
130
131
using AsyncFiberWorks.Core;
using AsyncFiberWorks.Fibers;
using AsyncFiberWorks.MessageFilters;
using AsyncFiberWorks.Procedures;
using AsyncFiberWorks.Threading;
using NUnit.Framework;
using System;
using System.Threading.Tasks;
namespace AsyncFiberWorksTests
{
[TestFixture]
public class ActionDriverTests
{
[Test]
public void Invoking()
{
var driver = new ActionDriver();
long counter = 0;
Action action1 = () =>
{
Assert.AreEqual(0, counter);
counter = 300;
};
Action action2 = () =>
{
Assert.AreEqual(300, counter);
counter += 1;
};
var disposable1 = driver.Subscribe(action1);
var disposable2 = driver.Subscribe(action2);
var loop = new ThreadPoolAdaptor();
var fiber = new PoolFiber(loop);
fiber.Enqueue((e) => driver.InvokeAsync(e));
fiber.Enqueue(() => loop.Stop());
loop.Run();
Assert.AreEqual(301, counter);
}
[Test]
public async Task AsyncInvoking()
{
var driver = new ActionDriver();
long counter = 0;
Func<Task> action1 = async () =>
{
Assert.AreEqual(0, counter);
await Task.Delay(100).ConfigureAwait(false);
counter = 300;
};
Func<Task> action2 = async () =>
{
await Task.Yield();
Assert.AreEqual(300, counter);
counter += 1;
};
var disposable1 = driver.SubscribeAndReceiveAsTask(action1);
var disposable2 = driver.SubscribeAndReceiveAsTask(action2);
var fiber = new PoolFiber();
fiber.Enqueue((e) => driver.InvokeAsync(e));
await fiber.SwitchTo();
Assert.AreEqual(301, counter);
}
[Test]
public async Task AsyncInvokingWithArgument()
{
var driver = new AsyncMessageDriver<int>();
long counter = 0;
Func<int, Task> action1 = async (value) =>
{
await Task.Delay(100).ConfigureAwait(false);
counter += value;
};
Func<int, Task> action2 = async (value) =>
{
await Task.Yield();
counter += value / 10;
};
var disposable1 = driver.Subscribe(action1);
var disposable2 = driver.Subscribe(action2);
await driver.InvokeAsync(200);
Assert.AreEqual(200 + 20, counter);
await driver.InvokeAsync(10);
Assert.AreEqual(200 + 20 + 10 + 1, counter);
}
[Test]
public void ToggleAtUnsubscribe()
{
var driver = new ActionDriver();
long counter = 0;
var unsubscriber = new Unsubscriber();
Action action = () =>
{
counter += 1;
unsubscriber.Dispose();
};
var disposable1 = driver.Subscribe(action);
unsubscriber.AppendDisposable(disposable1);
var disposable2 = driver.Subscribe(action);
unsubscriber.AppendDisposable(disposable2);
var loop = new ThreadPoolAdaptor();
var fiber = new PoolFiber(loop);
fiber.Enqueue((e) => driver.InvokeAsync(e));
fiber.Enqueue(() => loop.Stop());
loop.Run();
Assert.AreEqual(1, counter);
}
}
}