addded try with resources to tests

This commit is contained in:
Rob Austin
2018-11-16 18:31:11 +00:00
parent 95ed0c0049
commit 9edac73234
@@ -109,32 +109,33 @@ public class MemoryLeaksTest {
}
long nativeMemoryUsedBeforeMap = nativeMemoryUsed();
int serializersBeforeMap = serializerCount.get();
ChronicleMap<IntValue, String> map = getMap();
long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed();
assertEquals(expectedNativeMemory, nativeMemoryUsed());
tryCloseFromContext(map);
WeakReference<ChronicleMap<IntValue, String>> ref = new WeakReference<>(map);
Assert.assertNotNull(ref.get());
map = null;
try (ChronicleMap<IntValue, String> map = getMap()) {
long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed();
assertEquals(expectedNativeMemory, nativeMemoryUsed());
tryCloseFromContext(map);
WeakReference<ChronicleMap<IntValue, String>> ref = new WeakReference<>(map);
Assert.assertNotNull(ref.get());
// map = null;
// Wait until Map is collected by GC
while (ref.get() != null) {
System.gc();
byte[] garbage = new byte[10_000_000];
Thread.yield();
}
// Wait until Cleaner is called and memory is returned to the system
for (int i = 0; i < 6_000; i++) {
if (nativeMemoryUsedBeforeMap == nativeMemoryUsed() && // (*)
serializerCount.get() == serializersBeforeMap) {
break;
// Wait until Map is collected by GC
while (ref.get() != null) {
System.gc();
byte[] garbage = new byte[10_000_000];
Thread.yield();
}
System.gc();
byte[] garbage = new byte[10_000_000];
Thread.sleep(10);
// Wait until Cleaner is called and memory is returned to the system
for (int i = 0; i < 6_000; i++) {
if (nativeMemoryUsedBeforeMap == nativeMemoryUsed() && // (*)
serializerCount.get() == serializersBeforeMap) {
break;
}
System.gc();
byte[] garbage = new byte[10_000_000];
Thread.sleep(10);
}
Assert.assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed());
Assert.assertEquals(serializersBeforeMap, serializerCount.get());
}
Assert.assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed());
Assert.assertEquals(serializersBeforeMap, serializerCount.get());
}
private long nativeMemoryUsed() {
@@ -151,37 +152,39 @@ public class MemoryLeaksTest {
throws IOException, InterruptedException {
long nativeMemoryUsedBeforeMap = nativeMemoryUsed();
int serializersBeforeMap = serializerCount.get();
ChronicleMap<IntValue, String> map = getMap();
// One serializer should be copied to the map's valueReader field, another is copied from
// the map's valueReader field to the context
Assert.assertTrue(serializerCount.get() >= serializersBeforeMap + 2);
Assert.assertNotEquals(0, map.offHeapMemoryUsed());
try {
long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed();
assertEquals(String.format(
"used before map: %d, used by map: %d, expected used: %d, actual used: %d",
nativeMemoryUsedBeforeMap,
map.offHeapMemoryUsed(),
expectedNativeMemory, nativeMemoryUsed()),
expectedNativeMemory, nativeMemoryUsed());
} finally {
tryCloseFromContext(map);
map.close();
try (ChronicleMap<IntValue, String> map = getMap()) {
// One serializer should be copied to the map's valueReader field, another is copied from
// the map's valueReader field to the context
Assert.assertTrue(serializerCount.get() >= serializersBeforeMap + 2);
Assert.assertNotEquals(0, map.offHeapMemoryUsed());
try {
long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed();
assertEquals(String.format(
"used before map: %d, used by map: %d, expected used: %d, actual used: %d",
nativeMemoryUsedBeforeMap,
map.offHeapMemoryUsed(),
expectedNativeMemory, nativeMemoryUsed()),
expectedNativeMemory, nativeMemoryUsed());
} finally {
tryCloseFromContext(map);
map.close();
}
assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed());
// Wait until chronicle map context (hence serializers) is collected by the GC
for (int i = 0; i < 6_000; i++) {
if (serializerCount.get() == serializersBeforeMap)
break;
System.gc();
byte[] garbage = new byte[10_000_000];
Thread.sleep(10);
}
Assert.assertTrue(serializerCount.get() == serializersBeforeMap);
// This assertion ensures GC doesn't reclaim the map before or during the loop iteration
// above, to ensure that we test that the direct memory and contexts are released because
// of the manual map.close(), despite the "leak" of the map object itself.
Assert.assertEquals(0, map.offHeapMemoryUsed());
}
assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed());
// Wait until chronicle map context (hence serializers) is collected by the GC
for (int i = 0; i < 6_000; i++) {
if (serializerCount.get() == serializersBeforeMap)
break;
System.gc();
byte[] garbage = new byte[10_000_000];
Thread.sleep(10);
}
Assert.assertTrue(serializerCount.get() == serializersBeforeMap);
// This assertion ensures GC doesn't reclaim the map before or during the loop iteration
// above, to ensure that we test that the direct memory and contexts are released because
// of the manual map.close(), despite the "leak" of the map object itself.
Assert.assertEquals(0, map.offHeapMemoryUsed());
}
private ChronicleMap<IntValue, String> getMap() throws IOException {