activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r819685 - in /activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src: main/csharp/MessageProducer.cs main/csharp/Session.cs main/csharp/Util/ main/csharp/Util/MemoryUsage.cs test/csharp/Util/ test/csharp/Util/MemoryUsageTest.cs
Date Mon, 28 Sep 2009 19:31:25 GMT
Author: tabish
Date: Mon Sep 28 19:31:25 2009
New Revision: 819685

URL: http://svn.apache.org/viewvc?rev=819685&view=rev
Log:
https://issues.apache.org/activemq/browse/AMQNET-192

Moving the MemoryUsage into NMS.ActiveMQ since its specific to this Library and allows the
use of .NET 2.0 Monitor locks for safer thread control.

Added:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/MemoryUsage.cs
  (with props)
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/MemoryUsageTest.cs
  (with props)
Modified:
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageProducer.cs
    activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Session.cs

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageProducer.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageProducer.cs?rev=819685&r1=819684&r2=819685&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageProducer.cs
(original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/MessageProducer.cs
Mon Sep 28 19:31:25 2009
@@ -19,265 +19,266 @@
 using System.Threading;
 using Apache.NMS.Util;
 using Apache.NMS.ActiveMQ.Commands;
+using Apache.NMS.ActiveMQ.Util;
 
 namespace Apache.NMS.ActiveMQ
 {
-	/// <summary>
-	/// An object capable of sending messages to some destination
-	/// </summary>
-	public class MessageProducer : IMessageProducer
-	{
-		private Session session;
+    /// <summary>
+    /// An object capable of sending messages to some destination
+    /// </summary>
+    public class MessageProducer : IMessageProducer
+    {
+        private Session session;
         private MemoryUsage usage = null;
-		private bool closed = false;
+        private bool closed = false;
         private object closedLock = new object();
-		private readonly ProducerInfo info;
+        private readonly ProducerInfo info;
         private int producerSequenceId = 0;
 
-		private MsgDeliveryMode msgDeliveryMode = NMSConstants.defaultDeliveryMode;
-		private TimeSpan requestTimeout = NMSConstants.defaultRequestTimeout;
-		private TimeSpan msgTimeToLive = NMSConstants.defaultTimeToLive;
-		private MsgPriority msgPriority = NMSConstants.defaultPriority;
-		private bool disableMessageID = false;
-		private bool disableMessageTimestamp = false;
-		protected bool disposed = false;
-
-		public MessageProducer(Session session, ProducerInfo info)
-		{
-			this.session = session;
-			this.info = info;
-			this.RequestTimeout = session.RequestTimeout;
-            
+        private MsgDeliveryMode msgDeliveryMode = NMSConstants.defaultDeliveryMode;
+        private TimeSpan requestTimeout = NMSConstants.defaultRequestTimeout;
+        private TimeSpan msgTimeToLive = NMSConstants.defaultTimeToLive;
+        private MsgPriority msgPriority = NMSConstants.defaultPriority;
+        private bool disableMessageID = false;
+        private bool disableMessageTimestamp = false;
+        protected bool disposed = false;
+
+        public MessageProducer(Session session, ProducerInfo info)
+        {
+            this.session = session;
+            this.info = info;
+            this.RequestTimeout = session.RequestTimeout;
+
             // Version Three and higher will send us a ProducerAck, but only if we
             // have a set producer window size.
             if( session.Connection.ProtocolVersion >= 3 && info.WindowSize >
0 )
             {
                 usage = new MemoryUsage( info.WindowSize );
             }
-		}
+        }
+
+        ~MessageProducer()
+        {
+            Dispose(false);
+        }
+
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        protected void Dispose(bool disposing)
+        {
+            if(disposed)
+            {
+                return;
+            }
+
+            if(disposing)
+            {
+                // Dispose managed code here.
+            }
+
+            try
+            {
+                Close();
+            }
+            catch
+            {
+                // Ignore network errors.
+            }
+
+            disposed = true;
+        }
+
+        public void Close()
+        {
+            lock(closedLock)
+            {
+                if(closed)
+                {
+                    return;
+                }
 
-		~MessageProducer()
-		{
-			Dispose(false);
-		}
-
-		public void Dispose()
-		{
-			Dispose(true);
-			GC.SuppressFinalize(this);
-		}
-
-		protected void Dispose(bool disposing)
-		{
-			if(disposed)
-			{
-				return;
-			}
-
-			if(disposing)
-			{
-				// Dispose managed code here.
-			}
-
-			try
-			{
-				Close();
-			}
-			catch
-			{
-				// Ignore network errors.
-			}
-
-			disposed = true;
-		}
-
-		public void Close()
-		{
-			lock(closedLock)
-			{
-				if(closed)
-				{
-					return;
-				}
-
-				try
-				{
-					session.DisposeOf(info.ProducerId);
-				}
-				catch(Exception ex)
-				{
-					Tracer.ErrorFormat("Error during producer close: {0}", ex);
-				}
+                try
+                {
+                    session.DisposeOf(info.ProducerId);
+                }
+                catch(Exception ex)
+                {
+                    Tracer.ErrorFormat("Error during producer close: {0}", ex);
+                }
 
                 if(this.usage != null)
                 {
                     this.usage.Stop();
                 }
-                
-				session = null;
-				closed = true;
-			}
-		}
-
-		public void Send(IMessage message)
-		{
-			Send(info.Destination, message, this.msgDeliveryMode, this.msgPriority, this.msgTimeToLive,
false);
-		}
-
-		public void Send(IDestination destination, IMessage message)
-		{
-			Send(destination, message, this.msgDeliveryMode, this.msgPriority, this.msgTimeToLive,
false);
-		}
-
-		public void Send(IMessage message, MsgDeliveryMode deliveryMode, MsgPriority priority,
TimeSpan timeToLive)
-		{
-			Send(info.Destination, message, deliveryMode, priority, timeToLive, true);
-		}
-
-		public void Send(IDestination destination, IMessage message, MsgDeliveryMode deliveryMode,
MsgPriority priority, TimeSpan timeToLive)
-		{
-			Send(destination, message, deliveryMode, priority, timeToLive, true);
-		}
-
-		protected void Send(IDestination destination, IMessage message, MsgDeliveryMode deliveryMode,
MsgPriority priority, TimeSpan timeToLive, bool specifiedTimeToLive)
-		{
-			if(null == destination)
-			{
-				// See if this producer was created without a destination.
-				if(null == info.Destination)
-				{
-					throw new NotSupportedException();
-				}
-
-				// The producer was created with a destination, but an invalid destination
-				// was specified.
-				throw new Apache.NMS.InvalidDestinationException();
-			}
-
-			ActiveMQMessage activeMessage = (ActiveMQMessage) message;
-
-			activeMessage.ProducerId = info.ProducerId;
-			activeMessage.FromDestination = destination;
-			activeMessage.NMSDeliveryMode = deliveryMode;
-			activeMessage.NMSPriority = priority;
+
+                session = null;
+                closed = true;
+            }
+        }
+
+        public void Send(IMessage message)
+        {
+            Send(info.Destination, message, this.msgDeliveryMode, this.msgPriority, this.msgTimeToLive,
false);
+        }
+
+        public void Send(IDestination destination, IMessage message)
+        {
+            Send(destination, message, this.msgDeliveryMode, this.msgPriority, this.msgTimeToLive,
false);
+        }
+
+        public void Send(IMessage message, MsgDeliveryMode deliveryMode, MsgPriority priority,
TimeSpan timeToLive)
+        {
+            Send(info.Destination, message, deliveryMode, priority, timeToLive, true);
+        }
+
+        public void Send(IDestination destination, IMessage message, MsgDeliveryMode deliveryMode,
MsgPriority priority, TimeSpan timeToLive)
+        {
+            Send(destination, message, deliveryMode, priority, timeToLive, true);
+        }
+
+        protected void Send(IDestination destination, IMessage message, MsgDeliveryMode deliveryMode,
MsgPriority priority, TimeSpan timeToLive, bool specifiedTimeToLive)
+        {
+            if(null == destination)
+            {
+                // See if this producer was created without a destination.
+                if(null == info.Destination)
+                {
+                    throw new NotSupportedException();
+                }
+
+                // The producer was created with a destination, but an invalid destination
+                // was specified.
+                throw new Apache.NMS.InvalidDestinationException();
+            }
+
+            ActiveMQMessage activeMessage = (ActiveMQMessage) message;
+
+            activeMessage.ProducerId = info.ProducerId;
+            activeMessage.FromDestination = destination;
+            activeMessage.NMSDeliveryMode = deliveryMode;
+            activeMessage.NMSPriority = priority;
 
             // Always set the message Id regardless of the disable flag.
             MessageId id = new MessageId();
             id.ProducerId = info.ProducerId;
             id.ProducerSequenceId = Interlocked.Increment(ref this.producerSequenceId);
             activeMessage.MessageId = id;
-            
-			if(!disableMessageTimestamp)
-			{
-				activeMessage.NMSTimestamp = DateTime.UtcNow;
-			}
-
-			if(specifiedTimeToLive)
-			{
-				activeMessage.NMSTimeToLive = timeToLive;
-			}
 
-            // Ensure there's room left to send this message            
+            if(!disableMessageTimestamp)
+            {
+                activeMessage.NMSTimestamp = DateTime.UtcNow;
+            }
+
+            if(specifiedTimeToLive)
+            {
+                activeMessage.NMSTimeToLive = timeToLive;
+            }
+
+            // Ensure there's room left to send this message
             if(this.usage != null)
             {
                 usage.WaitForSpace();
             }
-            
-			lock(closedLock)
-			{
-				if(closed)
-				{
-					throw new ConnectionClosedException();
-				}
-
-				session.DoSend(activeMessage, this, this.usage, this.RequestTimeout);
-			}
-		}
+
+            lock(closedLock)
+            {
+                if(closed)
+                {
+                    throw new ConnectionClosedException();
+                }
+
+                session.DoSend(activeMessage, this, this.usage, this.RequestTimeout);
+            }
+        }
 
         public ProducerId ProducerId
         {
             get { return info.ProducerId; }
         }
-        
-		public MsgDeliveryMode DeliveryMode
-		{
-			get { return msgDeliveryMode; }
-			set { this.msgDeliveryMode = value; }
-		}
-
-		public TimeSpan TimeToLive
-		{
-			get { return msgTimeToLive; }
-			set { this.msgTimeToLive = value; }
-		}
-
-		public TimeSpan RequestTimeout
-		{
-			get { return requestTimeout; }
-			set { this.requestTimeout = value; }
-		}
-
-		public MsgPriority Priority
-		{
-			get { return msgPriority; }
-			set { this.msgPriority = value; }
-		}
-
-		public bool DisableMessageID
-		{
-			get { return disableMessageID; }
-			set { this.disableMessageID = value; }
-		}
-
-		public bool DisableMessageTimestamp
-		{
-			get { return disableMessageTimestamp; }
-			set { this.disableMessageTimestamp = value; }
-		}
-
-		public IMessage CreateMessage()
-		{
-			return session.CreateMessage();
-		}
-
-		public ITextMessage CreateTextMessage()
-		{
-			return session.CreateTextMessage();
-		}
-
-		public ITextMessage CreateTextMessage(string text)
-		{
-			return session.CreateTextMessage(text);
-		}
-
-		public IMapMessage CreateMapMessage()
-		{
-			return session.CreateMapMessage();
-		}
-
-		public IObjectMessage CreateObjectMessage(object body)
-		{
-			return session.CreateObjectMessage(body);
-		}
-
-		public IBytesMessage CreateBytesMessage()
-		{
-			return session.CreateBytesMessage();
-		}
-
-		public IBytesMessage CreateBytesMessage(byte[] body)
-		{
-			return session.CreateBytesMessage(body);
-		}
-        
+
+        public MsgDeliveryMode DeliveryMode
+        {
+            get { return msgDeliveryMode; }
+            set { this.msgDeliveryMode = value; }
+        }
+
+        public TimeSpan TimeToLive
+        {
+            get { return msgTimeToLive; }
+            set { this.msgTimeToLive = value; }
+        }
+
+        public TimeSpan RequestTimeout
+        {
+            get { return requestTimeout; }
+            set { this.requestTimeout = value; }
+        }
+
+        public MsgPriority Priority
+        {
+            get { return msgPriority; }
+            set { this.msgPriority = value; }
+        }
+
+        public bool DisableMessageID
+        {
+            get { return disableMessageID; }
+            set { this.disableMessageID = value; }
+        }
+
+        public bool DisableMessageTimestamp
+        {
+            get { return disableMessageTimestamp; }
+            set { this.disableMessageTimestamp = value; }
+        }
+
+        public IMessage CreateMessage()
+        {
+            return session.CreateMessage();
+        }
+
+        public ITextMessage CreateTextMessage()
+        {
+            return session.CreateTextMessage();
+        }
+
+        public ITextMessage CreateTextMessage(string text)
+        {
+            return session.CreateTextMessage(text);
+        }
+
+        public IMapMessage CreateMapMessage()
+        {
+            return session.CreateMapMessage();
+        }
+
+        public IObjectMessage CreateObjectMessage(object body)
+        {
+            return session.CreateObjectMessage(body);
+        }
+
+        public IBytesMessage CreateBytesMessage()
+        {
+            return session.CreateBytesMessage();
+        }
+
+        public IBytesMessage CreateBytesMessage(byte[] body)
+        {
+            return session.CreateBytesMessage(body);
+        }
+
         public void OnProducerAck(ProducerAck ack)
         {
             Tracer.Debug("Received ProducerAck for Message of Size = {" + ack.Size + "}"
);
-            
+
             if(this.usage != null)
             {
                 this.usage.DecreaseUsage( ack.Size );
             }
         }
-	}
+    }
 }

Modified: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Session.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Session.cs?rev=819685&r1=819684&r2=819685&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Session.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Session.cs Mon Sep
28 19:31:25 2009
@@ -19,6 +19,7 @@
 using System.Collections;
 using System.Threading;
 using Apache.NMS.ActiveMQ.Commands;
+using Apache.NMS.ActiveMQ.Util;
 using Apache.NMS.Util;
 
 namespace Apache.NMS.ActiveMQ
@@ -44,7 +45,7 @@
         private bool closed = false;
         private bool closing = false;
         private TimeSpan MAX_THREAD_WAIT = TimeSpan.FromMilliseconds(30000);
-        
+
         public Session(Connection connection, SessionInfo info, AcknowledgementMode acknowledgementMode)
         {
             this.connection = connection;
@@ -223,7 +224,7 @@
 
                 throw;
             }
-            
+
             // Registered with Connection so it can process Producer Acks.
             connection.addProducer(producerId, producer);
 
@@ -494,13 +495,13 @@
         public void DoSend( ActiveMQMessage message, MessageProducer producer, MemoryUsage
producerWindow, TimeSpan sendTimeout )
         {
             ActiveMQMessage msg = message;
-            
+
             if(Transacted)
             {
                 DoStartTransaction();
                 msg.TransactionId = TransactionContext.TransactionId;
             }
-                        
+
             msg.RedeliveryCounter = 0;
             msg.BrokerPath = null;
 
@@ -508,25 +509,25 @@
             {
                 msg = (ActiveMQMessage)msg.Clone();
             }
-            
+
             msg.OnSend();
             msg.ProducerId = msg.MessageId.ProducerId;
-            
-            if(sendTimeout.TotalMilliseconds <= 0 && !msg.ResponseRequired &&
!connection.AlwaysSyncSend && 
+
+            if(sendTimeout.TotalMilliseconds <= 0 && !msg.ResponseRequired &&
!connection.AlwaysSyncSend &&
                (!msg.Persistent || connection.AsyncSend || msg.TransactionId != null))
             {
                 this.connection.Oneway(msg);
-                
-                if(producerWindow != null) 
+
+                if(producerWindow != null)
                 {
-                    // Since we defer lots of the marshaling till we hit the wire, this 
+                    // 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

+                    // 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 
+            }
+            else
             {
                 if(sendTimeout.TotalMilliseconds > 0)
                 {
@@ -536,7 +537,7 @@
                 {
                     this.connection.SyncRequest(msg);
                 }
-            }            
+            }
         }
 
         /// <summary>

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/MemoryUsage.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/MemoryUsage.cs?rev=819685&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/MemoryUsage.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/MemoryUsage.cs
Mon Sep 28 19:31:25 2009
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using System;
+using System.Threading;
+using Apache.NMS.Util;
+
+namespace Apache.NMS.ActiveMQ.Util
+{
+    /// <summary>
+    /// Utility class for Tracking Memory Usage with an imposed limit on the amount
+    /// available.  Provides methods for objects to wait on more space to become
+    /// available if the memory limit is reached.
+    /// </summary>
+    public class MemoryUsage
+    {
+        private readonly Atomic<bool> stopped = new Atomic<bool>(false);
+        private long limit = 0;
+        private long usage = 0;
+        private readonly object mutex = new object();
+
+        public MemoryUsage()
+        {
+        }
+
+        public MemoryUsage( long limit )
+        {
+            this.limit = limit;
+        }
+
+        #region Property Accessors
+
+        public long Limit
+        {
+            get { return limit; }
+            set { limit = value; }
+        }
+
+        public long Usage
+        {
+            get { return usage; }
+            set { usage = value; }
+        }
+
+        #endregion
+
+        /// <summary>
+        /// If no space is available then this method blocks until more becomes available.
+        /// </summary>
+        public void WaitForSpace()
+        {
+            TimeSpan indefiniteWait = TimeSpan.FromMilliseconds(Timeout.Infinite);
+            this.WaitForSpace(indefiniteWait);
+        }
+
+        /// <summary>
+        /// If no space is available then this method blocks until more becomes available
+        /// or until the specified timeout expires.
+        /// </summary>
+        /// <param name="timeout">
+        /// A <see cref="System.TimeSpan"/>
+        /// </param>
+        public void WaitForSpace( TimeSpan timeout )
+        {
+            lock(this.mutex)
+            {
+                while(this.IsFull() && !stopped.Value)
+                {
+                    if( !Monitor.Wait(this.mutex, timeout ) )
+                    {
+                        return;
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// Attempts to increase the amount of Memory Used, if non is available to fill
+        /// then this method blocks until more is freed.
+        /// </summary>
+        /// <param name="usage">
+        /// A <see cref="System.Int64"/>
+        /// </param>
+        public void EnqueueUsage( long usage )
+        {
+            this.WaitForSpace();
+            this.IncreaseUsage(usage);
+        }
+
+        /// <summary>
+        /// Increase the level of Usage.
+        /// </summary>
+        /// <param name="value">
+        /// A <see cref="System.Int64"/>
+        /// </param>
+        public void IncreaseUsage( long value )
+        {
+            if(value == 0)
+            {
+                return;
+            }
+
+            lock(this.mutex)
+            {
+                this.Usage += value;
+            }
+        }
+
+        /// <summary>
+        /// Decrease the level of Usage.
+        /// </summary>
+        /// <param name="value">
+        /// A <see cref="System.Int64"/>
+        /// </param>
+        public void DecreaseUsage(long value)
+        {
+            if(value == 0)
+            {
+                return;
+            }
+
+            lock(this.mutex)
+            {
+                if( value > this.Usage )
+                {
+                    this.Usage = 0;
+                }
+                else
+                {
+                    this.Usage -= value;
+                }
+
+                Monitor.PulseAll(this.mutex);
+            }
+        }
+
+        /// <summary>
+        /// Checks if the Usage Windows has become full, is so returns true
+        /// otherwise returns false.
+        /// </summary>
+        /// <returns>
+        /// A <see cref="System.Boolean"/>
+        /// </returns>
+        public bool IsFull()
+        {
+            bool result = false;
+
+            lock(this.mutex)
+            {
+                result = this.Usage >= this.Limit;
+            }
+
+            return result;
+        }
+
+        public void Stop()
+        {
+            this.stopped.Value = true;
+            lock(this.mutex)
+            {
+                Monitor.PulseAll(this.mutex);
+            }
+        }
+    }
+}

Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/main/csharp/Util/MemoryUsage.cs
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/MemoryUsageTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/MemoryUsageTest.cs?rev=819685&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/MemoryUsageTest.cs
(added)
+++ activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/MemoryUsageTest.cs
Mon Sep 28 19:31:25 2009
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using System.Threading;
+using Apache.NMS.Test;
+using Apache.NMS.ActiveMQ.Util;
+using NUnit.Framework;
+using NUnit.Framework.Extensions;
+
+namespace Apache.NMS.ActiveMQ.Test
+{
+    [TestFixture]
+    public class MemoryUsageTest : NMSTestSupport
+    {
+
+        [Test]
+        public void TestConstructors()
+        {
+            MemoryUsage usage = new MemoryUsage();
+
+            Assert.That(usage.Limit == 0);
+            Assert.That(usage.Usage == 0);
+
+            usage = new MemoryUsage(1024);
+
+            Assert.That(usage.Limit == 1024);
+            Assert.That(usage.Usage == 0);
+        }
+
+        [Test]
+        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 );
+        }
+
+        [Test]
+        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 );
+        }
+
+        [Test]
+        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();
+        }
+    }
+}

Propchange: activemq/activemq-dotnet/Apache.NMS.ActiveMQ/trunk/src/test/csharp/Util/MemoryUsageTest.cs
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message