public async Task InlinedSendReappliesSyncContextWhenSticky()
    {
        var sticky = new NonConcurrentSynchronizationContext(sticky: true);
        var tcs    = new TaskCompletionSource <object?>();

        sticky.Post(
            _ =>
        {
            try
            {
                SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
                sticky.Send(
                    _ =>
                {
                    Assert.Same(sticky, SynchronizationContext.Current);
                },
                    null);
                Assert.IsType <SynchronizationContext>(SynchronizationContext.Current);
                tcs.TrySetResult(null);
            }
            catch (Exception ex)
            {
                tcs.TrySetException(ex);
            }
        },
            null);
        await tcs.Task.WithCancellation(this.TimeoutToken);
    }
Esempio n. 2
0
    public async Task SyncContextIsNotInherited()
    {
        // Set a SyncContext when creating the context, and ensure the current context is never used.
        SynchronizationContext.SetSynchronizationContext(new ThrowingSyncContext());
        var nonConcurrentContext = new NonConcurrentSynchronizationContext(sticky: false);

        // Also confirm the current context doesn't impact the Post method.
        Assert.Null(await GetCurrentSyncContextDuringPost(nonConcurrentContext).ConfigureAwait(false));
    }
Esempio n. 3
0
    public async Task Sticky_SetsCurrentSyncContext()
    {
        var sticky = new NonConcurrentSynchronizationContext(sticky: true);

        Assert.Same(sticky, await GetCurrentSyncContextDuringPost(sticky));
    }