Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Maupin <ermaup@microsoft.com>2018-07-05 23:59:24 +0300
committerEric Maupin <ermaup@microsoft.com>2018-07-19 00:20:56 +0300
commit1f19f945eceaa30a3259b0117d910db091d739e5 (patch)
treeb4516f987bedfdac422bf67d442de87062477435 /Xamarin.PropertyEditing.Tests
parent1300d59c4780ab25595c38c10321f63426ef74e5 (diff)
[Tests] Fix AsyncSynchronizationContext
We don't actually need the complicated version of this that seemed flawed. We just need to provide a context and execute the calls. We may need to make this more like the original later, but we're generally not executing things off-thread within our VMs.
Diffstat (limited to 'Xamarin.PropertyEditing.Tests')
-rw-r--r--Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs113
1 files changed, 6 insertions, 107 deletions
diff --git a/Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs b/Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs
index 6109c7d..34a7086 100644
--- a/Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs
+++ b/Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs
@@ -1,118 +1,17 @@
-// Copyright (c) 2004-2013 Charlie Poole
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-
-
-using System;
-using System.Collections;
using System.Threading;
-namespace NUnit.Framework.Internal
+namespace Xamarin.PropertyEditing.Tests
{
- internal class AsyncSynchronizationContext : SynchronizationContext
+ internal class AsyncSynchronizationContext
+ : SynchronizationContext
{
- private int _operationCount;
- private readonly AsyncOperationQueue _operations = new AsyncOperationQueue();
-
- public override void Send(SendOrPostCallback d, object state)
- {
- throw new InvalidOperationException("Sending to this synchronization context is not supported");
- }
-
- public override void Post(SendOrPostCallback d, object state)
- {
- _operations.Enqueue(new AsyncOperation(d, state));
- }
-
- public override void OperationStarted()
- {
- Interlocked.Increment(ref _operationCount);
- base.OperationStarted();
- }
-
- public override void OperationCompleted()
- {
- if (Interlocked.Decrement(ref _operationCount) == 0)
- _operations.MarkAsComplete();
-
- base.OperationCompleted();
- }
-
- public void WaitForPendingOperationsToComplete()
+ public override void Post (SendOrPostCallback d, object state)
{
- _operations.InvokeAll();
+ d (state);
}
- private class AsyncOperationQueue
+ public void WaitForPendingOperationsToComplete ()
{
- private bool _run = true;
- private readonly Queue _operations = Queue.Synchronized(new Queue());
- private readonly AutoResetEvent _operationsAvailable = new AutoResetEvent(false);
-
- public void Enqueue(AsyncOperation asyncOperation)
- {
- _operations.Enqueue(asyncOperation);
- _operationsAvailable.Set();
- }
-
- public void MarkAsComplete()
- {
- _run = false;
- _operationsAvailable.Set();
- }
-
- public void InvokeAll()
- {
- while (_run)
- {
- InvokePendingOperations();
- _operationsAvailable.WaitOne();
- }
-
- InvokePendingOperations();
- }
-
- private void InvokePendingOperations()
- {
- while (_operations.Count > 0)
- {
- AsyncOperation operation = (AsyncOperation)_operations.Dequeue();
- operation.Invoke();
- }
- }
- }
-
- private class AsyncOperation
- {
- private readonly SendOrPostCallback _action;
- private readonly object _state;
-
- public AsyncOperation(SendOrPostCallback action, object state)
- {
- _action = action;
- _state = state;
- }
-
- public void Invoke()
- {
- _action(_state);
- }
}
}
} \ No newline at end of file