public void TestUsage()
        {
            MemoryUsage usage1 = new MemoryUsage(2048);

            Assert.That(!usage1.IsFull());
            Assert.That(usage1.Usage == 0);

            usage1.IncreaseUsage(1024);

            Assert.That(!usage1.IsFull());
            Assert.That(usage1.Usage == 1024);

            usage1.DecreaseUsage(512);

            Assert.That(!usage1.IsFull());
            Assert.That(usage1.Usage == 512);

            usage1.Usage = 2048;

            Assert.That(usage1.IsFull());
            Assert.That(usage1.Usage == 2048);

            usage1.IncreaseUsage(1024);
            Assert.That(usage1.IsFull());
            Assert.That(usage1.Usage == 3072);
        }
        internal async Task DoSendAsync(ActiveMQDestination destination, ActiveMQMessage message,
                                        MessageProducer producer, MemoryUsage producerWindow, TimeSpan sendTimeout)
        {
            ActiveMQMessage msg = message;

            if (destination.IsTemporary && !connection.IsTempDestinationActive(destination as ActiveMQTempDestination))
            {
                throw new InvalidDestinationException("Cannot publish to a deleted Destination: " + destination);
            }

            if (IsTransacted)
            {
                DoStartTransaction();
                msg.TransactionId = TransactionContext.TransactionId;
            }

            msg.RedeliveryCounter = 0;
            msg.BrokerPath        = null;

            if (this.connection.CopyMessageOnSend)
            {
                msg = (ActiveMQMessage)msg.Clone();
            }

            msg.OnSend();
            msg.ProducerId = msg.MessageId.ProducerId;

            if (sendTimeout.TotalMilliseconds <= 0 && !msg.ResponseRequired && !connection.AlwaysSyncSend &&
                (!msg.Persistent || connection.AsyncSend || msg.TransactionId != null))
            {
                this.connection.Oneway(msg);

                if (producerWindow != null)
                {
                    // Since we defer lots of the marshaling till we hit the wire, this
                    // might not provide and accurate size. We may change over to doing
                    // more aggressive marshaling, to get more accurate sizes.. this is more
                    // important once users start using producer window flow control.
                    producerWindow.IncreaseUsage(msg.Size());
                }
            }
            else
            {
                if (sendTimeout.TotalMilliseconds > 0)
                {
                    await this.connection.SyncRequestAsync(msg, sendTimeout).Await();
                }
                else
                {
                    await this.connection.SyncRequestAsync(msg).Await();
                }
            }
        }
        public void TestTimedWait()
        {
            MemoryUsage usage = new MemoryUsage(2048);

            usage.IncreaseUsage(5072);

            DateTime start = DateTime.Now;

            usage.WaitForSpace(TimeSpan.FromMilliseconds(150));

            DateTime end = DateTime.Now;

            TimeSpan timePassed = end - start;

            Assert.That(timePassed.TotalMilliseconds >= 125);
        }
        public void TestWait()
        {
            MemoryUsage usage = new MemoryUsage(2048);

            usage.IncreaseUsage(5072);

            Thread thread1 = new Thread(delegate()
            {
                Thread.Sleep(100);
                usage.DecreaseUsage(usage.Usage);
            });

            thread1.Start();

            usage.WaitForSpace();
            Assert.That(usage.Usage == 0);

            thread1.Join();
        }