You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directmemory.apache.org by no...@apache.org on 2012/10/28 20:16:34 UTC

svn commit: r1403079 - in /directmemory/trunk/directmemory-cache/src: main/java/org/apache/directmemory/cache/ main/java/org/apache/directmemory/memory/ test/java/org/apache/directmemory/cache/ test/java/org/apache/directmemory/memory/ test/java/org/ap...

Author: noctarius
Date: Sun Oct 28 19:16:33 2012
New Revision: 1403079

URL: http://svn.apache.org/viewvc?rev=1403079&view=rev
Log:
Added closing the CacheService / MemoryManager / Allocator to different unittests

Modified:
    directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
    directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java
    directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java

Modified: directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java (original)
+++ directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java Sun Oct 28 19:16:33 2012
@@ -19,6 +19,7 @@ package org.apache.directmemory.cache;
  * under the License.
  */
 
+import java.io.IOException;
 
 import org.apache.directmemory.DirectMemory;
 import org.apache.directmemory.memory.MemoryManagerService;
@@ -51,10 +52,9 @@ public class Cache
     public static void init( int numberOfBuffers, int size, int initialCapacity, int concurrencyLevel )
     {
         cacheService =
-            builder.setNumberOfBuffers( numberOfBuffers ).setInitialCapacity( initialCapacity )
-                .setConcurrencyLevel(concurrencyLevel )
-                    .setSize( size ).newCacheService();
-//        concurrencyLevel ).setMemoryManager( new UnsafeMemoryManagerServiceImpl<Object>() ).setSize( size ).newCacheService();
+            builder.setNumberOfBuffers( numberOfBuffers ).setInitialCapacity( initialCapacity ).setConcurrencyLevel( concurrencyLevel ).setSize( size ).newCacheService();
+        // concurrencyLevel ).setMemoryManager( new UnsafeMemoryManagerServiceImpl<Object>() ).setSize( size
+        // ).newCacheService();
     }
 
     public static void init( int numberOfBuffers, int size )
@@ -122,12 +122,17 @@ public class Cache
         cacheService.collectAll();
     }
 
-
     public static void clear()
     {
         cacheService.clear();
     }
 
+    public static void close()
+        throws IOException
+    {
+        cacheService.close();
+    }
+
     public static long entries()
     {
         return cacheService.entries();

Modified: directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java (original)
+++ directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java Sun Oct 28 19:16:33 2012
@@ -1,5 +1,7 @@
 package org.apache.directmemory.memory;
 
+import java.io.IOException;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -25,7 +27,7 @@ public class MemoryManager
 
     private MemoryManager()
     {
-        //static class
+        // static class
     }
 
     public static void init( int numberOfBuffers, int size )
@@ -63,6 +65,12 @@ public class MemoryManager
         memoryManager.clear();
     }
 
+    public static void close()
+        throws IOException
+    {
+        memoryManager.close();
+    }
+
     public static long capacity()
     {
         return memoryManager.capacity();
@@ -85,6 +93,6 @@ public class MemoryManager
 
     public static Pointer<Object> allocate( int size )
     {
-        return memoryManager.allocate( Object.class, size, -1, -1 ); //add a version with expiration
+        return memoryManager.allocate( Object.class, size, -1, -1 ); // add a version with expiration
     }
 }

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java Sun Oct 28 19:16:33 2012
@@ -21,58 +21,57 @@ package org.apache.directmemory.cache;
 
 import static org.junit.Assert.*;
 
+import java.io.IOException;
+
 import org.apache.directmemory.DirectMemory;
 import org.apache.directmemory.memory.Pointer;
 import org.apache.directmemory.memory.UnsafeMemoryManagerServiceImpl;
 import org.junit.Test;
 
-public class BasicTest 
+public class BasicTest
 {
-	@Test
-	public void putRetrieveAndUpdate() 
-	{
-		CacheService<String, Long> cache = new DirectMemory<String, Long>()
-	            .setNumberOfBuffers(10)
-	            .setSize(1000)
-	            .setInitialCapacity(10000)
-	            .setConcurrencyLevel(4)
-	            .newCacheService();
-
-	        assertNull(cache.retrieve("a"));
-	        assertNotNull(cache.put("a", 3L));
-	        assertNotNull(cache.retrieve("a"));
-	        assertEquals(3L, cache.retrieve("a").longValue());
-	        
-	        Pointer<Long> ptr = cache.put("a", 5L);
-	        assertNotNull(ptr);
-	        assertFalse(ptr.isExpired());
-	        assertFalse(ptr.isFree());
-	        assertNotNull("pointer should not be null", cache.retrieve("a"));
-	        assertEquals(5L, cache.retrieve("a").longValue());
-	}
-	
-	@Test
-    public void putRetrieveAndUpdateWithUnsafe() 
+    @Test
+    public void putRetrieveAndUpdate()
+        throws IOException
+    {
+        CacheService<String, Long> cache =
+            new DirectMemory<String, Long>().setNumberOfBuffers( 10 ).setSize( 1000 ).setInitialCapacity( 10000 ).setConcurrencyLevel( 4 ).newCacheService();
+
+        assertNull( cache.retrieve( "a" ) );
+        assertNotNull( cache.put( "a", 3L ) );
+        assertNotNull( cache.retrieve( "a" ) );
+        assertEquals( 3L, cache.retrieve( "a" ).longValue() );
+
+        Pointer<Long> ptr = cache.put( "a", 5L );
+        assertNotNull( ptr );
+        assertFalse( ptr.isExpired() );
+        assertFalse( ptr.isFree() );
+        assertNotNull( "pointer should not be null", cache.retrieve( "a" ) );
+        assertEquals( 5L, cache.retrieve( "a" ).longValue() );
+
+        cache.close();
+    }
+
+    @Test
+    public void putRetrieveAndUpdateWithUnsafe()
+        throws IOException
     {
-        CacheService<String, Long> cache = new DirectMemory<String, Long>()
-                .setNumberOfBuffers(10)
-                .setSize(1000)
-                .setInitialCapacity(10000)
-                .setConcurrencyLevel(4)
-                .setMemoryManager( new UnsafeMemoryManagerServiceImpl<Long>() )
-                .newCacheService();
-
-            assertNull(cache.retrieve("a"));
-            assertNotNull(cache.put("a", 3L));
-            assertNotNull(cache.retrieve("a"));
-            assertEquals(3L, cache.retrieve("a").longValue());
-            
-            Pointer<Long> ptr = cache.put("a", 5L);
-            assertNotNull(ptr);
-            assertFalse(ptr.isExpired());
-            assertFalse(ptr.isFree());
-            assertNotNull("pointer should not be null", cache.retrieve("a"));
-            assertEquals(5L, cache.retrieve("a").longValue());
+        CacheService<String, Long> cache =
+            new DirectMemory<String, Long>().setNumberOfBuffers( 10 ).setSize( 1000 ).setInitialCapacity( 10000 ).setConcurrencyLevel( 4 ).setMemoryManager( new UnsafeMemoryManagerServiceImpl<Long>() ).newCacheService();
+
+        assertNull( cache.retrieve( "a" ) );
+        assertNotNull( cache.put( "a", 3L ) );
+        assertNotNull( cache.retrieve( "a" ) );
+        assertEquals( 3L, cache.retrieve( "a" ).longValue() );
+
+        Pointer<Long> ptr = cache.put( "a", 5L );
+        assertNotNull( ptr );
+        assertFalse( ptr.isExpired() );
+        assertFalse( ptr.isFree() );
+        assertNotNull( "pointer should not be null", cache.retrieve( "a" ) );
+        assertEquals( 5L, cache.retrieve( "a" ).longValue() );
+
+        cache.close();
     }
 
 }

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java Sun Oct 28 19:16:33 2012
@@ -28,6 +28,7 @@ import org.apache.directmemory.memory.Po
 import org.apache.directmemory.memory.RoundRobinAllocationPolicy;
 import org.junit.Test;
 
+import java.io.IOException;
 import java.io.Serializable;
 
 import static org.junit.Assert.*;
@@ -37,12 +38,12 @@ public class CacheServiceImplTest
 
     @Test
     public void testOffHeapExceedMemoryReturnNullWhenTrue()
+        throws IOException
     {
         AllocationPolicy allocationPolicy = new RoundRobinAllocationPolicy();
         MemoryManagerService<byte[]> memoryManager = new MemoryManagerServiceImpl<byte[]>( allocationPolicy, true );
         CacheService<Integer, byte[]> cache =
-            new DirectMemory<Integer, byte[]>().setMemoryManager( memoryManager ).setNumberOfBuffers( 1 ).setSize(
-                Ram.Mb( 1 ) ).newCacheService();
+            new DirectMemory<Integer, byte[]>().setMemoryManager( memoryManager ).setNumberOfBuffers( 1 ).setSize( Ram.Mb( 1 ) ).newCacheService();
 
         for ( int i = 0; i < 1000; i++ )
         {
@@ -54,6 +55,7 @@ public class CacheServiceImplTest
         }
         assertTrue( "This test ensures that no unexpected errors/behaviours occurs when heap space is full", true );
 
+        cache.close();
     }
 
     private static class MyBean
@@ -83,13 +85,12 @@ public class CacheServiceImplTest
 
     @Test
     public void testEntryIsNoMoreAvailableAfterExpiry()
-        throws InterruptedException
+        throws InterruptedException, IOException
     {
         AllocationPolicy allocationPolicy = new RoundRobinAllocationPolicy();
         MemoryManagerService<MyBean> memoryManager = new MemoryManagerServiceImpl<MyBean>( allocationPolicy, true );
         CacheService<Integer, MyBean> cache =
-            new DirectMemory<Integer, MyBean>().setMemoryManager( memoryManager ).setNumberOfBuffers( 1 ).setSize(
-                Ram.Mb( 1 ) ).newCacheService();
+            new DirectMemory<Integer, MyBean>().setMemoryManager( memoryManager ).setNumberOfBuffers( 1 ).setSize( Ram.Mb( 1 ) ).newCacheService();
         /*
          * let the scan run every 10s
          */
@@ -120,7 +121,8 @@ public class CacheServiceImplTest
         assertNotNull( pointer );
         assertTrue( pointer.isExpired() );
         assertTrue( pointer.isFree() );
-    }
 
+        cache.close();
+    }
 
 }

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java Sun Oct 28 19:16:33 2012
@@ -32,6 +32,7 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.util.Random;
 
 import static org.junit.Assert.assertEquals;
@@ -55,9 +56,11 @@ public class TestCachePlusSerialization
 
     @AfterClass
     public static void end()
+        throws IOException
     {
         Cache.dump();
         Monitor.dump();
+        Cache.close();
         logger.info( "test ended" );
     }
 

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java Sun Oct 28 19:16:33 2012
@@ -22,12 +22,14 @@ package org.apache.directmemory.memory;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.io.IOException;
 import java.util.Map;
 import java.util.Random;
 import java.util.zip.CRC32;
 import java.util.zip.Checksum;
 
 import org.apache.directmemory.measures.Ram;
+import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -54,6 +56,16 @@ public class BaseTest
         mem.init( 1, 1 * 1024 * 1024 );
     }
 
+    @After
+    public void cleanup()
+        throws IOException
+    {
+        if ( mem != null )
+        {
+            mem.close();
+        }
+    }
+
     @Test
     public void smokeTest()
     {

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java Sun Oct 28 19:16:33 2012
@@ -22,12 +22,14 @@ package org.apache.directmemory.memory;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.io.IOException;
 import java.util.Map;
 import java.util.Random;
 import java.util.zip.CRC32;
 import java.util.zip.Checksum;
 
 import org.apache.directmemory.measures.Ram;
+import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Before;
 import org.junit.BeforeClass;
@@ -51,6 +53,16 @@ public class BaseUnsafeTest
         mem.init( 1, 1 * 1024 * 1024 );
     }
 
+    @After
+    public void cleanup()
+        throws IOException
+    {
+        if ( mem != null )
+        {
+            mem.close();
+        }
+    }
+
     @Test
     public void smokeTest()
     {

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java Sun Oct 28 19:16:33 2012
@@ -35,6 +35,7 @@ import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.util.Random;
 import java.util.concurrent.ConcurrentMap;
 
@@ -42,11 +43,10 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertNotNull;
 
 @AxisRange( min = 0, max = 1 )
-@BenchmarkMethodChart()
+@BenchmarkMethodChart( )
 @BenchmarkOptions( benchmarkRounds = 1, warmupRounds = 0 )
 @BenchmarkHistoryChart( labelWith = LabelType.CUSTOM_KEY, maxRuns = 5 )
 @Ignore
-
 public class MallocTest
     extends AbstractBenchmark
 {
@@ -57,6 +57,7 @@ public class MallocTest
 
     @After
     public void dump()
+        throws IOException
     {
         logger.info( "off-heap allocated: " + Ram.inMb( mem.capacity() ) );
         logger.info( "off-heap used:      " + Ram.inMb( mem.used() ) );
@@ -64,10 +65,15 @@ public class MallocTest
         logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
         logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
         logger.info( "************************************************" );
+
+        if ( mem != null )
+        {
+            mem.close();
+        }
     }
 
     MemoryManagerService<Object> mem;
-    
+
     @Before
     public void initMMS()
     {
@@ -80,7 +86,7 @@ public class MallocTest
     {
         assertNotNull( mem );
         int howMany = 1000000;
-        int size = (int)mem.capacity() / ( howMany );
+        int size = (int) mem.capacity() / ( howMany );
         size -= size / 100 * 1;
         logger.info( "payload size=" + size );
         logger.info( "entries=" + howMany );
@@ -104,7 +110,7 @@ public class MallocTest
 
         assertNotNull( mem );
         int howMany = 2000000;
-        int size = (int)mem.capacity() / ( howMany );
+        int size = (int) mem.capacity() / ( howMany );
         size -= size / 100 * 1;
         logger.info( "payload size=" + size );
         logger.info( "entries=" + howMany );
@@ -127,7 +133,7 @@ public class MallocTest
 
         assertNotNull( mem );
         int howMany = 5000000;
-        int size = (int)mem.capacity() / ( howMany );
+        int size = (int) mem.capacity() / ( howMany );
         size -= size / 100 * 1;
         logger.info( "payload size=" + size );
         logger.info( "entries=" + howMany );
@@ -144,12 +150,12 @@ public class MallocTest
         logger.info( "...done in " + ( System.currentTimeMillis() - start ) + " msecs." );
     }
 
-
     @Test
     public void withMap()
     {
 
-        ConcurrentMap<Long, Pointer<Object>> map = new MapMaker().concurrencyLevel( 4 ).initialCapacity( 500000 ).makeMap();
+        ConcurrentMap<Long, Pointer<Object>> map =
+            new MapMaker().concurrencyLevel( 4 ).initialCapacity( 500000 ).makeMap();
 
         String str = "This is the string to store into the off-heap memory";
 
@@ -173,7 +179,6 @@ public class MallocTest
         mem.clear();
     }
 
-
     @Test
     public void oneMillionEntriesWithRead()
     {

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java Sun Oct 28 19:16:33 2012
@@ -22,6 +22,7 @@ package org.apache.directmemory.memory;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
+import java.io.IOException;
 import java.util.Random;
 import java.util.concurrent.ConcurrentMap;
 
@@ -43,6 +44,7 @@ public class MallocWithUnsafeTest
 
     @After
     public void dump()
+        throws IOException
     {
         logger.info( "off-heap allocated: " + Ram.inMb( mem.capacity() ) );
         logger.info( "off-heap used:      " + Ram.inMb( mem.used() ) );
@@ -50,6 +52,11 @@ public class MallocWithUnsafeTest
         logger.info( "heap - allocated: " + Ram.inMb( Runtime.getRuntime().totalMemory() ) );
         logger.info( "heap - free : " + Ram.inMb( Runtime.getRuntime().freeMemory() ) );
         logger.info( "************************************************" );
+
+        if ( mem != null )
+        {
+            mem.close();
+        }
     }
 
     MemoryManagerService<Object> mem;

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java Sun Oct 28 19:16:33 2012
@@ -19,6 +19,7 @@ package org.apache.directmemory.memory;
  * under the License.
  */
 
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Random;
@@ -66,6 +67,7 @@ public class MemoryManagerServiceImplTes
 
     @Test
     public void testFirstMatchBorderCase()
+        throws IOException
     {
 
         // Storing a first payload of 4 bytes, 1 byte remaining in the buffer.
@@ -84,10 +86,12 @@ public class MemoryManagerServiceImplTes
         Pointer<Object> pointer2 = memoryManagerService.store( SMALL_PAYLOAD );
         Assert.assertNull( pointer2 );
 
+        memoryManagerService.close();
     }
 
     @Test
     public void testAllocateMultipleBuffers()
+        throws IOException
     {
 
         // Initializing 4 buffers of 4 bytes, MemoryManagerService should search
@@ -107,10 +111,13 @@ public class MemoryManagerServiceImplTes
 
         Pointer<Object> pointerNull = memoryManagerService.store( SMALL_PAYLOAD );
         Assert.assertNull( pointerNull );
+
+        memoryManagerService.close();
     }
 
     @Test
     public void testByteLeaking()
+        throws IOException
     {
 
         // Initializing 1 buffer of 10*4 bytes, should be able to allocate 10
@@ -129,10 +136,13 @@ public class MemoryManagerServiceImplTes
 
         Pointer<Object> pointerNull = memoryManagerService.store( SMALL_PAYLOAD );
         Assert.assertNull( pointerNull );
+
+        memoryManagerService.close();
     }
 
     @Test
     public void testReportCorrectUsedMemory()
+        throws IOException
     {
 
         // Initializing 1 buffer of 4*4 bytes, storing and freeing and storing
@@ -165,10 +175,12 @@ public class MemoryManagerServiceImplTes
         // Buffer again fully used.
         Assert.assertEquals( BUFFER_SIZE, memoryManagerService.used() );
 
+        memoryManagerService.close();
     }
 
     @Test
     public void testRandomPayload()
+        throws IOException
     {
 
         final int NUMBER_OF_OBJECTS = 10;
@@ -221,6 +233,7 @@ public class MemoryManagerServiceImplTes
         }
         while ( pointer != null );
 
+        memoryManagerService.close();
     }
 
 }

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java Sun Oct 28 19:16:33 2012
@@ -19,27 +19,27 @@ package org.apache.directmemory.memory;
  * under the License.
  */
 
-import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
-import com.google.common.collect.Maps;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.IOException;
+import java.util.Map;
+import java.util.Random;
+
 import org.apache.directmemory.measures.Ram;
-import org.apache.directmemory.memory.MemoryManager;
-import org.apache.directmemory.memory.Pointer;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Map;
-import java.util.Random;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import com.carrotsearch.junitbenchmarks.AbstractBenchmark;
+import com.google.common.collect.Maps;
 
 public class MemoryManagerTest
     extends AbstractBenchmark
 {
 
-
     @BeforeClass
     public static void init()
     {
@@ -47,6 +47,13 @@ public class MemoryManagerTest
         MemoryManager.init( 1, Ram.Mb( 100 ) );
     }
 
+    @AfterClass
+    public static void cleanup()
+        throws IOException
+    {
+        MemoryManager.close();
+    }
+
     @Test
     public void smokeTest()
     {
@@ -56,7 +63,7 @@ public class MemoryManagerTest
         Pointer<Object> p = MemoryManager.store( new byte[size] );
         logger.info( "stored" );
         assertNotNull( p );
-        //assertEquals(size,p.end);
+        // assertEquals(size,p.end);
         assertEquals( size, p.getCapacity() );
         assertEquals( size, MemoryManager.getMemoryManager().used() );
         MemoryManager.free( p );
@@ -83,7 +90,6 @@ public class MemoryManagerTest
         logger.info( "" + howMany + " items stored" );
     }
 
-
     @Test
     public void readTest()
     {
@@ -98,7 +104,6 @@ public class MemoryManagerTest
         }
     }
 
-
     private static Logger logger = LoggerFactory.getLogger( MallocTest.class );
 
     final static Map<String, Byte> test = Maps.newHashMap();

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java Sun Oct 28 19:16:33 2012
@@ -21,12 +21,14 @@ package org.apache.directmemory.memory;
 
 import org.apache.directmemory.measures.Ram;
 import org.apache.directmemory.memory.buffer.MemoryBuffer;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.util.Random;
 
@@ -59,6 +61,13 @@ public class NIOTest
         logger.info( "" + howMany + " items stored" );
     }
 
+    @AfterClass
+    public static void cleanup()
+        throws IOException
+    {
+        MemoryManager.close();
+    }
+
     @Test
     public void nioTest()
     {
@@ -83,5 +92,4 @@ public class NIOTest
         logger.info( "end" );
     }
 
-
 }

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java Sun Oct 28 19:16:33 2012
@@ -24,20 +24,19 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertNull;
 
 import java.io.IOException;
-import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.directmemory.memory.allocator.Allocator;
 import org.apache.directmemory.memory.buffer.MemoryBuffer;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
 /**
  * Unit test of {@link RoundRobinAllocationPolicy} class.
- *
+ * 
  * @author benoit@noisette.ch
- *
  */
 public class RoundRobinAllocationPolicyTest
 {
@@ -63,6 +62,16 @@ public class RoundRobinAllocationPolicyT
         allocationPolicy.init( allocators );
     }
 
+    @After
+    public void cleanup()
+        throws IOException
+    {
+        for ( Allocator allocator : allocators )
+        {
+            allocator.close();
+        }
+    }
+
     @Test
     public void testSequence()
     {
@@ -93,7 +102,6 @@ public class RoundRobinAllocationPolicyT
 
     }
 
-
     @Test
     public void testMaxAllocation()
     {
@@ -106,7 +114,6 @@ public class RoundRobinAllocationPolicyT
 
     }
 
-
     /**
      * Dummy {@link OffHeapMemoryBuffer} that do nothing.
      */
@@ -116,7 +123,7 @@ public class RoundRobinAllocationPolicyT
 
         @Override
         public void free( MemoryBuffer buffer )
-        {            
+        {
         }
 
         @Override
@@ -127,7 +134,7 @@ public class RoundRobinAllocationPolicyT
 
         @Override
         public void clear()
-        {            
+        {
         }
 
         @Override
@@ -146,8 +153,8 @@ public class RoundRobinAllocationPolicyT
         public void close()
             throws IOException
         {
-            
+
         }
-        
+
     }
 }

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java Sun Oct 28 19:16:33 2012
@@ -19,6 +19,8 @@ package org.apache.directmemory.memory.a
  * under the License.
  */
 
+import java.io.IOException;
+
 import junit.framework.Assert;
 
 import org.apache.directmemory.memory.buffer.MemoryBuffer;
@@ -28,102 +30,106 @@ public class FixedSizeByteBufferAllocato
 {
     @Test
     public void allocationTest()
+        throws IOException
     {
-        
+
         Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 5000, 256, 1 );
-        
+
         MemoryBuffer bf1 = allocator.allocate( 250 );
         Assert.assertEquals( 256, bf1.maxCapacity() );
         Assert.assertEquals( 250, bf1.capacity() );
-        
+
         MemoryBuffer bf2 = allocator.allocate( 251 );
         Assert.assertEquals( 256, bf2.maxCapacity() );
         Assert.assertEquals( 251, bf2.capacity() );
-        
+
         MemoryBuffer bf3 = allocator.allocate( 200 );
         Assert.assertEquals( 256, bf3.maxCapacity() );
         Assert.assertEquals( 200, bf3.capacity() );
-        
+
         MemoryBuffer bf4 = allocator.allocate( 2000 );
         Assert.assertNull( bf4 );
-        
+
         MemoryBuffer bf5 = allocator.allocate( 298 );
         Assert.assertNull( bf5 );
-        
+
         MemoryBuffer bf6 = allocator.allocate( 128 );
         Assert.assertEquals( 256, bf6.maxCapacity() );
         Assert.assertEquals( 128, bf6.capacity() );
-        
+
+        allocator.close();
     }
-    
-    
+
     @Test
     public void releaseTest()
+        throws IOException
     {
-        
+
         Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 1000, 256, 1 );
-        
+
         MemoryBuffer bf1 = allocator.allocate( 250 );
         Assert.assertEquals( 256, bf1.maxCapacity() );
         Assert.assertEquals( 250, bf1.capacity() );
-        
+
         MemoryBuffer bf2 = allocator.allocate( 251 );
         Assert.assertEquals( 256, bf2.maxCapacity() );
         Assert.assertEquals( 251, bf2.capacity() );
-        
+
         MemoryBuffer bf3 = allocator.allocate( 252 );
         Assert.assertEquals( 256, bf3.maxCapacity() );
         Assert.assertEquals( 252, bf3.capacity() );
-        
+
         MemoryBuffer bf4 = allocator.allocate( 500 );
         Assert.assertNull( bf4 );
-        
+
         allocator.free( bf1 );
         allocator.free( bf2 );
-        
+
         MemoryBuffer bf5 = allocator.allocate( 500 );
         Assert.assertNull( bf5 );
-        
+
         MemoryBuffer bf6 = allocator.allocate( 249 );
         Assert.assertEquals( 256, bf6.maxCapacity() );
         Assert.assertEquals( 249, bf6.capacity() );
-        
+
         MemoryBuffer bf7 = allocator.allocate( 248 );
         Assert.assertEquals( 256, bf7.maxCapacity() );
         Assert.assertEquals( 248, bf7.capacity() );
-        
+
+        allocator.close();
     }
-    
+
     @Test
     public void allocateAndFreeTest()
+        throws IOException
     {
-        
+
         Allocator allocator = new FixedSizeByteBufferAllocatorImpl( 0, 1000, 256, 1 );
-        
-        for (int i = 0; i < 1000; i++)
+
+        for ( int i = 0; i < 1000; i++ )
         {
             MemoryBuffer bf1 = allocator.allocate( 250 );
             Assert.assertEquals( 256, bf1.maxCapacity() );
             Assert.assertEquals( 250, bf1.capacity() );
-            
+
             allocator.free( bf1 );
         }
-        
-        
+
         MemoryBuffer bf2 = allocator.allocate( 1000 );
         Assert.assertNull( bf2 );
-        
-        for (int i = 0; i < 3; i++)
+
+        for ( int i = 0; i < 3; i++ )
         {
             MemoryBuffer bf3 = allocator.allocate( 250 );
             Assert.assertEquals( 256, bf3.maxCapacity() );
             Assert.assertEquals( 250, bf3.capacity() );
-            
+
         }
-        
+
         MemoryBuffer bf4 = allocator.allocate( 238 );
         Assert.assertNull( bf4 );
-        
+
+        allocator.close();
     }
-    
+
 }

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java Sun Oct 28 19:16:33 2012
@@ -19,6 +19,7 @@ package org.apache.directmemory.memory.a
  * under the License.
  */
 
+import java.io.IOException;
 import java.nio.BufferOverflowException;
 
 import junit.framework.Assert;
@@ -30,159 +31,172 @@ public class MergingByteBufferAllocatorI
 {
     @Test
     public void allocationTest()
+        throws IOException
     {
-        
+
         Allocator allocator = new MergingByteBufferAllocatorImpl( 0, 5000 );
-        
+
         MemoryBuffer bf1 = allocator.allocate( 250 );
         Assert.assertEquals( 250, bf1.maxCapacity() );
         Assert.assertEquals( 250, bf1.capacity() );
-        
+
         MemoryBuffer bf2 = allocator.allocate( 251 );
         Assert.assertEquals( 251, bf2.maxCapacity() );
         Assert.assertEquals( 251, bf2.capacity() );
-        
+
         MemoryBuffer bf3 = allocator.allocate( 200 );
         Assert.assertEquals( 200, bf3.maxCapacity() );
         Assert.assertEquals( 200, bf3.capacity() );
-        
+
         MemoryBuffer bf4 = allocator.allocate( 2000 );
         Assert.assertEquals( 2000, bf4.maxCapacity() );
         Assert.assertEquals( 2000, bf4.capacity() );
-        
+
         MemoryBuffer bf5 = allocator.allocate( 2001 );
         Assert.assertEquals( 2001, bf5.maxCapacity() );
         Assert.assertEquals( 2001, bf5.capacity() );
-        
+
         MemoryBuffer bf6 = allocator.allocate( 298 );
         Assert.assertEquals( 298, bf6.maxCapacity() );
         Assert.assertEquals( 298, bf6.capacity() );
-        
+
         MemoryBuffer bf7 = allocator.allocate( 128 );
         Assert.assertNull( bf7 );
-        
+
+        allocator.close();
     }
-    
-    
+
     @Test
     public void releaseTest()
+        throws IOException
     {
-        
+
         Allocator allocator = new MergingByteBufferAllocatorImpl( 0, 1000 );
-        
+
         MemoryBuffer bf1 = allocator.allocate( 250 );
         Assert.assertEquals( 250, bf1.maxCapacity() );
         Assert.assertEquals( 250, bf1.capacity() );
-        
+
         MemoryBuffer bf2 = allocator.allocate( 251 );
         Assert.assertEquals( 251, bf2.maxCapacity() );
         Assert.assertEquals( 251, bf2.capacity() );
-        
+
         MemoryBuffer bf3 = allocator.allocate( 252 );
         Assert.assertEquals( 252, bf3.maxCapacity() );
         Assert.assertEquals( 252, bf3.capacity() );
-        
+
         MemoryBuffer bf4 = allocator.allocate( 500 );
         Assert.assertNull( bf4 );
-        
+
         allocator.free( bf1 );
         allocator.free( bf2 );
-        
+
         MemoryBuffer bf5 = allocator.allocate( 500 );
         Assert.assertEquals( 501, bf5.maxCapacity() );
         Assert.assertEquals( 500, bf5.capacity() );
-        
+
+        allocator.close();
     }
-    
+
     @Test
     public void allocateAndFreeTest()
+        throws IOException
     {
-        
+
         Allocator allocator = new MergingByteBufferAllocatorImpl( 0, 1000 );
-        
-        for (int i = 0; i < 1000; i++)
+
+        for ( int i = 0; i < 1000; i++ )
         {
             MemoryBuffer bf1 = allocator.allocate( 250 );
             Assert.assertEquals( 250, bf1.maxCapacity() );
             Assert.assertEquals( 250, bf1.capacity() );
-            
+
             allocator.free( bf1 );
         }
-        
-        
+
         MemoryBuffer bf2 = allocator.allocate( 1000 );
         Assert.assertEquals( 1000, bf2.maxCapacity() );
         Assert.assertEquals( 1000, bf2.capacity() );
-        
+
+        allocator.close();
     }
-    
+
     @Test
     public void allocationWithoutSplittingPointerTest()
+        throws IOException
     {
-        
+
         Allocator allocator = new MergingByteBufferAllocatorImpl( 0, 200 );
-        
+
         MemoryBuffer bf1 = allocator.allocate( 180 );
         Assert.assertEquals( 200, bf1.maxCapacity() );
         Assert.assertEquals( 180, bf1.capacity() );
-        
+
         MemoryBuffer bf2 = allocator.allocate( 5 );
         Assert.assertNull( bf2 );
-        
+
         allocator.free( bf1 );
-        
-        
+
         MemoryBuffer bf3 = allocator.allocate( 10 );
         Assert.assertEquals( 10, bf3.maxCapacity() );
         Assert.assertEquals( 10, bf3.capacity() );
-        
+
         MemoryBuffer bf4 = allocator.allocate( 20 );
         Assert.assertEquals( 20, bf4.maxCapacity() );
         Assert.assertEquals( 20, bf4.capacity() );
-        
+
         MemoryBuffer bf5 = allocator.allocate( 30 );
         Assert.assertEquals( 30, bf5.maxCapacity() );
         Assert.assertEquals( 30, bf5.capacity() );
-        
+
         allocator.free( bf4 );
         allocator.free( bf3 );
-        
+
         MemoryBuffer bf6 = allocator.allocate( 25 );
         Assert.assertEquals( 30, bf6.maxCapacity() );
         Assert.assertEquals( 25, bf6.capacity() );
-        
+
+        allocator.close();
     }
-    
+
     @Test
     public void allocationWithDifferentRatioTest()
+        throws IOException
     {
-        
+
         MergingByteBufferAllocatorImpl allocator = new MergingByteBufferAllocatorImpl( 0, 200 );
         allocator.setSizeRatioThreshold( 0.95 );
-        
+
         allocator.setSizeRatioThreshold( 10 );
-        
+
         MemoryBuffer bf1 = allocator.allocate( 180 );
         Assert.assertEquals( 180, bf1.maxCapacity() );
         Assert.assertEquals( 180, bf1.capacity() );
-        
+
         MemoryBuffer bf2 = allocator.allocate( 10 );
         Assert.assertEquals( 20, bf2.maxCapacity() );
         Assert.assertEquals( 10, bf2.capacity() );
-        
+
+        allocator.close();
     }
-    
-    
-    @Test(expected = BufferOverflowException.class)
+
+    @Test( expected = BufferOverflowException.class )
     public void allocationThrowingBOExceptionTest()
+        throws IOException
     {
-        
+
         MergingByteBufferAllocatorImpl allocator = new MergingByteBufferAllocatorImpl( 0, 200 );
         allocator.setReturnNullWhenBufferIsFull( false );
-        
-        allocator.allocate( 210 );
-        Assert.fail();
+
+        try
+        {
+            allocator.allocate( 210 );
+            Assert.fail();
+        }
+        finally
+        {
+            allocator.close();
+        }
     }
-    
-    
+
 }

Modified: directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java
URL: http://svn.apache.org/viewvc/directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java?rev=1403079&r1=1403078&r2=1403079&view=diff
==============================================================================
--- directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java (original)
+++ directmemory/trunk/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java Sun Oct 28 19:16:33 2012
@@ -19,6 +19,7 @@ package org.apache.directmemory.memory.a
  * under the License.
  */
 
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -31,6 +32,7 @@ public class SlabByteBufferAllocatorImpl
 {
     @Test
     public void allocationTest()
+        throws IOException
     {
         
         List<FixedSizeByteBufferAllocatorImpl> slabs = new ArrayList<FixedSizeByteBufferAllocatorImpl>();
@@ -70,7 +72,8 @@ public class SlabByteBufferAllocatorImpl
         MemoryBuffer bf7 = allocator.allocate( 800 );
         Assert.assertEquals( 1024, bf7.maxCapacity() );
         Assert.assertEquals( 800, bf7.capacity() );
-        
+     
+        allocator.close();
     }
     
 }