Return-Path: X-Original-To: apmail-geode-commits-archive@minotaur.apache.org Delivered-To: apmail-geode-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 3882E19C18 for ; Tue, 15 Mar 2016 00:23:53 +0000 (UTC) Received: (qmail 14330 invoked by uid 500); 15 Mar 2016 00:23:53 -0000 Delivered-To: apmail-geode-commits-archive@geode.apache.org Received: (qmail 14285 invoked by uid 500); 15 Mar 2016 00:23:53 -0000 Mailing-List: contact commits-help@geode.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@geode.incubator.apache.org Delivered-To: mailing list commits@geode.incubator.apache.org Received: (qmail 14270 invoked by uid 99); 15 Mar 2016 00:23:53 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Mar 2016 00:23:53 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 918BFC182C for ; Tue, 15 Mar 2016 00:23:52 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -2.971 X-Spam-Level: X-Spam-Status: No, score=-2.971 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, KAM_LOTSOFHASH=0.25, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id Y13wKKCtXeS9 for ; Tue, 15 Mar 2016 00:23:49 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 5FD505FAE1 for ; Tue, 15 Mar 2016 00:23:45 +0000 (UTC) Received: (qmail 12501 invoked by uid 99); 15 Mar 2016 00:23:44 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Mar 2016 00:23:44 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2FF50E0260; Tue, 15 Mar 2016 00:23:44 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: klund@apache.org To: commits@geode.incubator.apache.org Date: Tue, 15 Mar 2016 00:23:48 -0000 Message-Id: <1a020b1434904c2ca574fe4248f1ae61@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [05/44] incubator-geode git commit: GEODE-982: refactor off-heap http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3087c86f/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 index 1626a15..d444865 100644 --- 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 @@ -43,14 +43,14 @@ public class TinyMemoryBlockJUnitTest { 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 Slab[] slabs = { + new SlabImpl((int)OffHeapStorage.MIN_SLAB_SIZE), + new SlabImpl((int)OffHeapStorage.MIN_SLAB_SIZE), + new SlabImpl((int)OffHeapStorage.MIN_SLAB_SIZE) }; private static class TestableFreeListManager extends FreeListManager { - TestableFreeListManager(SimpleMemoryAllocatorImpl ma, final AddressableMemoryChunk[] slabs) { + TestableFreeListManager(SimpleMemoryAllocatorImpl ma, final Slab[] slabs) { super (ma, slabs); } } @@ -85,17 +85,17 @@ public class TinyMemoryBlockJUnitTest { return Long.valueOf(Long.MAX_VALUE); } - private MemoryChunkWithRefCount createChunk(byte[] v, boolean isSerialized, boolean isCompressed) { - MemoryChunkWithRefCount chunk = (MemoryChunkWithRefCount) ma.allocateAndInitialize(v, isSerialized, isCompressed); + private StoredObject createChunk(byte[] v, boolean isSerialized, boolean isCompressed) { + StoredObject chunk = (StoredObject) ma.allocateAndInitialize(v, isSerialized, isCompressed); return chunk; } - private MemoryChunkWithRefCount createValueAsSerializedStoredObject(Object value, boolean isCompressed) { + private StoredObject createValueAsSerializedStoredObject(Object value, boolean isCompressed) { byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); boolean isSerialized = true; - MemoryChunkWithRefCount createdObject = createChunk(valueInSerializedByteArray, isSerialized, isCompressed); + StoredObject createdObject = createChunk(valueInSerializedByteArray, isSerialized, isCompressed); return createdObject; } @@ -103,7 +103,7 @@ public class TinyMemoryBlockJUnitTest { return ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong((Long) value).array(); } - private MemoryChunkWithRefCount createValueAsUnserializedStoredObject(Object value, boolean isCompressed) { + private StoredObject createValueAsUnserializedStoredObject(Object value, boolean isCompressed) { byte[] valueInByteArray; if (value instanceof Long) { valueInByteArray = convertValueToByteArray(value); @@ -113,7 +113,7 @@ public class TinyMemoryBlockJUnitTest { boolean isSerialized = false; - MemoryChunkWithRefCount createdObject = createChunk(valueInByteArray, isSerialized, isCompressed); + StoredObject createdObject = createChunk(valueInByteArray, isSerialized, isCompressed); return createdObject; } @@ -132,12 +132,12 @@ public class TinyMemoryBlockJUnitTest { @Test public void getMemoryAddressReturnsAddressBlockWasContructedFrom() { MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(slabs[0].getMemoryAddress(), 0); - softly.assertThat(mb.getMemoryAddress()).isEqualTo(slabs[0].getMemoryAddress()); + softly.assertThat(mb.getAddress()).isEqualTo(slabs[0].getMemoryAddress()); } @Test public void getBlockSizeReturnsReturnsSizeOfUnderlyingChunk() { - MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).getMemoryAddress(), 0); + MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new OffHeapStoredObject(slabs[0].getMemoryAddress(), slabs[0].getSize()).getAddress(), 0); softly.assertThat(mb.getBlockSize()).isEqualTo(slabs[0].getSize()); } @@ -145,7 +145,7 @@ public class TinyMemoryBlockJUnitTest { public void getNextBlockThrowsUnsupportedOperationException() { expectedException.expect(UnsupportedOperationException.class); - MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).getMemoryAddress(), 0); + MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new OffHeapStoredObject(slabs[0].getMemoryAddress(), slabs[0].getSize()).getAddress(), 0); mb.getNextBlock(); fail("getNextBlock failed to throw UnsupportedOperationException"); } @@ -154,23 +154,23 @@ public class TinyMemoryBlockJUnitTest { public void getSlabIdThrowsUnsupportedOperationException() { expectedException.expect(UnsupportedOperationException.class); - MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new ObjectChunk(slabs[0].getMemoryAddress(), slabs[0].getSize()).getMemoryAddress(), 0); + MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(new OffHeapStoredObject(slabs[0].getMemoryAddress(), slabs[0].getSize()).getAddress(), 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); + MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(new OffHeapStoredObject(slabs[0].getMemoryAddress(), slabs[0].getSize()).getAddress(), 0); + MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(new OffHeapStoredObject(slabs[1].getMemoryAddress(), slabs[1].getSize()).getAddress(), 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); + MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(new OffHeapStoredObject(slabs[0].getMemoryAddress(), slabs[0].getSize()).getAddress(), 0); + MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(new OffHeapStoredObject(slabs[1].getMemoryAddress(), slabs[1].getSize()).getAddress(), 1); softly.assertThat(mb0.getRefCount()).isEqualTo(0); softly.assertThat(mb1.getRefCount()).isEqualTo(0); } @@ -180,8 +180,8 @@ public class TinyMemoryBlockJUnitTest { Object obj = getValue(); boolean compressed = false; - MemoryChunkWithRefCount storedObject0 = createValueAsSerializedStoredObject(obj, compressed); - MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getMemoryAddress(), 0); + StoredObject storedObject0 = createValueAsSerializedStoredObject(obj, compressed); + MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getAddress(), 0); softly.assertThat(mb.getDataType()).isEqualTo("N/A"); } @@ -190,8 +190,8 @@ public class TinyMemoryBlockJUnitTest { Object obj = getValue(); boolean compressed = false; - MemoryChunkWithRefCount storedObject0 = createValueAsSerializedStoredObject(obj, compressed); - MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getMemoryAddress(), 0); + StoredObject storedObject0 = createValueAsSerializedStoredObject(obj, compressed); + MemoryBlock mb = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getAddress(), 0); softly.assertThat(mb.getDataValue()).isNull(); } @@ -200,10 +200,10 @@ public class TinyMemoryBlockJUnitTest { 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); + StoredObject storedObject0 = createValueAsSerializedStoredObject(obj, compressed); + StoredObject storedObject1 = createValueAsUnserializedStoredObject(obj, compressed); + MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getAddress(), 0); + MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject1).getAddress(), 0); softly.assertThat(mb0.isSerialized()).isFalse(); softly.assertThat(mb1.isSerialized()).isFalse(); } @@ -212,10 +212,10 @@ public class TinyMemoryBlockJUnitTest { 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); + StoredObject storedObject0 = createValueAsUnserializedStoredObject(obj, compressed); + StoredObject storedObject1 = createValueAsUnserializedStoredObject(obj, compressed = true); + MemoryBlock mb0 = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject0).getAddress(), 0); + MemoryBlock mb1 = new TestableFreeListManager.TinyMemoryBlock(((MemoryBlock)storedObject1).getAddress(), 0); softly.assertThat(mb0.isCompressed()).isFalse(); softly.assertThat(mb1.isCompressed()).isFalse(); } @@ -238,7 +238,7 @@ public class TinyMemoryBlockJUnitTest { @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()); + softly.assertThat(mb.hashCode()).isEqualTo(new OffHeapStoredObject(slabs[0].getMemoryAddress(), slabs[0].getSize()).hashCode()); } } http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3087c86f/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyStoredObjectJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyStoredObjectJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyStoredObjectJUnitTest.java new file mode 100644 index 0000000..94559d6 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/TinyStoredObjectJUnitTest.java @@ -0,0 +1,353 @@ +/* + * 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 com.gemstone.gemfire.compression.Compressor; +import com.gemstone.gemfire.internal.cache.BytesAndBitsForCompactor; +import com.gemstone.gemfire.internal.cache.CachePerfStats; +import com.gemstone.gemfire.internal.cache.EntryEventImpl; +import com.gemstone.gemfire.internal.cache.RegionEntryContext; +import com.gemstone.gemfire.internal.offheap.TinyStoredObject; + +import com.gemstone.gemfire.internal.offheap.OffHeapRegionEntryHelper; +import com.gemstone.gemfire.test.junit.categories.UnitTest; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.mockito.Mock; + +import java.nio.ByteBuffer; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.*; + +@Category(UnitTest.class) +public class TinyStoredObjectJUnitTest extends AbstractStoredObjectTestBase { + + @Override + public Object getValue() { + return Integer.valueOf(123456789); + } + + @Override + public byte[] getValueAsByteArray() { + return convertValueToByteArray(getValue()); + } + + private byte[] convertValueToByteArray(Object value) { + return ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).putInt((Integer) value).array(); + } + + @Override + public Object convertByteArrayToObject(byte[] valueInByteArray) { + return ByteBuffer.wrap(valueInByteArray).getInt(); + } + + @Override + public Object convertSerializedByteArrayToObject(byte[] valueInSerializedByteArray) { + return EntryEventImpl.deserialize(valueInSerializedByteArray); + } + + @Override + public TinyStoredObject createValueAsUnserializedStoredObject(Object value) { + byte[] valueInByteArray; + if(value instanceof Integer) { + valueInByteArray = convertValueToByteArray(value); + } else { + valueInByteArray = (byte[]) value; + } + //encode a non-serialized entry value to address + long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInByteArray, false, false); + return new TinyStoredObject(encodedAddress); + } + + @Override + public TinyStoredObject createValueAsSerializedStoredObject(Object value) { + byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); + //encode a serialized entry value to address + long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, false); + return new TinyStoredObject(encodedAddress); + } + + public TinyStoredObject createValueAsCompressedStoredObject(Object value) { + byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); + //encode a serialized, compressed entry value to address + long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, true); + return new TinyStoredObject(encodedAddress); + } + + public TinyStoredObject createValueAsUncompressedStoredObject(Object value) { + byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value); + //encode a serialized, uncompressed entry value to address + long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(valueInSerializedByteArray, true, false); + return new TinyStoredObject(encodedAddress); + } + + @Test + public void shouldReturnCorrectEncodingAddress() { + + TinyStoredObject address1 = new TinyStoredObject(10001L); + assertNotNull(address1); + assertEquals("Encoding address should be:", 10001, address1.getAddress()); + + TinyStoredObject address2 = new TinyStoredObject(10002L); + assertNotNull(address2); + assertEquals("Returning always 10001 expected 10002", 10002, address2.getAddress()); + } + + @Test + public void twoAddressesShouldBeEqualIfEncodingAddressIsSame() { + TinyStoredObject address1 = new TinyStoredObject(10001L); + TinyStoredObject address2 = new TinyStoredObject(10001L); + + assertEquals("Two addresses are equal if encoding address is same", true, address1.equals(address2)); + } + + @Test + public void twoAddressesShouldNotBeEqualIfEncodingAddressIsNotSame() { + TinyStoredObject address1 = new TinyStoredObject(10001L); + TinyStoredObject address2 = new TinyStoredObject(10002L); + + assertEquals("Two addresses are not equal if encoding address is not same", false, address1.equals(address2)); + } + + @Test + public void twoAddressesAreNotEqualIfTheyAreNotTypeDataAsAddress() { + TinyStoredObject address1 = new TinyStoredObject(10001L); + Long address2 = new Long(10002L); + + assertEquals("Two addresses are not equal if encoding address is not same", false, address1.equals(address2)); + } + + @Test + public void addressHashCodeShouldBe() { + TinyStoredObject address1 = new TinyStoredObject(10001L); + assertEquals("", 10001, address1.hashCode()); + } + + @Test + public void getSizeInBytesAlwaysReturnsZero() { + TinyStoredObject address1 = new TinyStoredObject(10001L); + TinyStoredObject address2 = new TinyStoredObject(10002L); + + assertEquals("getSizeInBytes", 0, address1.getSizeInBytes()); + assertEquals("getSizeInBytes", 0, address2.getSizeInBytes()); + } + + @Test + public void getValueSizeInBytesAlwaysReturnsZero() { + TinyStoredObject address1 = new TinyStoredObject(10001L); + TinyStoredObject address2 = new TinyStoredObject(10002L); + + assertEquals("getSizeInBytes", 0, address1.getValueSizeInBytes()); + assertEquals("getSizeInBytes", 0, address2.getValueSizeInBytes()); + } + + @Test + public void isCompressedShouldReturnTrueIfCompressed() { + Object regionEntryValue = getValue(); + + TinyStoredObject offheapAddress = createValueAsCompressedStoredObject(regionEntryValue); + + assertEquals("Should return true as it is compressed", true, offheapAddress.isCompressed()); + } + + @Test + public void isCompressedShouldReturnFalseIfNotCompressed() { + Object regionEntryValue = getValue(); + + TinyStoredObject offheapAddress = createValueAsUncompressedStoredObject(regionEntryValue); + + assertEquals("Should return false as it is compressed", false, offheapAddress.isCompressed()); + } + + @Test + public void isSerializedShouldReturnTrueIfSeriazlied() { + Object regionEntryValue = getValue(); + + TinyStoredObject offheapAddress = createValueAsSerializedStoredObject(regionEntryValue); + + assertEquals("Should return true as it is serialized", true, offheapAddress.isSerialized()); + } + + @Test + public void isSerializedShouldReturnFalseIfNotSeriazlied() { + Object regionEntryValue = getValue(); + + TinyStoredObject offheapAddress = createValueAsUnserializedStoredObject(regionEntryValue); + + assertEquals("Should return false as it is serialized", false, offheapAddress.isSerialized()); + } + + @Test + public void getDecompressedBytesShouldReturnDecompressedBytesIfCompressed() { + Object regionEntryValue = getValue(); + byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue); + + //encode a non-serialized and compressed entry value to address - last argument is to let that it is compressed + long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(regionEntryValueAsBytes, false, true); + TinyStoredObject offheapAddress = new TinyStoredObject(encodedAddress); + + RegionEntryContext regionContext = mock(RegionEntryContext.class); + CachePerfStats cacheStats = mock(CachePerfStats.class); + Compressor compressor = mock(Compressor.class); + + long startTime = 10000L; + + //mock required things + when(regionContext.getCompressor()).thenReturn(compressor); + when(compressor.decompress(regionEntryValueAsBytes)).thenReturn(regionEntryValueAsBytes); + when(regionContext.getCachePerfStats()).thenReturn(cacheStats); + when(cacheStats.startDecompression()).thenReturn(startTime); + + //invoke the thing + byte[] bytes = offheapAddress.getDecompressedBytes(regionContext); + + //verify the thing happened + verify(cacheStats, atLeastOnce()).startDecompression(); + verify(compressor, times(1)).decompress(regionEntryValueAsBytes); + verify(cacheStats, atLeastOnce()).endDecompression(startTime); + + assertArrayEquals(regionEntryValueAsBytes, bytes); + } + + @Test + public void getDecompressedBytesShouldNotTryToDecompressIfNotCompressed() { + Object regionEntryValue = getValue(); + + TinyStoredObject offheapAddress = createValueAsUncompressedStoredObject(regionEntryValue); + + //mock the thing + RegionEntryContext regionContext = mock(RegionEntryContext.class); + Compressor compressor = mock(Compressor.class); + when(regionContext.getCompressor()).thenReturn(compressor); + + //invoke the thing + byte[] actualValueInBytes = offheapAddress.getDecompressedBytes(regionContext); + + //createValueAsUncompressedStoredObject does uses a serialized value - so convert it to object + Object actualRegionValue = convertSerializedByteArrayToObject(actualValueInBytes); + + //verify the thing happened + verify(regionContext, never()).getCompressor(); + assertEquals(regionEntryValue, actualRegionValue); + } + + @Test + public void getRawBytesShouldReturnAByteArray() { + byte[] regionEntryValueAsBytes = getValueAsByteArray(); + + TinyStoredObject offheapAddress = createValueAsUnserializedStoredObject(regionEntryValueAsBytes); + byte[] actual = offheapAddress.getRawBytes(); + + assertArrayEquals(regionEntryValueAsBytes, actual); + } + + @Test + public void getSerializedValueShouldReturnASerializedByteArray() { + Object regionEntryValue = getValue(); + + TinyStoredObject offheapAddress = createValueAsSerializedStoredObject(regionEntryValue); + + byte[] actualSerializedValue = offheapAddress.getSerializedValue(); + + Object actualRegionEntryValue = convertSerializedByteArrayToObject(actualSerializedValue); + + assertEquals(regionEntryValue, actualRegionEntryValue); + } + + @Test + public void getDeserializedObjectShouldReturnADeserializedObject() { + Object regionEntryValue = getValue(); + + TinyStoredObject offheapAddress = createValueAsSerializedStoredObject(regionEntryValue); + + Integer actualRegionEntryValue = (Integer) offheapAddress.getDeserializedValue(null, null); + + assertEquals(regionEntryValue, actualRegionEntryValue); + } + + @Test + public void getDeserializedObjectShouldReturnAByteArrayAsIsIfNotSerialized() { + byte[] regionEntryValueAsBytes = getValueAsByteArray(); + + TinyStoredObject offheapAddress = createValueAsUnserializedStoredObject(regionEntryValueAsBytes); + + byte[] deserializeValue = (byte[]) offheapAddress.getDeserializedValue(null, null); + + assertArrayEquals(regionEntryValueAsBytes, deserializeValue); + } + + @Test + public void fillSerializedValueShouldFillWrapperWithSerializedValueIfValueIsSerialized() { + Object regionEntryValue = getValue(); + byte[] serializedRegionEntryValue = EntryEventImpl.serialize(regionEntryValue); + + //encode a serialized entry value to address + long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(serializedRegionEntryValue, true, false); + + TinyStoredObject offheapAddress = new TinyStoredObject(encodedAddress); + + //mock the things + BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class); + + byte userBits = 1; + offheapAddress.fillSerializedValue(wrapper, userBits); + + verify(wrapper, times(1)).setData(serializedRegionEntryValue, userBits, serializedRegionEntryValue.length, true); + } + + @Test + public void fillSerializedValueShouldFillWrapperWithDeserializedValueIfValueIsNotSerialized() { + Object regionEntryValue = getValue(); + byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue); + + //encode a un serialized entry value to address + long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(regionEntryValueAsBytes, false, false); + + TinyStoredObject offheapAddress = new TinyStoredObject(encodedAddress); + + //mock the things + BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class); + + byte userBits = 1; + offheapAddress.fillSerializedValue(wrapper, userBits); + + verify(wrapper, times(1)).setData(regionEntryValueAsBytes, userBits, regionEntryValueAsBytes.length, true); + } + + @Test + public void getStringFormShouldCatchExceptionAndReturnErrorMessageAsString() { + Object regionEntryValueAsBytes = getValue(); + + byte[] serializedValue = EntryEventImpl.serialize(regionEntryValueAsBytes); + + //store -127 (DSCODE.ILLEGAL) - in order the deserialize to throw exception + serializedValue[0] = -127; + + //encode a un serialized entry value to address + long encodedAddress = OffHeapRegionEntryHelper.encodeDataAsAddress(serializedValue, true, false); + + TinyStoredObject offheapAddress = new TinyStoredObject(encodedAddress); + + String errorMessage = offheapAddress.getStringForm(); + + assertEquals(true, errorMessage.contains("Could not convert object to string because ")); + } +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3087c86f/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java deleted file mode 100644 index d7168a7..0000000 --- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/UnsafeMemoryChunkJUnitTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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 org.junit.Test; -import org.junit.experimental.categories.Category; - -import com.gemstone.gemfire.test.junit.categories.UnitTest; - -@Category(UnitTest.class) -public class UnsafeMemoryChunkJUnitTest extends MemoryChunkJUnitTestBase { - - @Override - protected MemoryChunk createChunk(int size) { - return new UnsafeMemoryChunk(size); - } - - @Test - public void testGetAddress() { - MemoryChunk mc = createChunk(1024); - try { - AddressableMemoryChunk umc = (AddressableMemoryChunk) 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); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3087c86f/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java index 3d06e11..9ded958 100644 --- a/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java +++ b/geode-core/src/test/java/com/gemstone/gemfire/management/OffHeapManagementDUnitTest.java @@ -36,7 +36,7 @@ import com.gemstone.gemfire.cache30.CacheTestCase; import com.gemstone.gemfire.distributed.internal.DistributionConfig; import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem; import com.gemstone.gemfire.internal.cache.GemFireCacheImpl; -import com.gemstone.gemfire.internal.offheap.ObjectChunk; +import com.gemstone.gemfire.internal.offheap.OffHeapStoredObject; import com.gemstone.gemfire.internal.offheap.OffHeapMemoryStats; import com.gemstone.gemfire.internal.offheap.OffHeapStorage; import com.gemstone.gemfire.management.internal.MBeanJMXAdapter; @@ -240,7 +240,7 @@ public class OffHeapManagementDUnitTest extends CacheTestCase { // After allocating large chunk (equal to total memory) // we should still have no fragmentation - int largeChunk = (int) TOTAL_MEMORY - ObjectChunk.OFF_HEAP_HEADER_SIZE; + int largeChunk = (int) TOTAL_MEMORY - OffHeapStoredObject.HEADER_SIZE; doPutOnVm(vm, KEY, new byte[largeChunk], OFF_HEAP_REGION_NAME, false); // No compaction has run, so fragmentation should be zero assertFragmentationStatOnVm(vm,0,ASSERT_OP.EQUAL); @@ -255,7 +255,7 @@ public class OffHeapManagementDUnitTest extends CacheTestCase { assertFragmentationStatOnVm(vm,0,ASSERT_OP.EQUAL); // Allocate HALF_TOTAL_MEMORY twice and release one to create one fragment - int halfChunk = HALF_TOTAL_MEMORY - ObjectChunk.OFF_HEAP_HEADER_SIZE; + int halfChunk = HALF_TOTAL_MEMORY - OffHeapStoredObject.HEADER_SIZE; doPutOnVm(vm, KEY + "0", new byte[halfChunk], OFF_HEAP_REGION_NAME, false); doPutOnVm(vm, KEY + "1", new byte[halfChunk], OFF_HEAP_REGION_NAME, false); doDestroyOnVm(vm, KEY + "0", OFF_HEAP_REGION_NAME); @@ -267,7 +267,7 @@ public class OffHeapManagementDUnitTest extends CacheTestCase { // Consume the available fragment as below // [16][262120][16][262120][16] = [524288] (HALF_TOTAL_MEMORY) - int smallChunk = ObjectChunk.MIN_CHUNK_SIZE - ObjectChunk.OFF_HEAP_HEADER_SIZE; + int smallChunk = OffHeapStoredObject.MIN_CHUNK_SIZE - OffHeapStoredObject.HEADER_SIZE; int mediumChunk = 262112; //(262120 - ObjectChunk.OFF_HEAP_HEADER_SIZE) doPutOnVm(vm, KEY + "S1", new byte[smallChunk], OFF_HEAP_REGION_NAME, false); doPutOnVm(vm, KEY + "M1", new byte[mediumChunk], OFF_HEAP_REGION_NAME, false); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3087c86f/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java index 0f918cb..8380f57 100644 --- a/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java +++ b/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteBufferByteSourceJUnitTest.java @@ -22,7 +22,7 @@ import java.nio.ByteBuffer; import org.junit.experimental.categories.Category; -import com.gemstone.gemfire.internal.offheap.ObjectChunk; +import com.gemstone.gemfire.internal.offheap.OffHeapStoredObject; import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl; import com.gemstone.gemfire.internal.offheap.StoredObject; import com.gemstone.gemfire.internal.tcp.ByteBufferInputStream.ByteSource; @@ -35,8 +35,8 @@ public class OffHeapByteBufferByteSourceJUnitTest extends OffHeapByteSourceJUnit @Override protected ByteSource createByteSource(byte[] bytes) { StoredObject so = SimpleMemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, false, false); - if (so instanceof ObjectChunk) { - ObjectChunk c = (ObjectChunk) so; + if (so instanceof OffHeapStoredObject) { + OffHeapStoredObject c = (OffHeapStoredObject) so; ByteBuffer bb = c.createDirectByteBuffer(); if (bb == null) { fail("could not create a direct ByteBuffer for an off-heap Chunk"); http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3087c86f/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java index 6457425..2111f79 100644 --- a/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java +++ b/geode-core/src/test/java/com/gemstone/gemfire/pdx/OffHeapByteSourceJUnitTest.java @@ -20,12 +20,12 @@ import org.junit.After; import org.junit.Before; import org.junit.experimental.categories.Category; -import com.gemstone.gemfire.internal.offheap.ObjectChunk; +import com.gemstone.gemfire.internal.offheap.OffHeapStoredObject; import com.gemstone.gemfire.internal.offheap.NullOffHeapMemoryStats; import com.gemstone.gemfire.internal.offheap.NullOutOfOffHeapMemoryListener; import com.gemstone.gemfire.internal.offheap.SimpleMemoryAllocatorImpl; import com.gemstone.gemfire.internal.offheap.StoredObject; -import com.gemstone.gemfire.internal.offheap.UnsafeMemoryChunk; +import com.gemstone.gemfire.internal.offheap.SlabImpl; import com.gemstone.gemfire.internal.tcp.ByteBufferInputStream.ByteSource; import com.gemstone.gemfire.internal.tcp.ByteBufferInputStream.ByteSourceFactory; import com.gemstone.gemfire.internal.tcp.ByteBufferInputStream.OffHeapByteSource; @@ -36,7 +36,7 @@ public class OffHeapByteSourceJUnitTest extends ByteSourceJUnitTest { @Before public void setUp() throws Exception { - SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new UnsafeMemoryChunk[]{new UnsafeMemoryChunk(1024*1024)}); + SimpleMemoryAllocatorImpl.createForUnitTest(new NullOutOfOffHeapMemoryListener(), new NullOffHeapMemoryStats(), new SlabImpl[]{new SlabImpl(1024*1024)}); } @After @@ -52,9 +52,9 @@ public class OffHeapByteSourceJUnitTest extends ByteSourceJUnitTest { @Override protected ByteSource createByteSource(byte[] bytes) { StoredObject so = SimpleMemoryAllocatorImpl.getAllocator().allocateAndInitialize(bytes, false, false); - if (so instanceof ObjectChunk) { + if (so instanceof OffHeapStoredObject) { // bypass the factory to make sure that OffHeapByteSource is tested - return new OffHeapByteSource((ObjectChunk)so); + return new OffHeapByteSource(so); } else { // bytes are so small they can be encoded in a long (see DataAsAddress). // So for this test just wrap the original bytes. http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/3087c86f/geode-core/src/test/resources/com/gemstone/gemfire/codeAnalysis/sanctionedDataSerializables.txt ---------------------------------------------------------------------- diff --git a/geode-core/src/test/resources/com/gemstone/gemfire/codeAnalysis/sanctionedDataSerializables.txt b/geode-core/src/test/resources/com/gemstone/gemfire/codeAnalysis/sanctionedDataSerializables.txt index dd48df4..bb9f350 100644 --- a/geode-core/src/test/resources/com/gemstone/gemfire/codeAnalysis/sanctionedDataSerializables.txt +++ b/geode-core/src/test/resources/com/gemstone/gemfire/codeAnalysis/sanctionedDataSerializables.txt @@ -1030,7 +1030,7 @@ toData,17,2ab400022bb800042ab400032bb80004b1 com/gemstone/gemfire/internal/cache/EntryEventImpl,2 fromData,214,2a2bb8001dc0001eb5001f2bb8001d4d2bb8001d4e2abb0020592c2d01b70021b500222a2bb900230100b80024b500252a2bb900260100b500082ab400222bb8001db600272a2bb8001dc00028b500092bb9002901009900112a2bb8001dc0002ab50007a700322bb9002901009900212a2bb8002bb5002c2a2ab4002cb500052a2ab4002cb8002db50004a7000b2a2bb8001db500042bb9002901009900192a2bb8002bb5002e2a2ab4002eb8002db50006a7000b2a2bb8001db500062a2bb8002fb500302a2bb80031b5000a2a2bb80032b50016b1 -toData,326,2ab4001f2bb801612ab600902bb801612ab40022b6018a2bb801612b2ab40025b4018bb9018c02002b2ab4000811c03f7eb9018d02002ab6004d2bb801612ab400092bb801612ab40007c6000704a70004033d2b1cb9018e02001c99000e2ab400072bb80161a7006f2ab600434e2dc10086360415049900152dc1008499000e2dc00084b900bb010036042b1504b9018e0200150499003b2ab4002cc6000e2ab4002c2bb8018fa7002e2ab40005c6000e2ab400052bb8018fa7001c2dc000863a051905b900c701002bb80190a700082d2bb801612ab700454d2cc100863e1d9900142cc1008499000d2cc00084b900bb01003e2b1db9018e02001d9900292ab4002ec6000e2ab4002e2bb8018fa7001c2cc000863a041904b900c701002bb80190a700082c2bb801612ab40030c001912bb801922ab6005a2bb801612ab400162bb80193b1 +toData,312,2ab400202bb801632ab6008f2bb801632ab40023b6018c2bb801632b2ab40026b4018db9018e02002b2ab4000911c03f7eb9018f02002ab6004e2bb801632ab4000a2bb801632ab40008c6000704a70004033d2b1cb9019002001c99000e2ab400082bb80163a700682ab600444e2dc100853604150499000e2dc00085b900bb010036042b1504b901900200150499003b2ab4002dc6000e2ab4002d2bb80191a7002e2ab40006c6000e2ab400062bb80191a7001c2dc000853a051905b900c901002bb80192a700082d2bb801632ab700464d2cc100853e1d99000d2cc00085b900bb01003e2b1db9019002001d9900292ab4002fc6000e2ab4002f2bb80191a7001c2cc000853a041904b900c901002bb80192a700082c2bb801632ab40031c001932bb801942ab6005b2bb801632ab400172bb80195b1 com/gemstone/gemfire/internal/cache/EntrySnapshot,2 fromData,50,2a03b500052bb9004101003d1c9900112abb000759b70042b50004a7000e2abb000359b70043b500042ab400042bb60044b1