Added closing the CacheService / MemoryManager / Allocator to different unittests
git-svn-id: https://svn.apache.org/repos/asf/directmemory/trunk@1403079 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java b/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
index aaf6326..288a124 100644
--- a/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
+++ b/directmemory-cache/src/main/java/org/apache/directmemory/cache/Cache.java
@@ -19,6 +19,7 @@
* under the License.
*/
+import java.io.IOException;
import org.apache.directmemory.DirectMemory;
import org.apache.directmemory.memory.MemoryManagerService;
@@ -51,10 +52,9 @@
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 @@
cacheService.collectAll();
}
-
public static void clear()
{
cacheService.clear();
}
+ public static void close()
+ throws IOException
+ {
+ cacheService.close();
+ }
+
public static long entries()
{
return cacheService.entries();
diff --git a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
index 90a30ef..c610b54 100644
--- a/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
+++ b/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManager.java
@@ -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 @@
private MemoryManager()
{
- //static class
+ // static class
}
public static void init( int numberOfBuffers, int size )
@@ -63,6 +65,12 @@
memoryManager.clear();
}
+ public static void close()
+ throws IOException
+ {
+ memoryManager.close();
+ }
+
public static long capacity()
{
return memoryManager.capacity();
@@ -85,6 +93,6 @@
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
}
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java
index abe4389..e840dc2 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/cache/BasicTest.java
@@ -21,58 +21,57 @@
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)
- .setMemoryManager( new UnsafeMemoryManagerServiceImpl<Long>() )
- .newCacheService();
+ 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());
+ 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() );
+
+ cache.close();
}
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
index 4667c7b..2f5e6e3 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/cache/CacheServiceImplTest.java
@@ -28,6 +28,7 @@
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 @@
@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 @@
}
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 @@
@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 @@
assertNotNull( pointer );
assertTrue( pointer.isExpired() );
assertTrue( pointer.isFree() );
- }
+ cache.close();
+ }
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java b/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java
index 5c4005a..ff33c83 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/cache/TestCachePlusSerialization.java
@@ -32,6 +32,7 @@
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 @@
@AfterClass
public static void end()
+ throws IOException
{
Cache.dump();
Monitor.dump();
+ Cache.close();
logger.info( "test ended" );
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java
index ed019cc..6cbba33 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseTest.java
@@ -22,12 +22,14 @@
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 @@
mem.init( 1, 1 * 1024 * 1024 );
}
+ @After
+ public void cleanup()
+ throws IOException
+ {
+ if ( mem != null )
+ {
+ mem.close();
+ }
+ }
+
@Test
public void smokeTest()
{
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java
index c3ae6a1..ab64473 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/BaseUnsafeTest.java
@@ -22,12 +22,14 @@
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 @@
mem.init( 1, 1 * 1024 * 1024 );
}
+ @After
+ public void cleanup()
+ throws IOException
+ {
+ if ( mem != null )
+ {
+ mem.close();
+ }
+ }
+
@Test
public void smokeTest()
{
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java
index d400013..e84da3a 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocTest.java
@@ -35,6 +35,7 @@
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.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 @@
@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 @@
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 @@
{
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 @@
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 @@
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 @@
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 @@
mem.clear();
}
-
@Test
public void oneMillionEntriesWithRead()
{
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java
index b70507c..89e338d 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MallocWithUnsafeTest.java
@@ -22,6 +22,7 @@
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 @@
@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 @@
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;
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java
index 09cdfca..e19bfb6 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerServiceImplTest.java
@@ -19,6 +19,7 @@
* under the License.
*/
+import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
@@ -66,6 +67,7 @@
@Test
public void testFirstMatchBorderCase()
+ throws IOException
{
// Storing a first payload of 4 bytes, 1 byte remaining in the buffer.
@@ -84,10 +86,12 @@
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 @@
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 @@
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 @@
// 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 @@
}
while ( pointer != null );
+ memoryManagerService.close();
}
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java
index 3dea6fe..e389de5 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/MemoryManagerTest.java
@@ -19,27 +19,27 @@
* 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 @@
MemoryManager.init( 1, Ram.Mb( 100 ) );
}
+ @AfterClass
+ public static void cleanup()
+ throws IOException
+ {
+ MemoryManager.close();
+ }
+
@Test
public void smokeTest()
{
@@ -56,7 +63,7 @@
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 @@
logger.info( "" + howMany + " items stored" );
}
-
@Test
public void readTest()
{
@@ -98,7 +104,6 @@
}
}
-
private static Logger logger = LoggerFactory.getLogger( MallocTest.class );
final static Map<String, Byte> test = Maps.newHashMap();
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java
index e3f10ce..dadfd91 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/NIOTest.java
@@ -21,12 +21,14 @@
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 @@
logger.info( "" + howMany + " items stored" );
}
+ @AfterClass
+ public static void cleanup()
+ throws IOException
+ {
+ MemoryManager.close();
+ }
+
@Test
public void nioTest()
{
@@ -83,5 +92,4 @@
logger.info( "end" );
}
-
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java
index ef97913..c714f81 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/RoundRobinAllocationPolicyTest.java
@@ -24,20 +24,19 @@
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 @@
allocationPolicy.init( allocators );
}
+ @After
+ public void cleanup()
+ throws IOException
+ {
+ for ( Allocator allocator : allocators )
+ {
+ allocator.close();
+ }
+ }
+
@Test
public void testSequence()
{
@@ -93,7 +102,6 @@
}
-
@Test
public void testMaxAllocation()
{
@@ -106,7 +114,6 @@
}
-
/**
* Dummy {@link OffHeapMemoryBuffer} that do nothing.
*/
@@ -116,7 +123,7 @@
@Override
public void free( MemoryBuffer buffer )
- {
+ {
}
@Override
@@ -127,7 +134,7 @@
@Override
public void clear()
- {
+ {
}
@Override
@@ -146,8 +153,8 @@
public void close()
throws IOException
{
-
+
}
-
+
}
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java
index 6c0b99d..5229749 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImplTest.java
@@ -19,6 +19,8 @@
* under the License.
*/
+import java.io.IOException;
+
import junit.framework.Assert;
import org.apache.directmemory.memory.buffer.MemoryBuffer;
@@ -28,102 +30,106 @@
{
@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();
}
-
+
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java
index 2a63b5f..f5a1a18 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImplTest.java
@@ -19,6 +19,7 @@
* under the License.
*/
+import java.io.IOException;
import java.nio.BufferOverflowException;
import junit.framework.Assert;
@@ -30,159 +31,172 @@
{
@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();
+ }
}
-
-
+
}
diff --git a/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java b/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java
index 666e004..3433730 100644
--- a/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java
+++ b/directmemory-cache/src/test/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImplTest.java
@@ -19,6 +19,7 @@
* under the License.
*/
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -31,6 +32,7 @@
{
@Test
public void allocationTest()
+ throws IOException
{
List<FixedSizeByteBufferAllocatorImpl> slabs = new ArrayList<FixedSizeByteBufferAllocatorImpl>();
@@ -70,7 +72,8 @@
MemoryBuffer bf7 = allocator.allocate( 800 );
Assert.assertEquals( 1024, bf7.maxCapacity() );
Assert.assertEquals( 800, bf7.capacity() );
-
+
+ allocator.close();
}
}