Example #1
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 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>
            ///  return true means the transaction has been flushed.
            ///</summary>
            ///<param name="manager"> </param>
            ///<param name="transaction"> </param>
            ///<returns> true if message is flushed, false otherwise </returns>
            public bool End(ITransaction transaction)
            {
                lock (_mStack.SyncRoot)
                {
                    if (_mStack.Count != 0)
                    {
                        ITransaction current = _mStack.Peek();

                        if (transaction == current)
                        {
                            _mStack.Pop();
                            _mManager._mValidator.Validate(_mStack.Count == 0 ? null : _mStack.Peek(), current);
                        }
                        else
                        {
                            if (_mStack.FirstOrDefault(item => item == transaction) != null)
                            {
                                current = _mStack.Pop();
                                while (transaction != current && _mStack.Count != 0)
                                {
                                    _mManager._mValidator.Validate(_mStack.Peek(), current);

                                    current = _mStack.Pop();
                                }
                            }
                        }

                        if (_mStack.Count == 0)
                        {
                            IMessageTree tree = _mTree.Copy();
                            _mTree.MessageId         = null;
                            _mTree.Message           = null;
                            _mTree.EstimatedByteSize = 0;

                            if (_mTotalDurationInMicros > 0)
                            {
                                AdjustForTruncatedTransaction((ITransaction)tree.Message);
                            }

                            _mManager.Flush(tree);
                            return(true);
                        }
                    }
                    return(false);
                }
            }
 public void UndoLastOperation()
 {
     if (!_undoStack.IsEmpty)
     {
         UndoableOperation <TClass> undoableOperation = _undoStack.Pop();
         Undone(this, new GenericEventArgs <UndoableOperation <TClass> >(undoableOperation));
         undoableOperation.Implementor.Undo(undoableOperation.Command);
     }
 }