Skip to content

Commit

Permalink
Merge pull request #21 from nventive/dev/dr/RevertFastAsyncLock
Browse files Browse the repository at this point in the history
Revert "Fix FastTaskCompletionSource not running continution synchronously"
  • Loading branch information
dr1rrb committed Jun 28, 2018
2 parents 3c49fbe + d88a9bc commit ab0253a
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 176 deletions.
157 changes: 0 additions & 157 deletions src/Uno.Core.Tests/Threading/FastTaskCompletionSourceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//
// ******************************************************************
using System;
using System.Reactive.Concurrency;
using System.Runtime.ExceptionServices;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -442,161 +441,5 @@ public void TestSetExceptionTwice_ThrowException()
action.Should().NotThrow();
action.Should().Throw<InvalidOperationException>();
}

[TestMethod]
public void TestContinuationWhenSetResultUsingAnotherSyncContext()
{
var sut = new FastTaskCompletionSource<string>();
var scheduler = new TestScheduler();
var threadId = Thread.CurrentThread.ManagedThreadId;

var isRunning = false;
var error = default(Exception);

scheduler.Schedule(async () =>
{
isRunning = true;
try
{
Thread.CurrentThread.ManagedThreadId.Should().Be(threadId);
await sut.Task;
Thread.CurrentThread.ManagedThreadId.Should().Be(threadId);
}
catch (Exception e)
{
error = e;
}
finally
{
isRunning = false;
}
});
scheduler.Schedule(() =>
{
isRunning.Should().BeTrue();
SynchronizationContext.SetSynchronizationContext(new ErrorSyncContext());
sut.SetResult("1234");
isRunning.Should().BeFalse();
if (error != null)
{
ExceptionDispatchInfo.Capture(error).Throw();
}
});

scheduler.AdvanceBy(100);
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void TestContinuationWhenSetExceptionUsingAnotherSyncContext()
{
var sut = new FastTaskCompletionSource<string>();
var scheduler = new TestScheduler();
var threadId = Thread.CurrentThread.ManagedThreadId;

var isRunning = false;
var error = default(Exception);

scheduler.Schedule(async () =>
{
isRunning = true;
try
{
Thread.CurrentThread.ManagedThreadId.Should().Be(threadId);
await sut.Task;
Thread.CurrentThread.ManagedThreadId.Should().Be(threadId);
}
catch (Exception e)
{
error = e;
}
finally
{
isRunning = false;
}
});
scheduler.Schedule(() =>
{
isRunning.Should().BeTrue();
SynchronizationContext.SetSynchronizationContext(new ErrorSyncContext());
sut.SetException(new ArgumentNullException("xxx"));
isRunning.Should().BeFalse();
if (error != null)
{
ExceptionDispatchInfo.Capture(error).Throw();
}
});

scheduler.AdvanceBy(100);
}

[TestMethod]
[ExpectedException(typeof(OperationCanceledException))]
public void TestContinuationWhenSetCancelUsingAnotherSyncContext()
{
var sut = new FastTaskCompletionSource<string>();
var scheduler = new TestScheduler();
var threadId = Thread.CurrentThread.ManagedThreadId;

var isRunning = false;
var error = default(Exception);

scheduler.Schedule(async () =>
{
isRunning = true;
try
{
Thread.CurrentThread.ManagedThreadId.Should().Be(threadId);
await sut.Task;
Thread.CurrentThread.ManagedThreadId.Should().Be(threadId);
}
catch (Exception e)
{
error = e;
}
finally
{
isRunning = false;
}
});
scheduler.Schedule(() =>
{
isRunning.Should().BeTrue();
SynchronizationContext.SetSynchronizationContext(new ErrorSyncContext());
sut.SetCanceled();
isRunning.Should().BeFalse();
if (error != null)
{
ExceptionDispatchInfo.Capture(error).Throw();
}
});

scheduler.AdvanceBy(100);
}

private class ErrorSyncContext : SynchronizationContext
{
public override void Post(SendOrPostCallback d, object state)
{
throw new NotSupportedException("Cannot schedule anything on the ErrorSyncContext");
}

public override void Send(SendOrPostCallback d, object state)
{
throw new NotSupportedException("Cannot schedule anything on the ErrorSyncContext");
}
}
}
}
20 changes: 1 addition & 19 deletions src/Uno.Core/Threading/FastTaskCompletionSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,25 +364,7 @@ private void SpinUntilTermination()

private async Task<T> CreateAsyncTask()
{
var originalContext = SynchronizationContext.Current;
try
{
SynchronizationContext.SetSynchronizationContext(ImmediatePostSyncContext.Instance);

return await this;
}
finally
{
SynchronizationContext.SetSynchronizationContext(originalContext);
}
}

private class ImmediatePostSyncContext : SynchronizationContext
{
public static ImmediatePostSyncContext Instance { get; } = new ImmediatePostSyncContext();

/// <inheritdoc />
public override void Post(SendOrPostCallback action, object state) => action(state);
return await this;
}
}
}

0 comments on commit ab0253a

Please sign in to comment.