You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by sa...@apache.org on 2016/03/01 19:46:48 UTC
incubator-geode git commit: GEDOE-856: Unit tests for TinyMemoryBlock,
a nested class in FreeListManager.
Repository: incubator-geode
Updated Branches:
refs/heads/develop d067f41d5 -> 00b4c8428
GEDOE-856: Unit tests for TinyMemoryBlock, a nested class in
FreeListManager.
To give testing access to the class, the access modifier of
TinyMemoryBlock and it's constructor has been changed from private to protected.
this closes #108
Project: http://git-wip-us.apache.org/repos/asf/incubator-geode/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-geode/commit/00b4c842
Tree: http://git-wip-us.apache.org/repos/asf/incubator-geode/tree/00b4c842
Diff: http://git-wip-us.apache.org/repos/asf/incubator-geode/diff/00b4c842
Branch: refs/heads/develop
Commit: 00b4c842832318d133c816afb345c84f2180b809
Parents: d067f41
Author: Ken Howe <kh...@pivotal.io>
Authored: Fri Feb 26 10:04:30 2016 -0800
Committer: Sai Boorlagadda <sb...@pivotal.io>
Committed: Tue Mar 1 10:45:49 2016 -0800
----------------------------------------------------------------------
.../internal/offheap/FreeListManager.java | 4 +-
.../offheap/TinyMemoryBlockJUnitTest.java | 244 +++++++++++++++++++
2 files changed, 246 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/00b4c842/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
----------------------------------------------------------------------
diff --git a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
index a716f14..b816eb9 100644
--- a/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
+++ b/geode-core/src/main/java/com/gemstone/gemfire/internal/offheap/FreeListManager.java
@@ -779,11 +779,11 @@ public class FreeListManager {
/**
* Used to represent an address from a tiny free list as a MemoryBlock
*/
- private static final class TinyMemoryBlock implements MemoryBlock {
+ protected static final class TinyMemoryBlock implements MemoryBlock {
private final long address;
private final int freeListId;
- private TinyMemoryBlock(long address, int freeListId) {
+ protected TinyMemoryBlock(long address, int freeListId) {
this.address = address;
this.freeListId = freeListId;
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/00b4c842/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java
new file mode 100644
index 0000000..1626a15
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyMemoryBlockJUnitTest.java
@@ -0,0 +1,244 @@
+/*
+ * 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 static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+
+import java.nio.ByteBuffer;
+
+import org.assertj.core.api.JUnitSoftAssertions;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.ExpectedException;
+
+import com.gemstone.gemfire.internal.cache.EntryEventImpl;
+import com.gemstone.gemfire.internal.offheap.MemoryBlock.State;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class TinyMemoryBlockJUnitTest {
+
+ private SimpleMemoryAllocatorImpl ma;
+ private OutOfOffHeapMemoryListener ooohml;
+ private OffHeapMemoryStats stats;
+
+ private AddressableMemoryChunk[] slabs = {
+ new UnsafeMemoryChunk((int)OffHeapStorage.MIN_SLAB_SIZE),
+ new UnsafeMemoryChunk((int)OffHeapStorage.MIN_SLAB_SIZE),
+ new UnsafeMemoryChunk((int)OffHeapStorage.MIN_SLAB_SIZE)
+ };
+
+ private static class TestableFreeListManager extends FreeListManager {
+ TestableFreeListManager(SimpleMemoryAllocatorImpl ma, final AddressableMemoryChunk[] slabs) {
+ super (ma, slabs);
+ }
+ }
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Rule
+ public JUnitSoftAssertions softly = new JUnitSoftAssertions();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ ooohml = mock(OutOfOffHeapMemoryListener.class);
+ stats = mock(OffHeapMemoryStats.class);
+ ma = (SimpleMemoryAllocatorImpl) SimpleMemoryAllocatorImpl.createForUnitTest(ooohml, stats, slabs);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ }
+
+ protected Object getValue() {
+ return Long.valueOf(Long.MAX_VALUE);
+ }
+
+ private MemoryChunkWithRefCount createChunk(byte[] v, boolean isSerialized, boolean isCompressed) {
+ MemoryChunkWithRefCount chunk = (MemoryChunkWithRefCount) ma.allocateAndInitialize(v, isSerialized, isCompressed);
+ return chunk;
+ }
+
+ private MemoryChunkWithRefCount createValueAsSerializedStoredObject(Object value, boolean isCompressed) {
+ byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value);
+
+ boolean isSerialized = true;
+
+ MemoryChunkWithRefCount createdObject = createChunk(valueInSerializedByteArray, isSerialized, isCompressed);
+ return createdObject;
+ }
+
+ private byte[] convertValueToByteArray(Object value) {
+ return ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong((Long) value).array();
+ }
+
+ private MemoryChunkWithRefCount createValueAsUnserializedStoredObject(Object value, boolean isCompressed) {
+ byte[] valueInByteArray;
+ if (value instanceof Long) {
+ valueInByteArray = convertValueToByteArray(value);
+ } else {
+ valueInByteArray = (byte[]) value;
+ }
+
+ boolean isSerialized = false;
+
+ MemoryChunkWithRefCount createdObject = createChunk(valueInByteArray, isSerialized, isCompressed);
+ return createdObject;
+ }
+
+ @Test
+ public void constructorReturnsNonNullMemoryBlock() {
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(slabs[0].getMemoryAddress(), 0);
+ softly.assertThat(mb).isNotNull();
+ }
+
+ @Test
+ public void stateAlwaysEqualsDeallocated() {
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(slabs[0].getMemoryAddress(), 0);
+ softly.assertThat(mb.getState()).isEqualTo(State.DEALLOCATED);
+ }
+
+ @Test
+ public void getMemoryAddressReturnsAddressBlockWasContructedFrom() {
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(slabs[0].getMemoryAddress(), 0);
+ softly.assertThat(mb.getMemoryAddress()).isEqualTo(slabs[0].getMemoryAddress());
+ }
+
+ @Test
+ public void getBlockSizeReturnsReturnsSizeOfUnderlyingChunk() {
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).getMemoryAddress(), 0);
+ softly.assertThat(mb.getBlockSize()).isEqualTo(slabs[0].getSize());
+ }
+
+ @Test
+ public void getNextBlockThrowsUnsupportedOperationException() {
+ expectedException.expect(UnsupportedOperationException.class);
+
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).getMemoryAddress(), 0);
+ mb.getNextBlock();
+ fail("getNextBlock failed to throw UnsupportedOperationException");
+ }
+
+ @Test
+ public void getSlabIdThrowsUnsupportedOperationException() {
+ expectedException.expect(UnsupportedOperationException.class);
+
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).getMemoryAddress(), 0);
+ mb.getSlabId();
+ fail("getSlabId failed to throw UnsupportedOperationException");
+ }
+
+ @Test
+ public void getFreeListIdReturnsIdBlockWasConstructedWith() {
+ MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).getMemoryAddress(), 0);
+ MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[1].getMemoryAddress(), slabs[1].getSize()).getMemoryAddress(), 1);
+ softly.assertThat(mb0.getFreeListId()).isEqualTo(0);
+ softly.assertThat(mb1.getFreeListId()).isEqualTo(1);
+ }
+
+ @Test
+ public void getRefCountReturnsZero() {
+ MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).getMemoryAddress(), 0);
+ MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[1].getMemoryAddress(), slabs[1].getSize()).getMemoryAddress(), 1);
+ softly.assertThat(mb0.getRefCount()).isEqualTo(0);
+ softly.assertThat(mb1.getRefCount()).isEqualTo(0);
+ }
+
+ @Test
+ public void getDataTypeReturnsNA() {
+ Object obj = getValue();
+ boolean compressed = false;
+
+ MemoryChunkWithRefCount storedObject0 = createValueAsSerializedStoredObject(obj, compressed);
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getMemoryAddress(), 0);
+ softly.assertThat(mb.getDataType()).isEqualTo("N/A");
+ }
+
+ @Test
+ public void getDataValueReturnsNull() {
+ Object obj = getValue();
+ boolean compressed = false;
+
+ MemoryChunkWithRefCount storedObject0 = createValueAsSerializedStoredObject(obj, compressed);
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getMemoryAddress(), 0);
+ softly.assertThat(mb.getDataValue()).isNull();
+ }
+
+ @Test
+ public void isSerializedReturnsFalse() {
+ Object obj = getValue();
+ boolean compressed = false;
+
+ MemoryChunkWithRefCount storedObject0 = createValueAsSerializedStoredObject(obj, compressed);
+ MemoryChunkWithRefCount storedObject1 = createValueAsUnserializedStoredObject(obj, compressed);
+ MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getMemoryAddress(), 0);
+ MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject1).getMemoryAddress(), 0);
+ softly.assertThat(mb0.isSerialized()).isFalse();
+ softly.assertThat(mb1.isSerialized()).isFalse();
+ }
+
+ @Test
+ public void isCompressedReturnsFalse() {
+ Object obj = getValue();
+ boolean compressed = false;
+ MemoryChunkWithRefCount storedObject0 = createValueAsUnserializedStoredObject(obj, compressed);
+ MemoryChunkWithRefCount storedObject1 = createValueAsUnserializedStoredObject(obj, compressed = true);
+ MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getMemoryAddress(), 0);
+ MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject1).getMemoryAddress(), 0);
+ softly.assertThat(mb0.isCompressed()).isFalse();
+ softly.assertThat(mb1.isCompressed()).isFalse();
+ }
+
+ @Test
+ public void equalsComparesAddressesOfTinyMemoryBlocks() {
+ MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(slabs[0].getMemoryAddress(), 0);
+ MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(slabs[0].getMemoryAddress(), 0);
+ MemoryBlock mb2 = new TestableFreeListManager.TinyMemoryBlock(slabs[1].getMemoryAddress(), 0);
+ softly.assertThat(mb0.equals(mb1)).isTrue();
+ softly.assertThat(mb0.equals(mb2)).isFalse();
+ }
+
+ @Test
+ public void equalsNotTinyMemoryBlockReturnsFalse() {
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(slabs[0].getMemoryAddress(), 0);
+ softly.assertThat(mb.equals(slabs[0])).isFalse();
+ }
+
+ @Test
+ public void hashCodeReturnsHashOfUnderlyingMemory() {
+ MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(slabs[0].getMemoryAddress(), 0);
+ softly.assertThat(mb.hashCode()).isEqualTo(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).hashCode());
+ }
+
+}