GEODE-643: improve UnsafeMemoryChunk unit test coverage
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/f90f1391 Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/f90f1391 Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/f90f1391 Branch: refs/heads/feature/GEODE-217 Commit: f90f13917ec9a26bc067bb525d006c08374a864c Parents: 871266c Author: Darrel Schneider <dschnei...@pivotal.io> Authored: Tue Dec 8 14:49:48 2015 -0800 Committer: Darrel Schneider <dschnei...@pivotal.io> Committed: Fri Jan 8 15:08:56 2016 -0800 ---------------------------------------------------------------------- .../internal/offheap/OffHeapStorage.java | 3 -- .../internal/offheap/UnsafeMemoryChunk.java | 55 ------------------- .../offheap/MemoryChunkJUnitTestBase.java | 16 +++++- .../internal/offheap/MemoryChunkTestSuite.java | 32 +++++++++++ .../offheap/OffHeapStorageJUnitTest.java | 1 + .../offheap/UnsafeMemoryChunkJUnitTest.java | 56 ++++++++++++++++++++ 6 files changed, 104 insertions(+), 59 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f90f1391/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java index 3eb839b..be27b5e 100755 --- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java +++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/OffHeapStorage.java @@ -121,9 +121,6 @@ public class OffHeapStorage implements OffHeapMemoryStats { } public static long parseOffHeapMemorySize(String value) { - if (value == null || value.equals("")) { - return 0; - } final long parsed = parseLongWithUnits(value, 0L, 1024*1024); if (parsed < 0) { return 0; http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f90f1391/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunk.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunk.java b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunk.java index 4f0e86d..ed1c843 100644 --- a/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunk.java +++ b/gemfire-core/src/main/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunk.java @@ -65,9 +65,6 @@ public class UnsafeMemoryChunk implements MemoryChunk { } } - public static int getPageSize() { - return unsafe != null ? unsafe.getPageSize() : 8192; - } @Override public int getSize() { return (int)this.size; @@ -77,46 +74,6 @@ public class UnsafeMemoryChunk implements MemoryChunk { return this.data; } - /** - * Compare the first 'size' bytes at addr1 to those at addr2 and return true - * if they are all equal. - */ - public static boolean compareUnsafeBytes(long addr1, long addr2, int size) { - if ((size >= 8) && ((addr1 & 7) == 0) && ((addr2 & 7) == 0)) { - // We have 8 or more bytes to compare and the addresses are 8 byte aligned - do { - if (unsafe.getLong(null, addr1) != unsafe.getLong(null, addr2)) { - return false; - } - size -= 8; - addr1 += 8; - addr2 += 8; - } while (size >= 8); - } - while (size > 0) { - if (unsafe.getByte(addr1) != unsafe.getByte(addr2)) { - return false; - } - size -= 1; - addr1 += 1; - addr2 += 1; - } - return true; - } - - /** - * Like readAbsoluteByte but does no validation of addr - */ - public static byte readUnsafeByte(long addr) { - return unsafe.getByte(addr); - } - /** - * Reads from "addr" to "bytes". Number of bytes read/written is the length of the array. - */ - public static void readUnsafeBytes(long addr, byte[] bytes) { - unsafe.copyMemory(null, addr, bytes, ARRAY_BYTE_BASE_OFFSET, bytes.length); - } - public static byte readAbsoluteByte(long addr) { return unsafe.getByte(addr); } @@ -172,23 +129,11 @@ public class UnsafeMemoryChunk implements MemoryChunk { writeAbsoluteByte(this.data+offset, value); } - public static void readAbsoluteBytes(long addr, byte[] bytes) { - readAbsoluteBytes(addr, bytes, 0, bytes.length); - } - @Override public void readBytes(int offset, byte[] bytes) { readBytes(offset, bytes, 0, bytes.length); } - public static void writeAbsoluteBytes(long addr, byte[] bytes) { - writeAbsoluteBytes(addr, bytes, 0, bytes.length); - } - - public static void clearAbsolute(long addr, long size) { - unsafe.setMemory(addr, size, (byte) 0); - } - @Override public void writeBytes(int offset, byte[] bytes) { writeBytes(offset, bytes, 0, bytes.length); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f90f1391/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkJUnitTestBase.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkJUnitTestBase.java b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkJUnitTestBase.java index c8c0b2b..1b1d300 100644 --- a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkJUnitTestBase.java +++ b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkJUnitTestBase.java @@ -29,6 +29,9 @@ import org.junit.Test; import static org.junit.Assert.*; public abstract class MemoryChunkJUnitTestBase { + static { + ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true); + } protected abstract MemoryChunk createChunk(int size); @@ -70,7 +73,18 @@ public abstract class MemoryChunkJUnitTestBase { mc.release(); } } - + + @Test + public void testToString() { + int CHUNK_SIZE = 1024; + MemoryChunk mc = createChunk(CHUNK_SIZE); + try { + mc.toString(); + } finally { + mc.release(); + } + } + @Test public void testCopyBytes() { int CHUNK_SIZE = 1024; http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f90f1391/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkTestSuite.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkTestSuite.java b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkTestSuite.java new file mode 100644 index 0000000..2908126 --- /dev/null +++ b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/MemoryChunkTestSuite.java @@ -0,0 +1,32 @@ +/* + * 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. + */ + +package com.gemstone.gemfire.internal.offheap; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@Suite.SuiteClasses({ + ByteArrayMemoryChunkJUnitTest.class, + DirectByteBufferMemoryChunkJUnitTest.class, + HeapByteBufferMemoryChunkJUnitTest.class, + UnsafeMemoryChunkJUnitTest.class, +}) +@RunWith(Suite.class) +public class MemoryChunkTestSuite { + +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f90f1391/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java index de21487..24fb82b 100755 --- a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java +++ b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStorageJUnitTest.java @@ -98,6 +98,7 @@ public class OffHeapStorageJUnitTest { try { System.setProperty("gemfire.OFF_HEAP_SLAB_SIZE", "99"); assertEquals(99*1024*1024, OffHeapStorage.calcMaxSlabSize(100L*1024*1024)); + assertEquals(88, OffHeapStorage.calcMaxSlabSize(88)); System.setProperty("gemfire.OFF_HEAP_SLAB_SIZE", "88m"); assertEquals(88*1024*1024, OffHeapStorage.calcMaxSlabSize(100L*1024*1024)); System.setProperty("gemfire.OFF_HEAP_SLAB_SIZE", "77M"); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/f90f1391/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java ---------------------------------------------------------------------- diff --git a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java index cf6ba44..a98fa28 100644 --- a/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java +++ b/gemfire-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java @@ -16,6 +16,9 @@ */ package com.gemstone.gemfire.internal.offheap; +import static org.junit.Assert.*; + +import org.junit.Test; import org.junit.experimental.categories.Category; import com.gemstone.gemfire.test.junit.categories.UnitTest; @@ -28,4 +31,57 @@ public class UnsafeMemoryChunkJUnitTest extends MemoryChunkJUnitTestBase { return new UnsafeMemoryChunk(size); } + @Test + public void testGetAddress() { + MemoryChunk mc = createChunk(1024); + try { + UnsafeMemoryChunk umc = (UnsafeMemoryChunk) mc; + assertNotEquals(0, umc.getMemoryAddress()); + } finally { + mc.release(); + } + } + + @Test(expected=AssertionError.class) + public void readAbsoluteBytesFailsIfSizeLessThanZero() { + UnsafeMemoryChunk.readAbsoluteBytes(0L, null, 0, -1); + } + @Test + public void readAbsoluteBytesDoesNothingIfSizeIsZero() { + UnsafeMemoryChunk.readAbsoluteBytes(0L, new byte[0], 0, 0); + } + @Test(expected=AssertionError.class) + public void readAbsoluteBytesFailsIfSizeGreaterThanArrayLength() { + UnsafeMemoryChunk.readAbsoluteBytes(0L, new byte[0], 0, 1); + } + @Test(expected=AssertionError.class) + public void readAbsoluteBytesFailsIfByteOffsetNegative() { + UnsafeMemoryChunk.readAbsoluteBytes(0L, new byte[0], -1, 0); + } + @Test(expected=AssertionError.class) + public void readAbsoluteBytesFailsIfByteOffsetGreaterThanArrayLength() { + UnsafeMemoryChunk.readAbsoluteBytes(0L, new byte[0], 1, 0); + } + + @Test(expected=AssertionError.class) + public void writeAbsoluteBytesFailsIfSizeLessThanZero() { + UnsafeMemoryChunk.writeAbsoluteBytes(0L, null, 0, -1); + } + @Test + public void writeAbsoluteBytesDoesNothingIfSizeIsZero() { + UnsafeMemoryChunk.writeAbsoluteBytes(0L, new byte[0], 0, 0); + } + @Test(expected=AssertionError.class) + public void writeAbsoluteBytesFailsIfSizeGreaterThanArrayLength() { + UnsafeMemoryChunk.writeAbsoluteBytes(0L, new byte[0], 0, 1); + } + @Test(expected=AssertionError.class) + public void writeAbsoluteBytesFailsIfByteOffsetNegative() { + UnsafeMemoryChunk.writeAbsoluteBytes(0L, new byte[0], -1, 0); + } + @Test(expected=AssertionError.class) + public void writeAbsoluteBytesFailsIfByteOffsetGreaterThanArrayLength() { + UnsafeMemoryChunk.writeAbsoluteBytes(0L, new byte[0], 1, 0); + } + }