コード例 #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);
        }
コード例 #2
0
        public void NestedDispatchSyncDoesntDeadlock()
        {
            var q   = new SerialQueue(new InvalidThreadPool());
            var hit = new List <int>();

            q.DispatchSync(() => {
                hit.Add(1);
                q.DispatchSync(() => {
                    hit.Add(2);
                });
                hit.Add(3);
            });
            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, hit);
        }
コード例 #3
0
        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.
            });
        }
コード例 #4
0
        public void DispatchSyncRunsNormally()
        {
            var q   = new SerialQueue(new InvalidThreadPool());
            var hit = new List <int>();

            q.DispatchSync(() => {
                hit.Add(1);
            });
            CollectionAssert.AreEqual(new[] { 1 }, hit);
        }
コード例 #5
0
        /// <summary>
        /// Starts the replication
        /// </summary>
        public void Start()
        {
            _threadSafetyQueue.DispatchSync(() =>
            {
                if (_disposed)
                {
                    throw new ObjectDisposedException("Replication cannot be started after disposal");
                }

                if (_repl != null)
                {
                    Log.To.Sync.W(Tag, $"{this} has already started");
                    return;
                }

                Log.To.Sync.I(Tag, $"{this}: Starting");
                _retryCount = 0;
                StartInternal();
            });
        }
コード例 #6
0
        public void CantCallDispatchSyncOnDisposedQueue()
        {
            var sq = new SerialQueue(invalidPool);

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

            AssertEx.Throws <ObjectDisposedException>(() => {
                sq.DispatchSync(() => hit.Add(1));
            });
        }
コード例 #7
0
        public void SyncRunsSynchronously()
        {
            SerialQueue queue = new SerialQueue(new ManagedThreadPoolDispatcher());

            int numberTest = 0;

            for (int i = 0; i < 100; ++i)
            {
                queue.DispatchSync(null, (_) =>
                {
                    Assert.AreEqual(numberTest++, i);
                });
            }
        }
コード例 #8
0
        public void SyncRunsOnSeparateThreads()
        {
            SerialQueue queue = new SerialQueue(new ManagedThreadPoolDispatcher());

            int threadId = Thread.CurrentThread.ManagedThreadId;

            for (int i = 0; i < 10; ++i)
            {
                queue.DispatchSync(null, (_) =>
                {
                    Assert.AreNotEqual(threadId, Thread.CurrentThread.ManagedThreadId);
                });
            }
        }
コード例 #9
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);
        }
コード例 #10
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);
        }
コード例 #11
0
        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);
            });
        }
コード例 #12
0
        public async Task TestSerialQueue()
        {
            var queue  = new SerialQueue();
            var now    = DateTime.Now;
            var then   = now;
            var ignore = queue.DispatchAfter(() => then = DateTime.Now, TimeSpan.FromSeconds(1));
            await Task.Delay(250);

            then.Should().Be(now);
            await Task.Delay(800);

            then.Should().NotBe(now);

            var testBool = false;

            queue.DispatchSync(() => Volatile.Read(ref testBool)).Should().BeFalse();

            var t = queue.DispatchAfter(() => Volatile.Read(ref testBool), TimeSpan.FromMilliseconds(500));

            Volatile.Write(ref testBool, true);
            (await t).Should().BeTrue();
        }