-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnitTest1.cs
82 lines (73 loc) · 2.56 KB
/
UnitTest1.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
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsyncEnumerableExtensions;
using Xunit;
namespace UnitTestProject1
{
public class UnitTest1
{
[Fact]
public async void NormalGeneratorTest()
{
var finished = false;
async Task Generator(IAsyncEnumerableSink<int> sink)
{
// Yield some items.
await sink.YieldAndWait(10);
await sink.YieldAndWait(20);
// Do some work.
await Task.Delay(100);
// Yield some more items.
await sink.YieldAndWait(new[] {30, 40});
// Ditto.
await Task.Delay(100);
await sink.YieldAndWait(50);
finished = true;
}
var array = await AsyncEnumerableFactory.FromAsyncGenerator<int>(Generator).ToArrayAsync();
Assert.True(finished);
Assert.Equal(new[] {10, 20, 30, 40, 50}, array);
}
[Fact]
public async void CancellationTest()
{
async Task Generator(IAsyncEnumerableSink<int> sink, CancellationToken token)
{
int value = 1;
NEXT:
value *= 2;
await sink.YieldAndWait(value);
await Task.Delay(100, token);
goto NEXT;
}
var array = await AsyncEnumerableFactory.FromAsyncGenerator<int>(Generator).Take(5).ToArrayAsync();
Assert.Equal(new[] {2, 4, 8, 16, 32}, array);
}
[Fact]
public void EmptyGeneratorTest()
{
async Task Generator(IAsyncEnumerableSink<int> sink)
{
await Task.Delay(100);
}
Assert.Empty(AsyncEnumerableFactory.FromAsyncGenerator<int>(Generator).ToEnumerable());
}
[Fact]
public async void GeneratorExceptionTest()
{
async Task Generator(IAsyncEnumerableSink<int> sink)
{
await Task.Delay(100);
await sink.YieldAndWait(10);
await sink.YieldAndWait(20);
throw new InvalidDataException();
}
Assert.Equal(new[] {10, 20}, await AsyncEnumerableFactory.FromAsyncGenerator<int>(Generator).Take(2).ToArrayAsync());
await Assert.ThrowsAsync<InvalidDataException>(async () =>
await AsyncEnumerableFactory.FromAsyncGenerator<int>(Generator).ToArrayAsync());
}
}
}