forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preserve synchronization context in ExecutionStrategy
Fixes dotnet#26763
- Loading branch information
Showing
4 changed files
with
81 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/EFCore.Specification.Tests/TestUtilities/SingleThreadSynchronizationContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Concurrent; | ||
|
||
namespace Microsoft.EntityFrameworkCore.TestUtilities; | ||
|
||
public class SingleThreadSynchronizationContext : SynchronizationContext, IDisposable | ||
{ | ||
private readonly BlockingCollection<(SendOrPostCallback callback, object state)> _tasks = new(); | ||
|
||
public Thread Thread { get; } | ||
|
||
public SingleThreadSynchronizationContext() | ||
{ | ||
Thread = new Thread(WorkLoop); | ||
Thread.Start(); | ||
} | ||
|
||
public override void Post(SendOrPostCallback callback, object state) | ||
=> _tasks.Add((callback, state)); | ||
|
||
public void Dispose() | ||
=> _tasks.CompleteAdding(); | ||
|
||
private void WorkLoop() | ||
{ | ||
SetSynchronizationContext(this); | ||
|
||
try | ||
{ | ||
while (true) | ||
{ | ||
var (callback, state) = _tasks.Take(); | ||
callback(state); | ||
} | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
_tasks.Dispose(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters