Skip to content

Commit 3669217

Browse files
committed
More Test Task runner features & tests
1 parent 5177250 commit 3669217

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

async-enumerable-dotnet-test/AmbTest.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See LICENSE file in the project root for full license information.
44

55
using System;
6+
using System.Runtime.CompilerServices;
67
using Xunit;
78
using async_enumerable_dotnet;
89
using System.Threading;
@@ -83,5 +84,81 @@ await AsyncEnumerable.Amb(
8384

8485
Assert.Equal(129, disposed);
8586
}
87+
88+
[Fact]
89+
public async void First_Win_Sync()
90+
{
91+
var ttr = new TestTaskRunner();
92+
93+
var t = AsyncEnumerable.Amb(
94+
AsyncEnumerable.Range(1, 5)
95+
.DoOnNext(async v => { await ttr.CreateCompleteTask(500); }),
96+
AsyncEnumerable.Range(6, 5)
97+
.DoOnNext(async v => { await ttr.CreateCompleteTask(1000); })
98+
)
99+
.GetAsyncEnumerator()
100+
;
101+
102+
try
103+
{
104+
for (var i = 1; i <= 5; i++)
105+
{
106+
var t0 = t.MoveNextAsync();
107+
108+
await ttr.TaskQueued();
109+
110+
ttr.AdvanceTimeBy(500);
111+
112+
await t0;
113+
114+
Assert.Equal(i, t.Current);
115+
}
116+
117+
Assert.False(await t.MoveNextAsync());
118+
}
119+
finally
120+
{
121+
await t.DisposeAsync();
122+
}
123+
124+
}
125+
126+
[Fact]
127+
public async void Second_Win_Sync()
128+
{
129+
var ttr = new TestTaskRunner();
130+
131+
var t = AsyncEnumerable.Amb(
132+
AsyncEnumerable.Range(1, 5)
133+
.DoOnNext(async v => { await ttr.CreateCompleteTask(1000); }),
134+
AsyncEnumerable.Range(6, 5)
135+
.DoOnNext(async v => { await ttr.CreateCompleteTask(500); })
136+
)
137+
.GetAsyncEnumerator()
138+
;
139+
140+
try
141+
{
142+
for (var i = 6; i <= 10; i++)
143+
{
144+
var t0 = t.MoveNextAsync();
145+
146+
await ttr.TaskQueued();
147+
148+
ttr.AdvanceTimeBy(500);
149+
150+
await t0;
151+
152+
Assert.Equal(i, t.Current);
153+
}
154+
155+
Assert.False(await t.MoveNextAsync());
156+
}
157+
finally
158+
{
159+
await t.DisposeAsync();
160+
}
161+
162+
}
86163
}
87164
}

async-enumerable-dotnet/TestTaskRunner.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using async_enumerable_dotnet.impl;
910

1011
namespace async_enumerable_dotnet
1112
{
@@ -49,6 +50,11 @@ public bool HasTasks
4950
}
5051
}
5152

53+
/// <summary>
54+
/// Signaled when a task queue becomes non-empty.
55+
/// </summary>
56+
private TaskCompletionSource<bool> _taskQueued;
57+
5258
/// <summary>
5359
/// Construct a fresh TestTaskRunner with the given
5460
/// (optional) start virtual time.
@@ -60,6 +66,16 @@ public TestTaskRunner(long startTime = 0L)
6066
_queue = new SortedList<IndexDueTime, TaskTask>(IndexDueTimeComparer.Default);
6167
}
6268

69+
/// <summary>
70+
/// Signals when the first task has been queued.
71+
/// </summary>
72+
/// <returns>The task to await for the queue to become non-empty.</returns>
73+
public async Task TaskQueued()
74+
{
75+
await ResumeHelper.Await(ref _taskQueued);
76+
ResumeHelper.Clear(ref _taskQueued);
77+
}
78+
6379
/// <summary>
6480
/// Advance the virtual time by the given amount and
6581
/// signal all timed tasks in the meantime.
@@ -109,6 +125,8 @@ private void Enqueue(TaskTask task)
109125
DueTime = task.DueTime
110126
}, task);
111127
}
128+
129+
ResumeHelper.Resume(ref _taskQueued);
112130
}
113131

114132
/// <summary>

0 commit comments

Comments
 (0)