Skip to content

Make ThreadingUtilities.ExecuteTasksOnNewThreads await on the operations completion. #1275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions tests/MongoDB.Bson.TestHelpers/Threading/TasksUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public static Task<T>[] CreateTasksOnOwnThread<T>(int count, Func<int, T> functi
new ThreadPerTaskScheduler()))
.ToArray();

public static Task[] RunTasksOnOwnThread(int count, Func<int, Task> taskCreator, CancellationToken cancellationToken = default) =>
Enumerable.Range(0, count)
.Select(i => Task.Factory.StartNew(
() => taskCreator(i),
cancellationToken,
TaskCreationOptions.None,
new ThreadPerTaskScheduler()).Unwrap())
.ToArray();

public static Task<T>[] CreateTasks<T>(int count, Func<int, Task<T>> taskCreator) =>
Enumerable.Range(0, count)
.Select(i => taskCreator(i))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static async Task<Exception[]> ExecuteTasksOnNewThreadsCollectExceptions(
var exceptions = new ConcurrentBag<Exception>();
var tasksExecutingCountEvent = new CountdownEvent(threadsCount);

var allTasks = TasksUtils.CreateTasksOnOwnThread(threadsCount, async i =>
var allTasks = TasksUtils.RunTasksOnOwnThread(threadsCount, async i =>
{
try
{
Expand Down