Skip to content

Commit 9c9a5c4

Browse files
authored
Fix incorrect SynchronizationContext use in TestSweeper (#4779)
1 parent daae78d commit 9c9a5c4

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

test/Microsoft.ML.Sweeper.Tests/TestSweeper.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void TestSimpleSweeperAsync()
175175
}
176176

177177
[Fact]
178-
public void TestDeterministicSweeperAsyncCancellation()
178+
public async Task TestDeterministicSweeperAsyncCancellation()
179179
{
180180
var random = new Random(42);
181181
var env = new MLContext(42);
@@ -215,7 +215,7 @@ public void TestDeterministicSweeperAsyncCancellation()
215215
// Cancel after the first barrier and check if the number of registered actions
216216
// is indeed 2 * batchSize.
217217
sweeper.Cancel();
218-
Task.WaitAll(tasks.ToArray());
218+
await Task.WhenAll(tasks);
219219
foreach (var task in tasks)
220220
{
221221
if (task.Result != null)
@@ -225,7 +225,7 @@ public void TestDeterministicSweeperAsyncCancellation()
225225
}
226226

227227
[Fact]
228-
public void TestDeterministicSweeperAsync()
228+
public async Task TestDeterministicSweeperAsync()
229229
{
230230
var random = new Random(42);
231231
var env = new MLContext(42);
@@ -289,8 +289,7 @@ public void TestDeterministicSweeperAsync()
289289
foreach (var run in results)
290290
sweeper.Update(run.Key, run.Value);
291291

292-
Task.WaitAll(tasks);
293-
tasks.All(t => t.IsCompleted);
292+
await Task.WhenAll(tasks);
294293
}
295294

296295
[Fact]
@@ -327,7 +326,7 @@ public void TestDeterministicSweeperAsyncParallel()
327326
int[] sleeps = new int[sweeps];
328327
for (int i = 0; i < sleeps.Length; i++)
329328
sleeps[i] = random.Next(10, 100);
330-
var r = Parallel.For(0, sweeps, options, (int i) =>
329+
var r = Task.Run(() => Parallel.For(0, sweeps, options, (int i) =>
331330
{
332331
var task = sweeper.Propose();
333332
task.Wait();
@@ -340,7 +339,7 @@ public void TestDeterministicSweeperAsyncParallel()
340339
sweeper.Update(paramWithId.Id, result);
341340
lock (mlock)
342341
paramSets.Add(paramWithId.ParameterSet);
343-
});
342+
}));
344343
Assert.True(paramSets.Count <= sweeps);
345344
CheckAsyncSweeperResult(paramSets);
346345
}

0 commit comments

Comments
 (0)