Example #1
0
        public void TestAsyncPushList()
        {
            ThreadSafeStack <int> integers = new ThreadSafeStack <int>();

            Task t1 = Task.Run(
                () => {
                List <int> data = new List <int>(500);
                for (int i = 0; i < 1000; i += 2)
                {
                    System.Threading.Thread.Sleep(10);
                    data.Add(i);
                }

                integers.Push(data);
            }
                );

            Task t2 = Task.Run(
                () => {
                List <int> data = new List <int>(500);
                for (int i = 1; i < 1000; i += 2)
                {
                    System.Threading.Thread.Sleep(10);
                    data.Add(i);
                }

                integers.Push(data);
            }
                );

            Task.WaitAll(t1, t2);

            Assert.AreEqual(1000, integers.Count);
        }
Example #2
0
        public void TestAsyncPush()
        {
            ThreadSafeStack <int> integers = new ThreadSafeStack <int>();

            Task t1 = Task.Run(
                () => {
                for (int i = 0; i < 1000000; ++i)
                {
                    integers.Push(i);
                }
            }
                );

            Task t2 = Task.Run(
                () => {
                for (int i = 0; i < 1000000; ++i)
                {
                    integers.Push(i);
                }
            }
                );

            Task.WaitAll(t1, t2);

            Assert.AreEqual(2000000, integers.Count);
        }
        public void PushTest()
        {
            for (var i = 0; i < 100; i++)
            {
                stack.Push(Rnd.Next(MaxNumber));
            }

            for (var i = 0; i < 100; i++)
            {
                stack.Pop().Should().Be.GreaterThanOrEqualTo(0).And.Be.LessThan(MaxNumber);
            }

            stack.Count.Should().Be(0);
        }
            /// <summary>
            ///   添加transaction
            /// </summary>
            /// <param name="manager"> </param>
            /// <param name="transaction"> </param>
            public void Start(ITransaction transaction, bool forked)
            {
                if (_mStack.Count != 0)
                {
                    // In the corresponding Java code, standAlone is NOT set to false here
                    // transaction.Standalone = false;

                    // Do NOT make strong reference from parent transaction to forked transaction.
                    // Instead, we create a "soft" reference to forked transaction later, via linkAsRunAway()
                    // By doing so, there is no need for synchronization between parent and child threads.
                    // Both threads can complete() anytime despite the other thread.
                    if (!(transaction is IForkedTransaction))
                    {
                        ITransaction parent = _mStack.Peek();
                        AddTransactionChild(transaction, parent);
                    }
                }
                else
                {
                    if (_mTree.MessageId == null)
                    {
                        _mTree.MessageId = _mManager.NextMessageId();
                    }

                    _mTree.Message = transaction;
                }

                if (!forked)
                {
                    Tree.EstimatedByteSize += transaction.EstimateByteSize();
                    _mStack.Push(transaction);
                }
            }
Example #5
0
        public void TestAsyncPop()
        {
            ThreadSafeStack <int> integers = new ThreadSafeStack <int>();

            for (int i = 3; i < 1000000; ++i)
            {
                integers.Push(i);
            }

            Task t1 = Task.Run(
                () => {
                while (!integers.IsEmpty())
                {
                    integers.Pop();
                }
            }
                );

            Task t2 = Task.Run(
                () => {
                while (!integers.IsEmpty())
                {
                    integers.Pop();
                }
            }
                );

            Task.WaitAll(t1, t2);

            Assert.AreEqual(0, integers.Count);
        }
 public void AddOperation(UndoableOperation <TClass> operation)
 {
     Added(this, new GenericEventArgs <UndoableOperation <TClass> >(operation));
     _undoStack.Push(operation);
 }