Skip to content

Commit 08d7f16

Browse files
author
Oleksandr Poliakov
committed
Make ThreadingUtilities.ExecuteTasksOnNewThreads await on the operation completion.
1 parent 13d89ef commit 08d7f16

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

tests/MongoDB.Bson.TestHelpers/Threading/ThreadingUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static async Task<Exception[]> ExecuteTasksOnNewThreadsCollectExceptions(
110110
}
111111
});
112112

113-
var taskAll = Task.WhenAll(allTasks);
113+
var taskAll = Task.WhenAll(allTasks.Select(t => t.Result));
114114
if (await Task.WhenAny(taskAll, Task.Delay(timeoutMilliseconds)) != taskAll)
115115
{
116116
exceptions.Add(new TimeoutException());
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using MongoDB.Bson.TestHelpers;
4+
using MongoDB.TestHelpers.XunitExtensions;
5+
using Xunit;
6+
using Xunit.Abstractions;
7+
8+
namespace MongoDB.Driver.Tests
9+
{
10+
// TODO: DO NOT MERGE THIS CLASS!
11+
// It exists for problem repro purposes only
12+
public class TestClassDoNotMerge
13+
{
14+
private readonly ITestOutputHelper _output;
15+
16+
public TestClassDoNotMerge(ITestOutputHelper output)
17+
{
18+
_output = output;
19+
}
20+
21+
[Theory]
22+
[ParameterAttributeData]
23+
public async Task RunTasksInParallelTest([Values(false, true)] bool async)
24+
{
25+
_output.WriteLine($"Test async={async}: before parallel");
26+
27+
await ThreadingUtilities.ExecuteTasksOnNewThreads(10, async i =>
28+
{
29+
if (async)
30+
{
31+
await Task.Delay(100);
32+
}
33+
else
34+
{
35+
Thread.Sleep(100);
36+
}
37+
_output.WriteLine($"I'm task number {i}");
38+
});
39+
40+
_output.WriteLine($"Test async={async}: after parallel");
41+
42+
await Task.Delay(1_000);
43+
_output.WriteLine($"Test async={async}: done");
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)