Imported Upstream version 5.20.0.185

Former-commit-id: fcd643a64a9c5470917dbbb1be4ba228e5c14650
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2019-02-06 08:26:08 +00:00
parent 97f8185566
commit f737def5f5
55 changed files with 79 additions and 46 deletions

View File

@ -491,6 +491,35 @@ namespace MonoTests.System.Threading
c1.Dispose ();
}
}
[Test] // https://github.com/mono/mono/issues/12421
public void EnsurePostIsNotCalled ()
{
SynchronizationContext mainContext = SynchronizationContext.Current;
var asc = new AssertSyncContext ();
SynchronizationContext.SetSynchronizationContext (asc);
var ct = new CancellationTokenSource ();
var tcs = new TaskCompletionSource<bool> ();
ct.Token.Register (() => tcs.TrySetCanceled ());
bool taskIsCancelled = false;
Action awaitAction = async () => {
try { await tcs.Task; }
catch (OperationCanceledException) {
taskIsCancelled = true;
}
};
awaitAction ();
ct.Cancel (); // should not trigger SynchronizationContext.Post
Assert.IsTrue (taskIsCancelled);
SynchronizationContext.SetSynchronizationContext (mainContext);
}
class AssertSyncContext : SynchronizationContext
{
public override void Post (SendOrPostCallback d, object state) =>
throw new InvalidOperationException ("SynchronizationContext.Post was not expected.");
}
}
}