Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:OmerMor/AsyncBridge
- Loading branch information
Showing
8 changed files
with
175 additions
and
7 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
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,42 @@ | |||
using System.Threading.Tasks; | |||
|
|||
namespace AsyncBridge | |||
{ | |||
/// <summary> | |||
/// An awaitable which wraps a class, maybe preventing it from capturing the SynchronizationContext | |||
/// </summary> | |||
public class ConfigurableTaskAwaitable<T> | |||
{ | |||
private readonly Task<T> m_task; | |||
private readonly bool m_useCapturedContext; | |||
|
|||
public ConfigurableTaskAwaitable(Task<T> task, bool useCapturedContext) | |||
{ | |||
m_task = task; | |||
m_useCapturedContext = useCapturedContext; | |||
} | |||
|
|||
public TaskAwaiter<T> GetAwaiter() | |||
{ | |||
return new TaskAwaiter<T>(m_task, m_useCapturedContext); | |||
} | |||
} | |||
|
|||
// ZOMG why isn't void an actual type | |||
public class ConfigurableTaskAwaitable | |||
{ | |||
private readonly Task m_task; | |||
private readonly bool m_useCapturedContext; | |||
|
|||
public ConfigurableTaskAwaitable(Task task, bool useCapturedContext) | |||
{ | |||
m_task = task; | |||
m_useCapturedContext = useCapturedContext; | |||
} | |||
|
|||
public TaskAwaiter GetAwaiter() | |||
{ | |||
return new TaskAwaiter(m_task, m_useCapturedContext); | |||
} | |||
} | |||
} |
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
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
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,100 @@ | |||
using System.Threading; | |||
using System.Threading.Tasks; | |||
using Microsoft.VisualStudio.TestTools.UnitTesting; | |||
|
|||
namespace AsyncBridge.Tests | |||
{ | |||
[TestClass] | |||
public class SyncContextTests | |||
{ | |||
class MagicSynchronizationContext : SynchronizationContext | |||
{ | |||
public static readonly MagicSynchronizationContext Instance = new MagicSynchronizationContext(); | |||
|
|||
public override void Post(SendOrPostCallback d, object state) | |||
{ | |||
base.Post(o => | |||
{ | |||
SetSynchronizationContext(this); | |||
d(o); | |||
}, state); | |||
} | |||
} | |||
|
|||
[TestMethod] | |||
public async Task YieldSyncContext() | |||
{ | |||
SynchronizationContext.SetSynchronizationContext(MagicSynchronizationContext.Instance); | |||
await TaskUtils.Yield(); | |||
Assert.IsTrue(SynchronizationContext.Current is MagicSynchronizationContext); | |||
} | |||
|
|||
[TestMethod] | |||
public async Task FromResultSyncContext() | |||
{ | |||
SynchronizationContext.SetSynchronizationContext(MagicSynchronizationContext.Instance); | |||
int r = await TaskUtils.FromResult(4); | |||
Assert.IsTrue(SynchronizationContext.Current is MagicSynchronizationContext); | |||
Assert.AreEqual(4, r); | |||
} | |||
|
|||
[TestMethod] | |||
public async Task DelaySyncContext() | |||
{ | |||
SynchronizationContext.SetSynchronizationContext(MagicSynchronizationContext.Instance); | |||
await TaskUtils.Delay(1); | |||
Assert.IsTrue(SynchronizationContext.Current is MagicSynchronizationContext); | |||
} | |||
|
|||
[TestMethod] | |||
public async Task SimpleTaskSyncContext() | |||
{ | |||
SynchronizationContext.SetSynchronizationContext(MagicSynchronizationContext.Instance); | |||
await WaitABit(); | |||
Assert.IsTrue(SynchronizationContext.Current is MagicSynchronizationContext); | |||
} | |||
|
|||
[TestMethod] | |||
public async Task ReturningTaskSyncContext() | |||
{ | |||
SynchronizationContext.SetSynchronizationContext(MagicSynchronizationContext.Instance); | |||
int r = await WaitAThing(); | |||
Assert.IsTrue(SynchronizationContext.Current is MagicSynchronizationContext); | |||
Assert.AreEqual(6, r); | |||
} | |||
|
|||
[TestMethod] | |||
public async Task ConfiguredSimpleTaskSyncContext() | |||
{ | |||
SynchronizationContext.SetSynchronizationContext(MagicSynchronizationContext.Instance); | |||
await WaitABit().ConfigureAwait(false); | |||
Assert.IsFalse(SynchronizationContext.Current is MagicSynchronizationContext); | |||
} | |||
|
|||
[TestMethod] | |||
public async Task ConfiguredReturningTaskSyncContext() | |||
{ | |||
SynchronizationContext.SetSynchronizationContext(MagicSynchronizationContext.Instance); | |||
int r = await WaitAThing().ConfigureAwait(false); | |||
Assert.IsFalse(SynchronizationContext.Current is MagicSynchronizationContext); | |||
Assert.AreEqual(6, r); | |||
} | |||
|
|||
/// <summary> | |||
/// Exercise our AsyncTaskMethodBuilder | |||
/// </summary> | |||
private async Task WaitABit() | |||
{ | |||
await TaskUtils.Delay(1); | |||
} | |||
|
|||
/// <summary> | |||
/// Exercise our AsyncTaskMethodBuilder'1 | |||
/// </summary> | |||
private async Task<int> WaitAThing() | |||
{ | |||
await TaskUtils.Delay(1); | |||
return await TaskUtils.FromResult(6); | |||
} | |||
} | |||
} |