Skip to content

Commit

Permalink
Fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
timcassell committed Nov 6, 2023
1 parent 966b309 commit e923270
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions Package/Tests/CoreTests/APIs/ParallelForTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,32 +211,35 @@ public void AllItemsEnumeratedOnce_WithCaptureValue_Sync(
}
}

private IEnumerable<int> IterateAndAssertContext(SynchronizationContext context)
private IEnumerable<int> IterateAndAssertContext(SynchronizationType expectedContext, Thread mainThread)
{
Assert.AreEqual(context, SynchronizationContext.Current);
TestHelper.AssertCallbackContext(expectedContext, expectedContext, mainThread);
for (int i = 1; i <= 100; i++)
{
yield return i;
Assert.AreEqual(context, SynchronizationContext.Current);
TestHelper.AssertCallbackContext(expectedContext, expectedContext, mainThread);
}
}

[Test]
public void SynchronizationContext_AllCodeExecutedOnCorrectContext_Sync(
[Values] bool foregroundContext)
[Values(SynchronizationType.Foreground, SynchronizationType.Background)] SynchronizationType syncContext)
{
SynchronizationContext context = foregroundContext ?
TestHelper._foregroundContext :
(SynchronizationContext) TestHelper._backgroundContext;
var mainThread = Thread.CurrentThread;
SynchronizationContext context = syncContext == SynchronizationType.Foreground
? TestHelper._foregroundContext
: (SynchronizationContext) TestHelper._backgroundContext;

var otherContext = new PromiseSynchronizationContext();
var otherContext = syncContext == SynchronizationType.Foreground
? (SynchronizationContext) TestHelper._backgroundContext
: TestHelper._foregroundContext;

var cq = new Queue<int>();
bool isComplete = false;

Promise.ParallelForEach(IterateAndAssertContext(context), (item, cancelationToken) =>
Promise.ParallelForEach(IterateAndAssertContext(syncContext, mainThread), (item, cancelationToken) =>
{
Assert.AreEqual(context, SynchronizationContext.Current);
TestHelper.AssertCallbackContext(syncContext, syncContext, mainThread);
return Promise.SwitchToContext(context)
.Then(() =>
{
Expand All @@ -257,7 +260,6 @@ public void SynchronizationContext_AllCodeExecutedOnCorrectContext_Sync(
if (!SpinWait.SpinUntil(() =>
{
TestHelper.ExecuteForegroundCallbacks();
otherContext.Execute();
return isComplete;
}, TimeSpan.FromSeconds(1)))
{
Expand Down

0 comments on commit e923270

Please sign in to comment.