Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Prev Previous commit
Next Next commit
feedback
  • Loading branch information
pavelsavara committed Jan 2, 2024
commit 615ce7a41a71ea3775b1d27c42fa57ceb1c2dc14
Original file line number Diff line number Diff line change
Expand Up @@ -139,37 +139,12 @@ public async Task JSDelay_ContinueWith(Executor executor)
{
await executor.Execute(async () =>
{
var currentTID = Environment.CurrentManagedThreadId;
await executor.StickyAwait(WebWorkerTestHelper.CreateDelay());

await WebWorkerTestHelper.Delay(1).ContinueWith(_ =>
{
// continue on the context of the target JS interop
switch (executor.Type)
{
case ExecutorType.Main:
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.Equal(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.JSWebWorker:
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.Equal(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.NewThread:
// it will synchronously continue on the UI thread
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.NotEqual(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.ThreadPool:
// it will synchronously continue on the UI thread
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.NotEqual(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
}
executor.AssertInteropThread();
}, TaskContinuationOptions.ExecuteSynchronously);
});
}
Expand All @@ -179,35 +154,11 @@ public async Task JSDelay_ConfigureAwait_True(Executor executor)
{
await executor.Execute(async () =>
{
var currentTID = Environment.CurrentManagedThreadId;
await executor.StickyAwait(WebWorkerTestHelper.CreateDelay());

await WebWorkerTestHelper.Delay(1).ConfigureAwait(true);
// continue on captured context
switch (executor.Type)
{
case ExecutorType.Main:
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.Equal(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.JSWebWorker:
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.Equal(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.NewThread:
// the actual new thread is now blocked in .Wait() and so this is running on TP
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.NotEqual(currentTID, Environment.CurrentManagedThreadId);
Assert.True(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.ThreadPool:
// it could migrate to any TP thread
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.True(Thread.CurrentThread.IsThreadPoolThread);
break;
}

executor.AssertAwaitCapturedContext();
});
}

Expand All @@ -231,35 +182,11 @@ public async Task ManagedDelay_ConfigureAwait_True(Executor executor)
{
await executor.Execute(async () =>
{
var currentTID = Environment.CurrentManagedThreadId;
executor.AssertTargetThread();

await Task.Delay(1).ConfigureAwait(true);
// continue on captured context
switch (executor.Type)
{
case ExecutorType.Main:
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.Equal(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.JSWebWorker:
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.Equal(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.NewThread:
// the actual new thread is now blocked in .Wait() and so this is running on TP
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.NotEqual(currentTID, Environment.CurrentManagedThreadId);
Assert.True(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.ThreadPool:
// it could migrate to any TP thread
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.True(Thread.CurrentThread.IsThreadPoolThread);
break;
}

executor.AssertAwaitCapturedContext();
});
}

Expand All @@ -268,30 +195,11 @@ public async Task ManagedYield(Executor executor)
{
await executor.Execute(async () =>
{
var currentTID = Environment.CurrentManagedThreadId;
executor.AssertTargetThread();

await Task.Yield();

switch (executor.Type)
{
case ExecutorType.Main:
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.JSWebWorker:
Assert.Equal(currentTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.NewThread:
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.True(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.ThreadPool:
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.True(Thread.CurrentThread.IsThreadPoolThread);
break;
}
executor.AssertAwaitCapturedContext();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public enum ExecutorType
}
public class Executor
{
public int ExecutorTID;

public ExecutorType Type;

public Executor(ExecutorType type)
Expand All @@ -182,16 +184,22 @@ public Executor(ExecutorType type)

public Task Execute(Func<Task> job)
{
Task wrapExecute()
{
ExecutorTID = Environment.CurrentManagedThreadId;
return job();
}

switch (Type)
{
case ExecutorType.Main:
return WebWorkerTestHelper.RunOnMainAsync(job);
return WebWorkerTestHelper.RunOnMainAsync(wrapExecute);
case ExecutorType.ThreadPool:
return Task.Run(job);
return Task.Run(wrapExecute);
case ExecutorType.NewThread:
return WebWorkerTestHelper.RunOnNewThread(job);
return WebWorkerTestHelper.RunOnNewThread(wrapExecute);
case ExecutorType.JSWebWorker:
return JSWebWorker.RunAsync(job);
return JSWebWorker.RunAsync(wrapExecute);
default:
throw new InvalidOperationException();
}
Expand All @@ -217,6 +225,63 @@ public void AssertTargetThread()
}
}

public void AssertAwaitCapturedContext()
{
switch (Type)
{
case ExecutorType.Main:
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.Equal(ExecutorTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.JSWebWorker:
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.Equal(ExecutorTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.NewThread:
// the actual new thread is now blocked in .Wait() and so this is running on TP
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.NotEqual(ExecutorTID, Environment.CurrentManagedThreadId);
Assert.True(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.ThreadPool:
// it could migrate to any TP thread
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.True(Thread.CurrentThread.IsThreadPoolThread);
break;
}
}

public void AssertInteropThread()
{
switch (Type)
{
case ExecutorType.Main:
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.Equal(ExecutorTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.JSWebWorker:
Assert.NotEqual(1, Environment.CurrentManagedThreadId);
Assert.Equal(ExecutorTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.NewThread:
// it will synchronously continue on the UI thread
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.NotEqual(ExecutorTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
case ExecutorType.ThreadPool:
// it will synchronously continue on the UI thread
Assert.Equal(1, Environment.CurrentManagedThreadId);
Assert.NotEqual(ExecutorTID, Environment.CurrentManagedThreadId);
Assert.False(Thread.CurrentThread.IsThreadPoolThread);
break;
}
}

public override string ToString() => Type.ToString();

// make sure we stay on the executor
Expand Down