Ejemplo n.º 1
0
        public void MultipleConcurrentDispatchAsyncsRunInSerialFollowedByDispatchSync()
        {
            var q   = new SerialQueue(realPool);
            var hit = new List <int>();

            q.DispatchAsync(() => {
                hit.Add(1);
                Thread.Sleep(50);
                hit.Add(2);
            });
            q.DispatchAsync(() => {
                hit.Add(3);
                Thread.Sleep(50);
                hit.Add(4);
            });
            q.DispatchSync(() => {
                hit.Add(5);
            });
            q.DispatchSync(() => {
                hit.Add(6);
            });
            q.DispatchAsync(() => {
                hit.Add(7);
                Thread.Sleep(50);
                hit.Add(8);
            });
            q.DispatchSync(() => {
                hit.Add(9);
            });

            CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, hit);
        }
Ejemplo n.º 2
0
 public unsafe void CloseSocket()
 {
     _queue.DispatchAsync(() =>
     {
         _connected.Wait();
         ResetConnections();
         _c4Queue.DispatchAsync(() => Native.c4socket_closed(_socket, new C4Error()));
     });
 }
        public void TasksRunOneAtATime()
        {
            SerialQueue queue = new SerialQueue(new ManagedThreadPoolDispatcher());

            bool taskRunning = false;

            for (int i = 0; i < 10; ++i)
            {
                queue.DispatchAsync(null, (_) =>
                {
                    Assert.IsFalse(taskRunning);
                    taskRunning = true;

                    Thread.Sleep(TimeSpan.FromMilliseconds(30));

                    Assert.IsTrue(taskRunning);
                    taskRunning = false;
                });
            }

            queue.DispatchSync(null, (_) =>
            {
                // just to ensure that the tests don't finish prematurely.
            });
        }
Ejemplo n.º 4
0
        public void MultipleDispatchAsyncCallsGetProcessedByOneWorker()
        {
            var q   = new SerialQueue(mockPool);
            var hit = new List <int>();

            q.DispatchAsync(() => {
                hit.Add(1);
            });
            q.DispatchAsync(() => {
                hit.Add(2);
            });
            CollectionAssert.AreEqual(new int[0], hit);

            mockPool.RunNextAction();

            CollectionAssert.AreEqual(new[] { 1, 2 }, hit);
            Assert.AreEqual(0, mockPool.Actions.Count);
        }
        // Normal closure (requested by client)
        public unsafe void CloseSocket()
        {
            // Wait my turn!
            _queue.DispatchAsync(() =>
            {
                ResetConnections();
                _c4Queue.DispatchAsync(() =>
                {
                    if (_closed)
                    {
                        WriteLog.To.Sync.W(Tag, "Double close detected, ignoring...");
                        return;
                    }

                    WriteLog.To.Sync.I(Tag, "Closing socket normally due to request from LiteCore");
                    ReleaseSocket(new C4Error(0, 0));
                });
            });
        }
Ejemplo n.º 6
0
        public void CantCallDispatchAsyncOnDisposedQueue()
        {
            var sq = new SerialQueue(invalidPool);

            sq.Dispose();
            var hit = new List <int>();

            AssertEx.Throws <ObjectDisposedException>(() => {
                sq.DispatchAsync(() => hit.Add(1));
            });
        }
        public void AsyncRunsOnSeparateThreads()
        {
            SerialQueue queue = new SerialQueue(new ManagedThreadPoolDispatcher());

            int threadId = Thread.CurrentThread.ManagedThreadId;

            for (int i = 0; i < 10; ++i)
            {
                queue.DispatchAsync(null, (_) =>
                {
                    Assert.AreNotEqual(threadId, Thread.CurrentThread.ManagedThreadId);
                });
            }
        }
Ejemplo n.º 8
0
        private void ReachabilityChanged(object sender, NetworkReachabilityChangeEventArgs e)
        {
            Debug.Assert(e != null);

            _threadSafetyQueue.DispatchAsync(() =>
            {
                if (_repl == null && e.Status == NetworkReachabilityStatus.Reachable)
                {
                    Log.To.Sync.I(Tag, $"{this}: Server may now be reachable; retrying...");
                    _retryCount = 0;
                    Retry();
                }
            });
        }
Ejemplo n.º 9
0
        public void DispatchAsyncCanBeCanceled()
        {
            var q   = new SerialQueue(mockPool);
            var hit = new List <int>();
            var d   = q.DispatchAsync(() => hit.Add(1));

            Assert.AreEqual(1, mockPool.Actions.Count);

            d.Dispose();
            Assert.AreEqual(1, mockPool.Actions.Count); // we can't "take it out" of the threadpool as not all threadpools support that

            mockPool.RunNextAction();
            CollectionAssert.AreEqual(new int[0], hit); // lambda didn't run
            Assert.AreEqual(0, mockPool.Actions.Count);
        }
Ejemplo n.º 10
0
        public void DispatchAsyncQueuesToThreadpool()
        {
            var q   = new SerialQueue(mockPool);
            var hit = new List <int>();

            q.DispatchAsync(() => {
                hit.Add(1);
            });
            CollectionAssert.AreEqual(new int[0], hit);

            mockPool.RunNextAction();

            CollectionAssert.AreEqual(new[] { 1 }, hit);
            Assert.AreEqual(0, mockPool.Actions.Count);
        }
Ejemplo n.º 11
0
        public void NestedDispatchSyncInsideDispatchAsyncDoesntDeadlock()
        {
            var q   = new SerialQueue(new TaskThreadPool());
            var hit = new List <int>();
            var mre = new ManualResetEvent(false);

            q.DispatchAsync(() => {
                hit.Add(1);
                q.DispatchSync(() => {
                    hit.Add(2);
                });
                hit.Add(3);
                mre.Set();
            });
            mre.WaitOne();
            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, hit);
        }
Ejemplo n.º 12
0
        public void DispatchAsyncCanBeSafelyCanceledAfterItsRun()
        {
            var q   = new SerialQueue(mockPool);
            var hit = new List <int>();

            var d = q.DispatchAsync(() => hit.Add(1));

            mockPool.RunNextAction();

            CollectionAssert.AreEqual(new int[] { 1 }, hit);
            Assert.AreEqual(0, mockPool.Actions.Count);

            d.Dispose();

            CollectionAssert.AreEqual(new int[] { 1 }, hit); // lambda didn't run
            Assert.AreEqual(0, mockPool.Actions.Count);
        }
Ejemplo n.º 13
0
        public void DispatchSyncConcurrentWithAnotherDispatchAsyncWorksProperly()
        {
            var q   = new SerialQueue(new TaskThreadPool());
            var hit = new List <int>();
            var are = new AutoResetEvent(false);

            q.DispatchAsync(() => {
                hit.Add(1);
                are.Set();
                Thread.Sleep(100); // we can't block on an event as that would deadlock, we just have to slow it down enough to force the DispatchSync to wait
                hit.Add(2);
            });
            are.WaitOne();
            q.DispatchSync(() => {
                hit.Add(3);
            });

            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, hit);
        }
        public void TasksCompletedInProperSequence()
        {
            int numberTest = 0;

            SerialQueue queue = new SerialQueue(new ManagedThreadPoolDispatcher());

            for (int i = 0; i < 100; ++i)
            {
                int localValue = i;
                queue.DispatchAsync(null, (_) =>
                {
                    Assert.AreEqual(numberTest++, localValue);
                });
            }

            queue.DispatchSync(null, (_) =>
            {
                Assert.AreEqual(numberTest, 100);
            });
        }
Ejemplo n.º 15
0
 protected override void QueueTask(Task task)
 {
     _queue.DispatchAsync(() => TryExecuteTask(task));
 }