activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r817390 - in /activemq/activemq-dotnet/Apache.NMS/trunk/src: main/csharp/Util/MemoryUsage.cs test/csharp/MemoryUsageTest.cs
Date Mon, 21 Sep 2009 20:05:45 GMT
Author: tabish
Date: Mon Sep 21 20:05:45 2009
New Revision: 817390

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

Adds class MemoryUsage for tracking a Producer overall memory for use in ProducerAck processing.

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

Added: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MemoryUsage.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MemoryUsage.cs?rev=817390&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MemoryUsage.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/MemoryUsage.cs Mon Sep
21 20:05:45 2009
@@ -0,0 +1,160 @@
+/*
+ * 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;
+
+namespace Apache.NMS.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 long limit = 0;
+        private long usage = 0;
+        private readonly object myLock = 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.myLock)
+            {
+                while(this.IsFull())
+                {
+                    if( !Monitor.Wait(this.myLock, 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.myLock)
+            {
+                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.myLock)
+            {
+                if( value > this.Usage )                    
+                {
+                    this.Usage = 0;
+                }
+                else
+                {
+                    this.Usage -= value;
+                }
+                
+                Monitor.PulseAll(this.myLock);
+            }
+        }
+        
+        public bool IsFull() 
+        {
+            bool result = false;
+            
+            lock(this.myLock) {
+                result = this.Usage >= this.Limit;
+            }
+        
+            return result;        
+        }
+        
+    }
+}

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

Added: activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/MemoryUsageTest.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/MemoryUsageTest.cs?rev=817390&view=auto
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/MemoryUsageTest.cs (added)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/test/csharp/MemoryUsageTest.cs Mon Sep 21
20:05:45 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.Util;
+using NUnit.Framework;
+using NUnit.Framework.Extensions;
+
+namespace Apache.NMS.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(() =>
+            {
+                Thread.Sleep( 100 );
+                usage.DecreaseUsage( usage.Usage );
+            });
+
+            thread1.Start();
+        
+            usage.WaitForSpace();
+            Assert.That( usage.Usage == 0 );
+        
+            thread1.Join();
+        
+        }        
+	}
+}

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



Mime
View raw message