You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ds...@apache.org on 2016/03/05 01:51:31 UTC
[31/38] incubator-geode git commit: renamed ObjectStoredInMemory to
OffHeapStoredObject
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkJUnitTest.java
deleted file mode 100644
index a9390c1..0000000
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkJUnitTest.java
+++ /dev/null
@@ -1,889 +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.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.atLeastOnce;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.io.IOException;
-import java.nio.ByteBuffer;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-
-import com.gemstone.gemfire.LogWriter;
-import com.gemstone.gemfire.compression.Compressor;
-import com.gemstone.gemfire.internal.DSCODE;
-import com.gemstone.gemfire.internal.HeapDataOutputStream;
-import com.gemstone.gemfire.internal.Version;
-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.MemoryBlock.State;
-import com.gemstone.gemfire.test.junit.categories.UnitTest;
-
-@Category(UnitTest.class)
-public class ObjectChunkJUnitTest extends AbstractStoredObjectTestBase {
-
- private MemoryAllocator ma;
-
- static {
- ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
- }
-
- @Before
- public void setUp() {
- OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
- OffHeapMemoryStats stats = mock(OffHeapMemoryStats.class);
- LogWriter lw = mock(LogWriter.class);
-
- ma = SimpleMemoryAllocatorImpl.create(ooohml, stats, lw, 3, OffHeapStorage.MIN_SLAB_SIZE * 3, OffHeapStorage.MIN_SLAB_SIZE);
- }
-
- @After
- public void tearDown() {
- SimpleMemoryAllocatorImpl.freeOffHeapMemory();
- }
-
- @Override
- public Object getValue() {
- return Long.valueOf(Long.MAX_VALUE);
- }
-
- @Override
- public byte[] getValueAsByteArray() {
- return convertValueToByteArray(getValue());
- }
-
- private byte[] convertValueToByteArray(Object value) {
- return ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong((Long) value).array();
- }
-
- @Override
- public Object convertByteArrayToObject(byte[] valueInByteArray) {
- return ByteBuffer.wrap(valueInByteArray).getLong();
- }
-
- @Override
- public Object convertSerializedByteArrayToObject(byte[] valueInSerializedByteArray) {
- return EntryEventImpl.deserialize(valueInSerializedByteArray);
- }
-
- @Override
- public ObjectStoredInMemory createValueAsUnserializedStoredObject(Object value) {
- byte[] valueInByteArray;
- if (value instanceof Long) {
- valueInByteArray = convertValueToByteArray(value);
- } else {
- valueInByteArray = (byte[]) value;
- }
-
- boolean isSerialized = false;
- boolean isCompressed = false;
-
- return createChunk(valueInByteArray, isSerialized, isCompressed);
- }
-
- @Override
- public ObjectStoredInMemory createValueAsSerializedStoredObject(Object value) {
- byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value);
-
- boolean isSerialized = true;
- boolean isCompressed = false;
-
- return createChunk(valueInSerializedByteArray, isSerialized, isCompressed);
- }
-
- private ObjectStoredInMemory createChunk(byte[] v, boolean isSerialized, boolean isCompressed) {
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(v, isSerialized, isCompressed);
- return chunk;
- }
-
- @Test
- public void chunkCanBeCreatedFromAnotherChunk() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
-
- ObjectStoredInMemory newChunk = new ObjectStoredInMemory(chunk);
-
- assertNotNull(newChunk);
- assertThat(newChunk.getAddress()).isEqualTo(chunk.getAddress());
-
- chunk.release();
- }
-
- @Test
- public void chunkCanBeCreatedWithOnlyMemoryAddress() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
-
- ObjectStoredInMemory newChunk = new ObjectStoredInMemory(chunk.getAddress());
-
- assertNotNull(newChunk);
- assertThat(newChunk.getAddress()).isEqualTo(chunk.getAddress());
-
- chunk.release();
- }
-
- @Test
- public void chunkSliceCanBeCreatedFromAnotherChunk() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
-
- int position = 1;
- int end = 2;
-
- ObjectStoredInMemory newChunk = (ObjectStoredInMemory) chunk.slice(position, end);
-
- assertNotNull(newChunk);
- assertThat(newChunk.getClass()).isEqualTo(ObjectChunkSlice.class);
- assertThat(newChunk.getAddress()).isEqualTo(chunk.getAddress());
-
- chunk.release();
- }
-
- @Test
- public void fillSerializedValueShouldFillWrapperWithSerializedValueIfValueIsSerialized() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- // mock the things
- BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class);
-
- byte userBits = 0;
- byte serializedUserBits = 1;
- chunk.fillSerializedValue(wrapper, userBits);
-
- verify(wrapper, times(1)).setOffHeapData(chunk, serializedUserBits);
-
- chunk.release();
- }
-
- @Test
- public void fillSerializedValueShouldFillWrapperWithDeserializedValueIfValueIsNotSerialized() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
-
- // mock the things
- BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class);
-
- byte userBits = 1;
- chunk.fillSerializedValue(wrapper, userBits);
-
- verify(wrapper, times(1)).setOffHeapData(chunk, userBits);
-
- chunk.release();
- }
-
- @Test
- public void getShortClassNameShouldReturnShortClassName() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- assertThat(chunk.getShortClassName()).isEqualTo("ObjectStoredInMemory");
-
- chunk.release();
- }
-
- @Test
- public void chunksAreEqualsOnlyByAddress() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- ObjectStoredInMemory newChunk = new ObjectStoredInMemory(chunk.getAddress());
- assertThat(chunk.equals(newChunk)).isTrue();
-
- ObjectStoredInMemory chunkWithSameValue = createValueAsUnserializedStoredObject(getValue());
- assertThat(chunk.equals(chunkWithSameValue)).isFalse();
-
- Object someObject = getValue();
- assertThat(chunk.equals(someObject)).isFalse();
-
- chunk.release();
- chunkWithSameValue.release();
- }
-
- @Test
- public void chunksShouldBeComparedBySize() {
- ObjectStoredInMemory chunk1 = createValueAsSerializedStoredObject(getValue());
-
- ObjectStoredInMemory chunk2 = chunk1;
- assertThat(chunk1.compareTo(chunk2)).isEqualTo(0);
-
- ObjectStoredInMemory chunkWithSameValue = createValueAsSerializedStoredObject(getValue());
- assertThat(chunk1.compareTo(chunkWithSameValue)).isEqualTo(Long.signum(chunk1.getAddress() - chunkWithSameValue.getAddress()));
-
- ObjectStoredInMemory chunk3 = createValueAsSerializedStoredObject(Long.MAX_VALUE);
- ObjectStoredInMemory chunk4 = createValueAsSerializedStoredObject(Long.MAX_VALUE);
-
- int newSizeForChunk3 = 2;
- int newSizeForChunk4 = 3;
-
- assertThat(chunk3.compareTo(chunk4)).isEqualTo(Integer.signum(newSizeForChunk3 - newSizeForChunk4));
-
- chunk1.release();
- chunk4.release();
- }
-
- @Test
- public void setSerializedShouldSetTheSerializedBit() {
- Object regionEntryValue = getValue();
- byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
-
- boolean isSerialized = false;
- boolean isCompressed = false;
-
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
-
- int headerBeforeSerializedBitSet = AddressableMemoryManager.readIntVolatile(chunk.getAddress() + ObjectStoredInMemory.REF_COUNT_OFFSET);
-
- assertThat(chunk.isSerialized()).isFalse();
-
- chunk.setSerialized(true); // set to true
-
- assertThat(chunk.isSerialized()).isTrue();
-
- int headerAfterSerializedBitSet = AddressableMemoryManager.readIntVolatile(chunk.getAddress() + ObjectStoredInMemory.REF_COUNT_OFFSET);
-
- assertThat(headerAfterSerializedBitSet).isEqualTo(headerBeforeSerializedBitSet | ObjectStoredInMemory.IS_SERIALIZED_BIT);
-
- chunk.release();
- }
-
- @Test(expected = IllegalStateException.class)
- public void setSerialziedShouldThrowExceptionIfChunkIsAlreadyReleased() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.release();
- chunk.setSerialized(true);
-
- chunk.release();
- }
-
- @Test
- public void setCompressedShouldSetTheCompressedBit() {
- Object regionEntryValue = getValue();
- byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
-
- boolean isSerialized = false;
- boolean isCompressed = false;
-
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
-
- int headerBeforeCompressedBitSet = AddressableMemoryManager.readIntVolatile(chunk.getAddress() + ObjectStoredInMemory.REF_COUNT_OFFSET);
-
- assertThat(chunk.isCompressed()).isFalse();
-
- chunk.setCompressed(true); // set to true
-
- assertThat(chunk.isCompressed()).isTrue();
-
- int headerAfterCompressedBitSet = AddressableMemoryManager.readIntVolatile(chunk.getAddress() + ObjectStoredInMemory.REF_COUNT_OFFSET);
-
- assertThat(headerAfterCompressedBitSet).isEqualTo(headerBeforeCompressedBitSet | ObjectStoredInMemory.IS_COMPRESSED_BIT);
-
- chunk.release();
- }
-
- @Test(expected = IllegalStateException.class)
- public void setCompressedShouldThrowExceptionIfChunkIsAlreadyReleased() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.release();
- chunk.setCompressed(true);
-
- chunk.release();
- }
-
- @Test
- public void setDataSizeShouldSetTheDataSizeBits() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
-
- int beforeSize = chunk.getDataSize();
-
- chunk.setDataSize(2);
-
- int afterSize = chunk.getDataSize();
-
- assertThat(afterSize).isEqualTo(2);
- assertThat(afterSize).isNotEqualTo(beforeSize);
-
- chunk.release();
- }
-
- @Test(expected = IllegalStateException.class)
- public void setDataSizeShouldThrowExceptionIfChunkIsAlreadyReleased() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.release();
- chunk.setDataSize(1);
-
- chunk.release();
- }
-
- @Test(expected = IllegalStateException.class)
- public void initializeUseCountShouldThrowIllegalStateExceptionIfChunkIsAlreadyRetained() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.retain();
- chunk.initializeUseCount();
-
- chunk.release();
- }
-
- @Test(expected = IllegalStateException.class)
- public void initializeUseCountShouldThrowIllegalStateExceptionIfChunkIsAlreadyReleased() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.release();
- chunk.initializeUseCount();
-
- chunk.release();
- }
-
- @Test
- public void isSerializedPdxInstanceShouldReturnTrueIfItsPDXInstance() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- byte[] serailizedValue = chunk.getSerializedValue();
- serailizedValue[0] = DSCODE.PDX;
- chunk.setSerializedValue(serailizedValue);
-
- assertThat(chunk.isSerializedPdxInstance()).isTrue();
-
- serailizedValue = chunk.getSerializedValue();
- serailizedValue[0] = DSCODE.PDX_ENUM;
- chunk.setSerializedValue(serailizedValue);
-
- assertThat(chunk.isSerializedPdxInstance()).isTrue();
-
- serailizedValue = chunk.getSerializedValue();
- serailizedValue[0] = DSCODE.PDX_INLINE_ENUM;
- chunk.setSerializedValue(serailizedValue);
-
- assertThat(chunk.isSerializedPdxInstance()).isTrue();
-
- chunk.release();
- }
-
- @Test
- public void isSerializedPdxInstanceShouldReturnFalseIfItsNotPDXInstance() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- assertThat(chunk.isSerializedPdxInstance()).isFalse();
-
- chunk.release();
- }
-
- @Test
- public void checkDataEqualsByChunk() {
- ObjectStoredInMemory chunk1 = createValueAsSerializedStoredObject(getValue());
- ObjectStoredInMemory sameAsChunk1 = chunk1;
-
- assertThat(chunk1.checkDataEquals(sameAsChunk1)).isTrue();
-
- ObjectStoredInMemory unserializedChunk = createValueAsUnserializedStoredObject(getValue());
- assertThat(chunk1.checkDataEquals(unserializedChunk)).isFalse();
-
- ObjectStoredInMemory chunkDifferBySize = createValueAsSerializedStoredObject(getValue());
- chunkDifferBySize.setSize(0);
- assertThat(chunk1.checkDataEquals(chunkDifferBySize)).isFalse();
-
- ObjectStoredInMemory chunkDifferByValue = createValueAsSerializedStoredObject(Long.MAX_VALUE - 1);
- assertThat(chunk1.checkDataEquals(chunkDifferByValue)).isFalse();
-
- ObjectStoredInMemory newChunk1 = createValueAsSerializedStoredObject(getValue());
- assertThat(chunk1.checkDataEquals(newChunk1)).isTrue();
-
- chunk1.release();
- unserializedChunk.release();
- chunkDifferBySize.release();
- chunkDifferByValue.release();
- newChunk1.release();
- }
-
- @Test
- public void checkDataEqualsBySerializedValue() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- assertThat(chunk.checkDataEquals(new byte[1])).isFalse();
-
- ObjectStoredInMemory chunkDifferByValue = createValueAsSerializedStoredObject(Long.MAX_VALUE - 1);
- assertThat(chunk.checkDataEquals(chunkDifferByValue.getSerializedValue())).isFalse();
-
- ObjectStoredInMemory newChunk = createValueAsSerializedStoredObject(getValue());
- assertThat(chunk.checkDataEquals(newChunk.getSerializedValue())).isTrue();
-
- chunk.release();
- chunkDifferByValue.release();
- newChunk.release();
- }
-
- @Test
- public void getDecompressedBytesShouldReturnDecompressedBytesIfCompressed() {
- Object regionEntryValue = getValue();
- byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
-
- boolean isSerialized = true;
- boolean isCompressed = true;
-
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
-
- 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 = chunk.getDecompressedBytes(regionContext);
-
- // verify the thing happened
- verify(cacheStats, atLeastOnce()).startDecompression();
- verify(compressor, times(1)).decompress(regionEntryValueAsBytes);
- verify(cacheStats, atLeastOnce()).endDecompression(startTime);
-
- assertArrayEquals(regionEntryValueAsBytes, bytes);
-
- chunk.release();
- }
-
- @Test
- public void incSizeShouldIncrementSize() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- int beforeSize = chunk.getSize();
-
- chunk.incSize(1);
- assertThat(chunk.getSize()).isEqualTo(beforeSize + 1);
-
- chunk.incSize(2);
- assertThat(chunk.getSize()).isEqualTo(beforeSize + 1 + 2);
-
- chunk.release();
- }
-
- @Test
- public void readyForFreeShouldResetTheRefCount() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- int refCountBeforeFreeing = chunk.getRefCount();
- assertThat(refCountBeforeFreeing).isEqualTo(1);
-
- chunk.readyForFree();
-
- int refCountAfterFreeing = chunk.getRefCount();
- assertThat(refCountAfterFreeing).isEqualTo(0);
- }
-
- @Test(expected = IllegalStateException.class)
- public void readyForAllocationShouldThrowExceptionIfAlreadyAllocated() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- // chunk is already allocated when we created it, so calling readyForAllocation should throw exception.
- chunk.readyForAllocation();
-
- chunk.release();
- }
-
- @Test
- public void checkIsAllocatedShouldReturnIfAllocated() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- chunk.checkIsAllocated();
-
- chunk.release();
- }
-
- @Test(expected = IllegalStateException.class)
- public void checkIsAllocatedShouldThrowExceptionIfNotAllocated() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- chunk.release();
- chunk.checkIsAllocated();
-
- chunk.release();
- }
-
- @Test
- public void sendToShouldWriteSerializedValueToDataOutputIfValueIsSerialized() throws IOException {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- ObjectStoredInMemory spyChunk = spy(chunk);
-
- HeapDataOutputStream dataOutput = mock(HeapDataOutputStream.class);
- ByteBuffer directByteBuffer = ByteBuffer.allocate(1024);
-
- doReturn(directByteBuffer).when(spyChunk).createDirectByteBuffer();
- doNothing().when(dataOutput).write(directByteBuffer);
-
- spyChunk.sendTo(dataOutput);
-
- verify(dataOutput, times(1)).write(directByteBuffer);
-
- chunk.release();
- }
-
- @Test
- public void sendToShouldWriteUnserializedValueToDataOutputIfValueIsUnserialized() throws IOException {
- byte[] regionEntryValue = getValueAsByteArray();
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(regionEntryValue);
-
- // writeByte is a final method and cannot be mocked, so creating a real one
- HeapDataOutputStream dataOutput = new HeapDataOutputStream(Version.CURRENT);
-
- chunk.sendTo(dataOutput);
-
- byte[] actual = dataOutput.toByteArray();
-
- byte[] expected = new byte[regionEntryValue.length + 2];
- expected[0] = DSCODE.BYTE_ARRAY;
- expected[1] = (byte) regionEntryValue.length;
- System.arraycopy(regionEntryValue, 0, expected, 2, regionEntryValue.length);
-
- assertNotNull(dataOutput);
- assertThat(actual).isEqualTo(expected);
-
- chunk.release();
- }
-
- @Test
- public void sendAsByteArrayShouldWriteValueToDataOutput() throws IOException {
- byte[] regionEntryValue = getValueAsByteArray();
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(regionEntryValue);
-
- // writeByte is a final method and cannot be mocked, so creating a real one
- HeapDataOutputStream dataOutput = new HeapDataOutputStream(Version.CURRENT);
-
- chunk.sendAsByteArray(dataOutput);
-
- byte[] actual = dataOutput.toByteArray();
-
- byte[] expected = new byte[regionEntryValue.length + 1];
- expected[0] = (byte) regionEntryValue.length;
- System.arraycopy(regionEntryValue, 0, expected, 1, regionEntryValue.length);
-
- assertNotNull(dataOutput);
- assertThat(actual).isEqualTo(expected);
-
- chunk.release();
- }
-
- @Test
- public void createDirectByteBufferShouldCreateAByteBuffer() {
- byte[] regionEntryValue = getValueAsByteArray();
-
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(regionEntryValue);
-
- ByteBuffer buffer = chunk.createDirectByteBuffer();
-
- byte[] actual = new byte[regionEntryValue.length];
- buffer.get(actual);
-
- assertArrayEquals(regionEntryValue, actual);
-
- chunk.release();
- }
-
- @Test
- public void getDirectByteBufferShouldCreateAByteBuffer() {
- byte[] regionEntryValue = getValueAsByteArray();
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(regionEntryValue);
-
- ByteBuffer buffer = chunk.createDirectByteBuffer();
- long bufferAddress = AddressableMemoryManager.getDirectByteBufferAddress(buffer);
-
- // returned address should be starting of the value (after skipping HEADER_SIZE bytes)
- assertEquals(chunk.getAddress() + ObjectStoredInMemory.HEADER_SIZE, bufferAddress);
-
- chunk.release();
- }
-
- @Test(expected = AssertionError.class)
- public void getAddressForReadingShouldFailIfItsOutsideOfChunk() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- chunk.getAddressForReadingData(0, chunk.getDataSize() + 1);
-
- chunk.release();
- }
-
- @Test
- public void getAddressForReadingShouldReturnDataAddressFromGivenOffset() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- int offset = 1;
- long requestedAddress = chunk.getAddressForReadingData(offset, 1);
-
- assertThat(requestedAddress).isEqualTo(chunk.getBaseDataAddress() + offset);
-
- chunk.release();
- }
-
- @Test
- public void getSizeInBytesShouldReturnSize() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- assertThat(chunk.getSizeInBytes()).isEqualTo(chunk.getSize());
-
- chunk.release();
- }
-
- @Test(expected = AssertionError.class)
- public void getUnsafeAddressShouldFailIfOffsetIsNegative() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- chunk.getUnsafeAddress(-1, 1);
-
- chunk.release();
- }
-
- @Test(expected = AssertionError.class)
- public void getUnsafeAddressShouldFailIfSizeIsNegative() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- chunk.getUnsafeAddress(1, -1);
-
- chunk.release();
- }
-
- @Test(expected = AssertionError.class)
- public void getUnsafeAddressShouldFailIfItsOutsideOfChunk() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
- chunk.getUnsafeAddress(0, chunk.getDataSize() + 1);
-
- chunk.release();
- }
-
- @Test
- public void getUnsafeAddressShouldReturnUnsafeAddress() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- int offset = 1;
- long unsafeAddress = chunk.getUnsafeAddress(offset, 1);
-
- assertThat(unsafeAddress).isEqualTo(chunk.getBaseDataAddress() + offset);
-
- chunk.release();
- }
-
- @Test(expected = AssertionError.class)
- public void readByteAndWriteByteShouldFailIfOffsetIsOutside() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- chunk.readDataByte(chunk.getDataSize() + 1);
-
- chunk.writeDataByte(chunk.getDataSize() + 1, Byte.MAX_VALUE);
-
- chunk.release();
- }
-
- @Test
- public void writeByteShouldWriteAtCorrectLocation() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
-
- byte valueBeforeWrite = chunk.readDataByte(2);
-
- Byte expected = Byte.MAX_VALUE;
- chunk.writeDataByte(2, expected);
-
- Byte actual = chunk.readDataByte(2);
-
- assertThat(actual).isNotEqualTo(valueBeforeWrite);
- assertThat(actual).isEqualTo(expected);
-
- chunk.release();
- }
-
- @Test
- public void retainShouldIncrementRefCount() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- assertThat(chunk.getRefCount()).isEqualTo(1);
-
- chunk.retain();
- assertThat(chunk.getRefCount()).isEqualTo(2);
-
- chunk.retain();
- assertThat(chunk.getRefCount()).isEqualTo(3);
-
- chunk.release();
- chunk.release();
- chunk.release();
- boolean retainAfterRelease = chunk.retain();
-
- assertThat(retainAfterRelease).isFalse();
- }
-
- @Test(expected = IllegalStateException.class)
- public void retainShouldThrowExceptionAfterMaxNumberOfTimesRetained() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
-
- // loop though and invoke retain for MAX_REF_COUNT-1 times, as create chunk above counted as one reference
- for (int i = 0; i < ObjectStoredInMemory.MAX_REF_COUNT - 1; i++)
- chunk.retain();
-
- // invoke for the one more time should throw exception
- chunk.retain();
- }
-
- @Test
- public void releaseShouldDecrementRefCount() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- assertThat(chunk.getRefCount()).isEqualTo(1);
-
- chunk.retain();
- chunk.retain();
- assertThat(chunk.getRefCount()).isEqualTo(3);
-
- chunk.release();
- assertThat(chunk.getRefCount()).isEqualTo(2);
-
- chunk.release();
- assertThat(chunk.getRefCount()).isEqualTo(1);
-
- chunk.retain();
- chunk.release();
- assertThat(chunk.getRefCount()).isEqualTo(1);
-
- chunk.release();
- assertThat(chunk.getRefCount()).isEqualTo(0);
- }
-
- @Test(expected = IllegalStateException.class)
- public void releaseShouldThrowExceptionIfChunkIsAlreadyReleased() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.release();
- chunk.release();
- }
-
- @Test
- public void testToString() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
-
- String expected = ":<dataSize=" + chunk.getDataSize() + " refCount=" + chunk.getRefCount() + " addr=" + Long.toHexString(chunk.getAddress()) + ">";
- assertThat(chunk.toString()).endsWith(expected);
-
- chunk.release();
- }
-
- @Test
- public void getStateShouldReturnAllocatedIfRefCountIsGreaterThanZero() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- assertEquals(State.ALLOCATED, chunk.getState());
-
- chunk.release();
- }
-
- @Test
- public void getStateShouldReturnDeallocatedIfRefCountIsZero() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.release();
- assertEquals(State.DEALLOCATED, chunk.getState());
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void getNextBlockShouldThrowUnSupportedOperationException() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.getNextBlock();
-
- chunk.release();
- }
-
- @Test
- public void getBlockSizeShouldBeSameSameGetSize() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- assertEquals(chunk.getSize(), chunk.getBlockSize());
-
- chunk.release();
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void getSlabIdShouldThrowUnSupportedOperationException() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- chunk.getSlabId();
-
- chunk.release();
- }
-
- @Test
- public void getFreeListIdShouldReturnMinusOne() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- assertThat(chunk.getFreeListId()).isEqualTo(-1);
-
- chunk.release();
- }
-
- @Test
- public void getDataTypeShouldReturnNull() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- assertThat(chunk.getDataType()).isNull();
-
- chunk.release();
- }
-
- @Test
- public void getDataDataShouldReturnNull() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
- assertThat(chunk.getDataValue()).isNull();
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void getRawBytesShouldThrowExceptionIfValueIsCompressed() {
- Object regionEntryValue = getValue();
- byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
-
- boolean isSerialized = true;
- boolean isCompressed = true;
-
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
-
- chunk.getRawBytes();
-
- chunk.release();
- }
-
- @Test
- public void getSerializedValueShouldSerializeTheValue() {
- Object regionEntryValue = getValue();
- byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
-
- boolean isSerialized = false;
- boolean isCompressed = false;
-
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
-
- byte[] serializedValue = chunk.getSerializedValue();
-
- assertThat(serializedValue).isEqualTo(EntryEventImpl.serialize(regionEntryValueAsBytes));
-
- chunk.release();
- }
-
- @Test
- public void fillShouldFillTheChunk() {
- boolean isSerialized = false;
- boolean isCompressed = false;
-
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(new byte[100], isSerialized, isCompressed);
-
- // first fill the unused part with FILL_PATTERN
- ObjectStoredInMemory.fill(chunk.getAddress());
-
- // Validate that it is filled
- chunk.validateFill();
-
- chunk.release();
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkSliceJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkSliceJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkSliceJUnitTest.java
index 627769d..cea0fd6 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkSliceJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkSliceJUnitTest.java
@@ -26,14 +26,14 @@ import org.junit.experimental.categories.Category;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
@Category(UnitTest.class)
-public class ObjectChunkSliceJUnitTest extends ObjectChunkJUnitTest {
+public class ObjectChunkSliceJUnitTest extends OffHeapStoredObjectJUnitTest {
@Test
public void sliceShouldHaveAValidDataSize() {
int position = 1;
int end = 2;
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
ObjectChunkSlice slice = (ObjectChunkSlice) chunk.slice(position, end);
assertNotNull(slice);
@@ -47,7 +47,7 @@ public class ObjectChunkSliceJUnitTest extends ObjectChunkJUnitTest {
int position = 1;
int end = 2;
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
ObjectChunkSlice slice = (ObjectChunkSlice) chunk.slice(position, end);
assertNotNull(slice);
@@ -61,7 +61,7 @@ public class ObjectChunkSliceJUnitTest extends ObjectChunkJUnitTest {
int position = 1;
int end = 2;
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
ObjectChunkSlice slice = (ObjectChunkSlice) chunk.slice(position, end);
assertNotNull(slice);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkWithHeapFormJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkWithHeapFormJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkWithHeapFormJUnitTest.java
index aa1df16..4262e90 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkWithHeapFormJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/ObjectChunkWithHeapFormJUnitTest.java
@@ -29,11 +29,11 @@ import org.junit.experimental.categories.Category;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
@Category(UnitTest.class)
-public class ObjectChunkWithHeapFormJUnitTest extends ObjectChunkJUnitTest {
+public class ObjectChunkWithHeapFormJUnitTest extends OffHeapStoredObjectJUnitTest {
@Test
public void getRawBytesShouldReturnCachedHeapForm() {
- ObjectStoredInMemory chunk = createValueAsUnserializedStoredObject(getValue());
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
byte[] valueInBytes = getValueAsByteArray();
ObjectChunkWithHeapForm heapForm = new ObjectChunkWithHeapForm(chunk, valueInBytes);
@@ -45,15 +45,15 @@ public class ObjectChunkWithHeapFormJUnitTest extends ObjectChunkJUnitTest {
@Test
public void getChunkWithoutHeapFormShouldReturnGemFireChunk() {
- ObjectStoredInMemory chunk = createValueAsSerializedStoredObject(getValue());
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
byte[] valueInBytes = getValueAsByteArray();
ObjectChunkWithHeapForm heapForm = new ObjectChunkWithHeapForm(chunk, valueInBytes);
- ObjectStoredInMemory chunkWithOutHeapForm = heapForm.getChunkWithoutHeapForm();
+ OffHeapStoredObject chunkWithOutHeapForm = heapForm.getChunkWithoutHeapForm();
assertNotNull(chunkWithOutHeapForm);
- assertEquals(ObjectStoredInMemory.class, chunkWithOutHeapForm.getClass());
+ assertEquals(OffHeapStoredObject.class, chunkWithOutHeapForm.getClass());
assertEquals(chunk, heapForm.getChunkWithoutHeapForm());
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java
index 4517206..989abfc 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapHelperJUnitTest.java
@@ -128,8 +128,8 @@ public class OffHeapHelperJUnitTest extends AbstractStoredObjectTestBase {
return createdObject;
}
- private ObjectStoredInMemory createChunk(byte[] v, boolean isSerialized, boolean isCompressed) {
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(v, isSerialized, isCompressed);
+ private OffHeapStoredObject createChunk(byte[] v, boolean isSerialized, boolean isCompressed) {
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(v, isSerialized, isCompressed);
return chunk;
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java
index a167414..ae1b35d 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionBase.java
@@ -540,11 +540,11 @@ public abstract class OffHeapRegionBase {
@Released(OffHeapIdentifier.TEST_OFF_HEAP_REGION_BASE_LISTENER)
@Override
public void close() {
- if (this.ohOldValue instanceof ObjectStoredInMemory) {
- ((ObjectStoredInMemory)this.ohOldValue).release();
+ if (this.ohOldValue instanceof OffHeapStoredObject) {
+ ((OffHeapStoredObject)this.ohOldValue).release();
}
- if (this.ohNewValue instanceof ObjectStoredInMemory) {
- ((ObjectStoredInMemory)this.ohNewValue).release();
+ if (this.ohNewValue instanceof OffHeapStoredObject) {
+ ((OffHeapStoredObject)this.ohNewValue).release();
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
index ad6fa35..540bba5 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapRegionEntryHelperJUnitTest.java
@@ -54,7 +54,7 @@ import com.gemstone.gemfire.test.junit.categories.UnitTest;
@Category(UnitTest.class)
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("*.UnitTest")
-@PrepareForTest({ ObjectStoredInMemory.class, OffHeapRegionEntryHelper.class })
+@PrepareForTest({ OffHeapStoredObject.class, OffHeapRegionEntryHelper.class })
public class OffHeapRegionEntryHelperJUnitTest {
private static final Long VALUE_IS_NOT_ENCODABLE = 0L;
@@ -75,13 +75,13 @@ public class OffHeapRegionEntryHelperJUnitTest {
SimpleMemoryAllocatorImpl.freeOffHeapMemory();
}
- private ObjectStoredInMemory createChunk(Object value) {
+ private OffHeapStoredObject createChunk(Object value) {
byte[] v = EntryEventImpl.serialize(value);
boolean isSerialized = true;
boolean isCompressed = false;
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(v, isSerialized, isCompressed);
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(v, isSerialized, isCompressed);
return chunk;
}
@@ -294,7 +294,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
@Test
public void isOffHeapShouldReturnTrueIfAddressIsOnOffHeap() {
- ObjectStoredInMemory value = createChunk(Long.MAX_VALUE);
+ OffHeapStoredObject value = createChunk(Long.MAX_VALUE);
assertThat(OffHeapRegionEntryHelper.isOffHeap(value.getAddress())).isTrue();
}
@@ -327,7 +327,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
long oldAddress = 1L;
// testing when the newValue is a chunk
- ObjectStoredInMemory newValue = createChunk(Long.MAX_VALUE);
+ OffHeapStoredObject newValue = createChunk(Long.MAX_VALUE);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(oldAddress);
when(re.setAddress(oldAddress, newValue.getAddress())).thenReturn(Boolean.TRUE);
@@ -440,13 +440,13 @@ public class OffHeapRegionEntryHelperJUnitTest {
// mock region entry
OffHeapRegionEntry re = mock(OffHeapRegionEntry.class);
- ObjectStoredInMemory oldValue = createChunk(Long.MAX_VALUE);
- ObjectStoredInMemory newValue = createChunk(Long.MAX_VALUE - 1);
+ OffHeapStoredObject oldValue = createChunk(Long.MAX_VALUE);
+ OffHeapStoredObject newValue = createChunk(Long.MAX_VALUE - 1);
// mock Chunk static methods - in-order to verify that release is called
- PowerMockito.spy(ObjectStoredInMemory.class);
- PowerMockito.doNothing().when(ObjectStoredInMemory.class);
- ObjectStoredInMemory.release(oldValue.getAddress());
+ PowerMockito.spy(OffHeapStoredObject.class);
+ PowerMockito.doNothing().when(OffHeapStoredObject.class);
+ OffHeapStoredObject.release(oldValue.getAddress());
// mock region entry methods required for test
when(re.getAddress()).thenReturn(oldValue.getAddress());
@@ -460,7 +460,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
// verify oldAddress is released
PowerMockito.verifyStatic();
- ObjectStoredInMemory.release(oldValue.getAddress());
+ OffHeapStoredObject.release(oldValue.getAddress());
}
@Test
@@ -475,9 +475,9 @@ public class OffHeapRegionEntryHelperJUnitTest {
TinyStoredObject newAddress = new TinyStoredObject(OffHeapRegionEntryHelper.encodeDataAsAddress(newData, false, false));
// mock Chunk static methods - in-order to verify that release is never called
- PowerMockito.spy(ObjectStoredInMemory.class);
- PowerMockito.doNothing().when(ObjectStoredInMemory.class);
- ObjectStoredInMemory.release(oldAddress);
+ PowerMockito.spy(OffHeapStoredObject.class);
+ PowerMockito.doNothing().when(OffHeapStoredObject.class);
+ OffHeapStoredObject.release(oldAddress);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(oldAddress);
@@ -491,7 +491,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
// verify that release is never called as the old address is not on offheap
PowerMockito.verifyStatic(never());
- ObjectStoredInMemory.release(oldAddress);
+ OffHeapStoredObject.release(oldAddress);
}
@Test
@@ -505,9 +505,9 @@ public class OffHeapRegionEntryHelperJUnitTest {
long newAddress = OffHeapRegionEntryHelper.REMOVED_PHASE2_ADDRESS;
// mock Chunk static methods - in-order to verify that release is never called
- PowerMockito.spy(ObjectStoredInMemory.class);
- PowerMockito.doNothing().when(ObjectStoredInMemory.class);
- ObjectStoredInMemory.release(oldAddress);
+ PowerMockito.spy(OffHeapStoredObject.class);
+ PowerMockito.doNothing().when(OffHeapStoredObject.class);
+ OffHeapStoredObject.release(oldAddress);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(oldAddress);
@@ -521,7 +521,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
// verify that release is never called as the old address is not on offheap
PowerMockito.verifyStatic(never());
- ObjectStoredInMemory.release(oldAddress);
+ OffHeapStoredObject.release(oldAddress);
}
@Test(expected = IllegalStateException.class)
@@ -541,7 +541,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
// mock region entry
OffHeapRegionEntry re = mock(OffHeapRegionEntry.class);
- ObjectStoredInMemory chunk = createChunk(Long.MAX_VALUE);
+ OffHeapStoredObject chunk = createChunk(Long.MAX_VALUE);
// mock region entry methods required for test
when(re.getAddress()).thenReturn(chunk.getAddress());
@@ -627,10 +627,10 @@ public class OffHeapRegionEntryHelperJUnitTest {
@Test
public void addressToObjectShouldReturnValueFromChunk() {
- ObjectStoredInMemory expected = createChunk(Long.MAX_VALUE);
+ OffHeapStoredObject expected = createChunk(Long.MAX_VALUE);
Object actual = OffHeapRegionEntryHelper.addressToObject(expected.getAddress(), false, null);
- assertThat(actual).isInstanceOf(ObjectStoredInMemory.class);
+ assertThat(actual).isInstanceOf(OffHeapStoredObject.class);
assertThat(actual).isEqualTo(expected);
}
@@ -640,7 +640,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
boolean isSerialized = true;
boolean isCompressed = true;
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(data, isSerialized, isCompressed);
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(data, isSerialized, isCompressed);
// create the mock context
RegionEntryContext regionContext = mock(RegionEntryContext.class);
@@ -669,7 +669,7 @@ public class OffHeapRegionEntryHelperJUnitTest {
boolean isSerialized = false;
boolean isCompressed = true;
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) ma.allocateAndInitialize(data, isSerialized, isCompressed);
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(data, isSerialized, isCompressed);
// create the mock context
RegionEntryContext regionContext = mock(RegionEntryContext.class);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java
new file mode 100644
index 0000000..c0ec983
--- /dev/null
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapStoredObjectJUnitTest.java
@@ -0,0 +1,869 @@
+/*
+ * 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.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import com.gemstone.gemfire.LogWriter;
+import com.gemstone.gemfire.compression.Compressor;
+import com.gemstone.gemfire.internal.DSCODE;
+import com.gemstone.gemfire.internal.HeapDataOutputStream;
+import com.gemstone.gemfire.internal.Version;
+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.MemoryBlock.State;
+import com.gemstone.gemfire.test.junit.categories.UnitTest;
+
+@Category(UnitTest.class)
+public class OffHeapStoredObjectJUnitTest extends AbstractStoredObjectTestBase {
+
+ private MemoryAllocator ma;
+
+ static {
+ ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
+ }
+
+ @Before
+ public void setUp() {
+ OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
+ OffHeapMemoryStats stats = mock(OffHeapMemoryStats.class);
+ LogWriter lw = mock(LogWriter.class);
+
+ ma = SimpleMemoryAllocatorImpl.create(ooohml, stats, lw, 3, OffHeapStorage.MIN_SLAB_SIZE * 3, OffHeapStorage.MIN_SLAB_SIZE);
+ }
+
+ @After
+ public void tearDown() {
+ SimpleMemoryAllocatorImpl.freeOffHeapMemory();
+ }
+
+ @Override
+ public Object getValue() {
+ return Long.valueOf(Long.MAX_VALUE);
+ }
+
+ @Override
+ public byte[] getValueAsByteArray() {
+ return convertValueToByteArray(getValue());
+ }
+
+ private byte[] convertValueToByteArray(Object value) {
+ return ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong((Long) value).array();
+ }
+
+ @Override
+ public Object convertByteArrayToObject(byte[] valueInByteArray) {
+ return ByteBuffer.wrap(valueInByteArray).getLong();
+ }
+
+ @Override
+ public Object convertSerializedByteArrayToObject(byte[] valueInSerializedByteArray) {
+ return EntryEventImpl.deserialize(valueInSerializedByteArray);
+ }
+
+ @Override
+ public OffHeapStoredObject createValueAsUnserializedStoredObject(Object value) {
+ byte[] valueInByteArray;
+ if (value instanceof Long) {
+ valueInByteArray = convertValueToByteArray(value);
+ } else {
+ valueInByteArray = (byte[]) value;
+ }
+
+ boolean isSerialized = false;
+ boolean isCompressed = false;
+
+ return createChunk(valueInByteArray, isSerialized, isCompressed);
+ }
+
+ @Override
+ public OffHeapStoredObject createValueAsSerializedStoredObject(Object value) {
+ byte[] valueInSerializedByteArray = EntryEventImpl.serialize(value);
+
+ boolean isSerialized = true;
+ boolean isCompressed = false;
+
+ return createChunk(valueInSerializedByteArray, isSerialized, isCompressed);
+ }
+
+ private OffHeapStoredObject createChunk(byte[] v, boolean isSerialized, boolean isCompressed) {
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(v, isSerialized, isCompressed);
+ return chunk;
+ }
+
+ @Test
+ public void chunkCanBeCreatedFromAnotherChunk() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+
+ OffHeapStoredObject newChunk = new OffHeapStoredObject(chunk);
+
+ assertNotNull(newChunk);
+ assertThat(newChunk.getAddress()).isEqualTo(chunk.getAddress());
+
+ chunk.release();
+ }
+
+ @Test
+ public void chunkCanBeCreatedWithOnlyMemoryAddress() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+
+ OffHeapStoredObject newChunk = new OffHeapStoredObject(chunk.getAddress());
+
+ assertNotNull(newChunk);
+ assertThat(newChunk.getAddress()).isEqualTo(chunk.getAddress());
+
+ chunk.release();
+ }
+
+ @Test
+ public void chunkSliceCanBeCreatedFromAnotherChunk() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+
+ int position = 1;
+ int end = 2;
+
+ OffHeapStoredObject newChunk = (OffHeapStoredObject) chunk.slice(position, end);
+
+ assertNotNull(newChunk);
+ assertThat(newChunk.getClass()).isEqualTo(ObjectChunkSlice.class);
+ assertThat(newChunk.getAddress()).isEqualTo(chunk.getAddress());
+
+ chunk.release();
+ }
+
+ @Test
+ public void fillSerializedValueShouldFillWrapperWithSerializedValueIfValueIsSerialized() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ // mock the things
+ BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class);
+
+ byte userBits = 0;
+ byte serializedUserBits = 1;
+ chunk.fillSerializedValue(wrapper, userBits);
+
+ verify(wrapper, times(1)).setOffHeapData(chunk, serializedUserBits);
+
+ chunk.release();
+ }
+
+ @Test
+ public void fillSerializedValueShouldFillWrapperWithDeserializedValueIfValueIsNotSerialized() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+
+ // mock the things
+ BytesAndBitsForCompactor wrapper = mock(BytesAndBitsForCompactor.class);
+
+ byte userBits = 1;
+ chunk.fillSerializedValue(wrapper, userBits);
+
+ verify(wrapper, times(1)).setOffHeapData(chunk, userBits);
+
+ chunk.release();
+ }
+
+ @Test
+ public void getShortClassNameShouldReturnShortClassName() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ assertThat(chunk.getShortClassName()).isEqualTo("ObjectStoredInMemory");
+
+ chunk.release();
+ }
+
+ @Test
+ public void chunksAreEqualsOnlyByAddress() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ OffHeapStoredObject newChunk = new OffHeapStoredObject(chunk.getAddress());
+ assertThat(chunk.equals(newChunk)).isTrue();
+
+ OffHeapStoredObject chunkWithSameValue = createValueAsUnserializedStoredObject(getValue());
+ assertThat(chunk.equals(chunkWithSameValue)).isFalse();
+
+ Object someObject = getValue();
+ assertThat(chunk.equals(someObject)).isFalse();
+
+ chunk.release();
+ chunkWithSameValue.release();
+ }
+
+ @Test
+ public void chunksShouldBeComparedBySize() {
+ OffHeapStoredObject chunk1 = createValueAsSerializedStoredObject(getValue());
+
+ OffHeapStoredObject chunk2 = chunk1;
+ assertThat(chunk1.compareTo(chunk2)).isEqualTo(0);
+
+ OffHeapStoredObject chunkWithSameValue = createValueAsSerializedStoredObject(getValue());
+ assertThat(chunk1.compareTo(chunkWithSameValue)).isEqualTo(Long.signum(chunk1.getAddress() - chunkWithSameValue.getAddress()));
+
+ OffHeapStoredObject chunk3 = createValueAsSerializedStoredObject(Long.MAX_VALUE);
+ OffHeapStoredObject chunk4 = createValueAsSerializedStoredObject(Long.MAX_VALUE);
+
+ int newSizeForChunk3 = 2;
+ int newSizeForChunk4 = 3;
+
+ assertThat(chunk3.compareTo(chunk4)).isEqualTo(Integer.signum(newSizeForChunk3 - newSizeForChunk4));
+
+ chunk1.release();
+ chunk4.release();
+ }
+
+ @Test
+ public void setSerializedShouldSetTheSerializedBit() {
+ Object regionEntryValue = getValue();
+ byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
+
+ boolean isSerialized = false;
+ boolean isCompressed = false;
+
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
+
+ int headerBeforeSerializedBitSet = AddressableMemoryManager.readIntVolatile(chunk.getAddress() + OffHeapStoredObject.REF_COUNT_OFFSET);
+
+ assertThat(chunk.isSerialized()).isFalse();
+
+ chunk.setSerialized(true); // set to true
+
+ assertThat(chunk.isSerialized()).isTrue();
+
+ int headerAfterSerializedBitSet = AddressableMemoryManager.readIntVolatile(chunk.getAddress() + OffHeapStoredObject.REF_COUNT_OFFSET);
+
+ assertThat(headerAfterSerializedBitSet).isEqualTo(headerBeforeSerializedBitSet | OffHeapStoredObject.IS_SERIALIZED_BIT);
+
+ chunk.release();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void setSerialziedShouldThrowExceptionIfChunkIsAlreadyReleased() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.release();
+ chunk.setSerialized(true);
+
+ chunk.release();
+ }
+
+ @Test
+ public void setCompressedShouldSetTheCompressedBit() {
+ Object regionEntryValue = getValue();
+ byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
+
+ boolean isSerialized = false;
+ boolean isCompressed = false;
+
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
+
+ int headerBeforeCompressedBitSet = AddressableMemoryManager.readIntVolatile(chunk.getAddress() + OffHeapStoredObject.REF_COUNT_OFFSET);
+
+ assertThat(chunk.isCompressed()).isFalse();
+
+ chunk.setCompressed(true); // set to true
+
+ assertThat(chunk.isCompressed()).isTrue();
+
+ int headerAfterCompressedBitSet = AddressableMemoryManager.readIntVolatile(chunk.getAddress() + OffHeapStoredObject.REF_COUNT_OFFSET);
+
+ assertThat(headerAfterCompressedBitSet).isEqualTo(headerBeforeCompressedBitSet | OffHeapStoredObject.IS_COMPRESSED_BIT);
+
+ chunk.release();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void setCompressedShouldThrowExceptionIfChunkIsAlreadyReleased() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.release();
+ chunk.setCompressed(true);
+
+ chunk.release();
+ }
+
+ @Test
+ public void setDataSizeShouldSetTheDataSizeBits() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+
+ int beforeSize = chunk.getDataSize();
+
+ chunk.setDataSize(2);
+
+ int afterSize = chunk.getDataSize();
+
+ assertThat(afterSize).isEqualTo(2);
+ assertThat(afterSize).isNotEqualTo(beforeSize);
+
+ chunk.release();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void setDataSizeShouldThrowExceptionIfChunkIsAlreadyReleased() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.release();
+ chunk.setDataSize(1);
+
+ chunk.release();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void initializeUseCountShouldThrowIllegalStateExceptionIfChunkIsAlreadyRetained() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.retain();
+ chunk.initializeUseCount();
+
+ chunk.release();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void initializeUseCountShouldThrowIllegalStateExceptionIfChunkIsAlreadyReleased() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.release();
+ chunk.initializeUseCount();
+
+ chunk.release();
+ }
+
+ @Test
+ public void isSerializedPdxInstanceShouldReturnTrueIfItsPDXInstance() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ byte[] serailizedValue = chunk.getSerializedValue();
+ serailizedValue[0] = DSCODE.PDX;
+ chunk.setSerializedValue(serailizedValue);
+
+ assertThat(chunk.isSerializedPdxInstance()).isTrue();
+
+ serailizedValue = chunk.getSerializedValue();
+ serailizedValue[0] = DSCODE.PDX_ENUM;
+ chunk.setSerializedValue(serailizedValue);
+
+ assertThat(chunk.isSerializedPdxInstance()).isTrue();
+
+ serailizedValue = chunk.getSerializedValue();
+ serailizedValue[0] = DSCODE.PDX_INLINE_ENUM;
+ chunk.setSerializedValue(serailizedValue);
+
+ assertThat(chunk.isSerializedPdxInstance()).isTrue();
+
+ chunk.release();
+ }
+
+ @Test
+ public void isSerializedPdxInstanceShouldReturnFalseIfItsNotPDXInstance() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ assertThat(chunk.isSerializedPdxInstance()).isFalse();
+
+ chunk.release();
+ }
+
+ @Test
+ public void checkDataEqualsByChunk() {
+ OffHeapStoredObject chunk1 = createValueAsSerializedStoredObject(getValue());
+ OffHeapStoredObject sameAsChunk1 = chunk1;
+
+ assertThat(chunk1.checkDataEquals(sameAsChunk1)).isTrue();
+
+ OffHeapStoredObject unserializedChunk = createValueAsUnserializedStoredObject(getValue());
+ assertThat(chunk1.checkDataEquals(unserializedChunk)).isFalse();
+
+ OffHeapStoredObject chunkDifferBySize = createValueAsSerializedStoredObject(getValue());
+ chunkDifferBySize.setSize(0);
+ assertThat(chunk1.checkDataEquals(chunkDifferBySize)).isFalse();
+
+ OffHeapStoredObject chunkDifferByValue = createValueAsSerializedStoredObject(Long.MAX_VALUE - 1);
+ assertThat(chunk1.checkDataEquals(chunkDifferByValue)).isFalse();
+
+ OffHeapStoredObject newChunk1 = createValueAsSerializedStoredObject(getValue());
+ assertThat(chunk1.checkDataEquals(newChunk1)).isTrue();
+
+ chunk1.release();
+ unserializedChunk.release();
+ chunkDifferBySize.release();
+ chunkDifferByValue.release();
+ newChunk1.release();
+ }
+
+ @Test
+ public void checkDataEqualsBySerializedValue() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ assertThat(chunk.checkDataEquals(new byte[1])).isFalse();
+
+ OffHeapStoredObject chunkDifferByValue = createValueAsSerializedStoredObject(Long.MAX_VALUE - 1);
+ assertThat(chunk.checkDataEquals(chunkDifferByValue.getSerializedValue())).isFalse();
+
+ OffHeapStoredObject newChunk = createValueAsSerializedStoredObject(getValue());
+ assertThat(chunk.checkDataEquals(newChunk.getSerializedValue())).isTrue();
+
+ chunk.release();
+ chunkDifferByValue.release();
+ newChunk.release();
+ }
+
+ @Test
+ public void getDecompressedBytesShouldReturnDecompressedBytesIfCompressed() {
+ Object regionEntryValue = getValue();
+ byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
+
+ boolean isSerialized = true;
+ boolean isCompressed = true;
+
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
+
+ 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 = chunk.getDecompressedBytes(regionContext);
+
+ // verify the thing happened
+ verify(cacheStats, atLeastOnce()).startDecompression();
+ verify(compressor, times(1)).decompress(regionEntryValueAsBytes);
+ verify(cacheStats, atLeastOnce()).endDecompression(startTime);
+
+ assertArrayEquals(regionEntryValueAsBytes, bytes);
+
+ chunk.release();
+ }
+
+ @Test
+ public void incSizeShouldIncrementSize() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ int beforeSize = chunk.getSize();
+
+ chunk.incSize(1);
+ assertThat(chunk.getSize()).isEqualTo(beforeSize + 1);
+
+ chunk.incSize(2);
+ assertThat(chunk.getSize()).isEqualTo(beforeSize + 1 + 2);
+
+ chunk.release();
+ }
+
+ @Test
+ public void readyForFreeShouldResetTheRefCount() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ int refCountBeforeFreeing = chunk.getRefCount();
+ assertThat(refCountBeforeFreeing).isEqualTo(1);
+
+ chunk.readyForFree();
+
+ int refCountAfterFreeing = chunk.getRefCount();
+ assertThat(refCountAfterFreeing).isEqualTo(0);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void readyForAllocationShouldThrowExceptionIfAlreadyAllocated() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ // chunk is already allocated when we created it, so calling readyForAllocation should throw exception.
+ chunk.readyForAllocation();
+
+ chunk.release();
+ }
+
+ @Test
+ public void checkIsAllocatedShouldReturnIfAllocated() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ chunk.checkIsAllocated();
+
+ chunk.release();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void checkIsAllocatedShouldThrowExceptionIfNotAllocated() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ chunk.release();
+ chunk.checkIsAllocated();
+
+ chunk.release();
+ }
+
+ @Test
+ public void sendToShouldWriteSerializedValueToDataOutputIfValueIsSerialized() throws IOException {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ OffHeapStoredObject spyChunk = spy(chunk);
+
+ HeapDataOutputStream dataOutput = mock(HeapDataOutputStream.class);
+ ByteBuffer directByteBuffer = ByteBuffer.allocate(1024);
+
+ doReturn(directByteBuffer).when(spyChunk).createDirectByteBuffer();
+ doNothing().when(dataOutput).write(directByteBuffer);
+
+ spyChunk.sendTo(dataOutput);
+
+ verify(dataOutput, times(1)).write(directByteBuffer);
+
+ chunk.release();
+ }
+
+ @Test
+ public void sendToShouldWriteUnserializedValueToDataOutputIfValueIsUnserialized() throws IOException {
+ byte[] regionEntryValue = getValueAsByteArray();
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(regionEntryValue);
+
+ // writeByte is a final method and cannot be mocked, so creating a real one
+ HeapDataOutputStream dataOutput = new HeapDataOutputStream(Version.CURRENT);
+
+ chunk.sendTo(dataOutput);
+
+ byte[] actual = dataOutput.toByteArray();
+
+ byte[] expected = new byte[regionEntryValue.length + 2];
+ expected[0] = DSCODE.BYTE_ARRAY;
+ expected[1] = (byte) regionEntryValue.length;
+ System.arraycopy(regionEntryValue, 0, expected, 2, regionEntryValue.length);
+
+ assertNotNull(dataOutput);
+ assertThat(actual).isEqualTo(expected);
+
+ chunk.release();
+ }
+
+ @Test
+ public void sendAsByteArrayShouldWriteValueToDataOutput() throws IOException {
+ byte[] regionEntryValue = getValueAsByteArray();
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(regionEntryValue);
+
+ // writeByte is a final method and cannot be mocked, so creating a real one
+ HeapDataOutputStream dataOutput = new HeapDataOutputStream(Version.CURRENT);
+
+ chunk.sendAsByteArray(dataOutput);
+
+ byte[] actual = dataOutput.toByteArray();
+
+ byte[] expected = new byte[regionEntryValue.length + 1];
+ expected[0] = (byte) regionEntryValue.length;
+ System.arraycopy(regionEntryValue, 0, expected, 1, regionEntryValue.length);
+
+ assertNotNull(dataOutput);
+ assertThat(actual).isEqualTo(expected);
+
+ chunk.release();
+ }
+
+ @Test
+ public void createDirectByteBufferShouldCreateAByteBuffer() {
+ byte[] regionEntryValue = getValueAsByteArray();
+
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(regionEntryValue);
+
+ ByteBuffer buffer = chunk.createDirectByteBuffer();
+
+ byte[] actual = new byte[regionEntryValue.length];
+ buffer.get(actual);
+
+ assertArrayEquals(regionEntryValue, actual);
+
+ chunk.release();
+ }
+
+ @Test
+ public void getDirectByteBufferShouldCreateAByteBuffer() {
+ byte[] regionEntryValue = getValueAsByteArray();
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(regionEntryValue);
+
+ ByteBuffer buffer = chunk.createDirectByteBuffer();
+ long bufferAddress = AddressableMemoryManager.getDirectByteBufferAddress(buffer);
+
+ // returned address should be starting of the value (after skipping HEADER_SIZE bytes)
+ assertEquals(chunk.getAddress() + OffHeapStoredObject.HEADER_SIZE, bufferAddress);
+
+ chunk.release();
+ }
+
+ @Test(expected = AssertionError.class)
+ public void getAddressForReadingDataShouldFailIfItsOutsideOfChunk() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ chunk.getAddressForReadingData(0, chunk.getDataSize() + 1);
+
+ chunk.release();
+ }
+
+ @Test
+ public void getAddressForReadingDataShouldReturnDataAddressFromGivenOffset() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ int offset = 1;
+ long requestedAddress = chunk.getAddressForReadingData(offset, 1);
+
+ assertThat(requestedAddress).isEqualTo(chunk.getBaseDataAddress() + offset);
+
+ chunk.release();
+ }
+
+ @Test
+ public void getSizeInBytesShouldReturnSize() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ assertThat(chunk.getSizeInBytes()).isEqualTo(chunk.getSize());
+
+ chunk.release();
+ }
+
+ @Test(expected = AssertionError.class)
+ public void getAddressForReadingDataShouldFailIfOffsetIsNegative() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ chunk.getAddressForReadingData(-1, 1);
+
+ chunk.release();
+ }
+
+ @Test(expected = AssertionError.class)
+ public void getAddressForReadingDataShouldFailIfSizeIsNegative() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+ chunk.getAddressForReadingData(1, -1);
+
+ chunk.release();
+ }
+
+ @Test(expected = AssertionError.class)
+ public void readByteAndWriteByteShouldFailIfOffsetIsOutside() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ chunk.readDataByte(chunk.getDataSize() + 1);
+
+ chunk.writeDataByte(chunk.getDataSize() + 1, Byte.MAX_VALUE);
+
+ chunk.release();
+ }
+
+ @Test
+ public void writeByteShouldWriteAtCorrectLocation() {
+ OffHeapStoredObject chunk = createValueAsSerializedStoredObject(getValue());
+
+ byte valueBeforeWrite = chunk.readDataByte(2);
+
+ Byte expected = Byte.MAX_VALUE;
+ chunk.writeDataByte(2, expected);
+
+ Byte actual = chunk.readDataByte(2);
+
+ assertThat(actual).isNotEqualTo(valueBeforeWrite);
+ assertThat(actual).isEqualTo(expected);
+
+ chunk.release();
+ }
+
+ @Test
+ public void retainShouldIncrementRefCount() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ assertThat(chunk.getRefCount()).isEqualTo(1);
+
+ chunk.retain();
+ assertThat(chunk.getRefCount()).isEqualTo(2);
+
+ chunk.retain();
+ assertThat(chunk.getRefCount()).isEqualTo(3);
+
+ chunk.release();
+ chunk.release();
+ chunk.release();
+ boolean retainAfterRelease = chunk.retain();
+
+ assertThat(retainAfterRelease).isFalse();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void retainShouldThrowExceptionAfterMaxNumberOfTimesRetained() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+
+ // loop though and invoke retain for MAX_REF_COUNT-1 times, as create chunk above counted as one reference
+ for (int i = 0; i < OffHeapStoredObject.MAX_REF_COUNT - 1; i++)
+ chunk.retain();
+
+ // invoke for the one more time should throw exception
+ chunk.retain();
+ }
+
+ @Test
+ public void releaseShouldDecrementRefCount() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ assertThat(chunk.getRefCount()).isEqualTo(1);
+
+ chunk.retain();
+ chunk.retain();
+ assertThat(chunk.getRefCount()).isEqualTo(3);
+
+ chunk.release();
+ assertThat(chunk.getRefCount()).isEqualTo(2);
+
+ chunk.release();
+ assertThat(chunk.getRefCount()).isEqualTo(1);
+
+ chunk.retain();
+ chunk.release();
+ assertThat(chunk.getRefCount()).isEqualTo(1);
+
+ chunk.release();
+ assertThat(chunk.getRefCount()).isEqualTo(0);
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void releaseShouldThrowExceptionIfChunkIsAlreadyReleased() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.release();
+ chunk.release();
+ }
+
+ @Test
+ public void testToString() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+
+ String expected = ":<dataSize=" + chunk.getDataSize() + " refCount=" + chunk.getRefCount() + " addr=" + Long.toHexString(chunk.getAddress()) + ">";
+ assertThat(chunk.toString()).endsWith(expected);
+
+ chunk.release();
+ }
+
+ @Test
+ public void getStateShouldReturnAllocatedIfRefCountIsGreaterThanZero() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ assertEquals(State.ALLOCATED, chunk.getState());
+
+ chunk.release();
+ }
+
+ @Test
+ public void getStateShouldReturnDeallocatedIfRefCountIsZero() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.release();
+ assertEquals(State.DEALLOCATED, chunk.getState());
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void getNextBlockShouldThrowUnSupportedOperationException() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.getNextBlock();
+
+ chunk.release();
+ }
+
+ @Test
+ public void getBlockSizeShouldBeSameSameGetSize() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ assertEquals(chunk.getSize(), chunk.getBlockSize());
+
+ chunk.release();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void getSlabIdShouldThrowUnSupportedOperationException() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ chunk.getSlabId();
+
+ chunk.release();
+ }
+
+ @Test
+ public void getFreeListIdShouldReturnMinusOne() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ assertThat(chunk.getFreeListId()).isEqualTo(-1);
+
+ chunk.release();
+ }
+
+ @Test
+ public void getDataTypeShouldReturnNull() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ assertThat(chunk.getDataType()).isNull();
+
+ chunk.release();
+ }
+
+ @Test
+ public void getDataDataShouldReturnNull() {
+ OffHeapStoredObject chunk = createValueAsUnserializedStoredObject(getValue());
+ assertThat(chunk.getDataValue()).isNull();
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void getRawBytesShouldThrowExceptionIfValueIsCompressed() {
+ Object regionEntryValue = getValue();
+ byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
+
+ boolean isSerialized = true;
+ boolean isCompressed = true;
+
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
+
+ chunk.getRawBytes();
+
+ chunk.release();
+ }
+
+ @Test
+ public void getSerializedValueShouldSerializeTheValue() {
+ Object regionEntryValue = getValue();
+ byte[] regionEntryValueAsBytes = convertValueToByteArray(regionEntryValue);
+
+ boolean isSerialized = false;
+ boolean isCompressed = false;
+
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(regionEntryValueAsBytes, isSerialized, isCompressed);
+
+ byte[] serializedValue = chunk.getSerializedValue();
+
+ assertThat(serializedValue).isEqualTo(EntryEventImpl.serialize(regionEntryValueAsBytes));
+
+ chunk.release();
+ }
+
+ @Test
+ public void fillShouldFillTheChunk() {
+ boolean isSerialized = false;
+ boolean isCompressed = false;
+
+ OffHeapStoredObject chunk = (OffHeapStoredObject) ma.allocateAndInitialize(new byte[100], isSerialized, isCompressed);
+
+ // first fill the unused part with FILL_PATTERN
+ OffHeapStoredObject.fill(chunk.getAddress());
+
+ // Validate that it is filled
+ chunk.validateFill();
+
+ chunk.release();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java
index 49cf900..f8a5c8e 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapValidationJUnitTest.java
@@ -296,8 +296,8 @@ public class OffHeapValidationJUnitTest {
private long getMemoryAddress(Region region, String key) {
Object entry = ((LocalRegion) region).getRegionEntry(key)._getValue();
- assertTrue(entry instanceof ObjectStoredInMemory);
- long memoryAddress = ((ObjectStoredInMemory)entry).getAddress();
+ assertTrue(entry instanceof OffHeapStoredObject);
+ long memoryAddress = ((OffHeapStoredObject)entry).getAddress();
assertTrue(memoryAddress > 0);
return memoryAddress;
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
index eb60d1d..7157eaa 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OffHeapWriteObjectAsByteArrayJUnitTest.java
@@ -64,7 +64,7 @@ public class OffHeapWriteObjectAsByteArrayJUnitTest {
public void testByteArrayChunk() throws IOException, ClassNotFoundException {
byte[] expected = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
StoredObject so = createStoredObject(expected, false, false);
- assertTrue(so instanceof ObjectStoredInMemory);
+ assertTrue(so instanceof OffHeapStoredObject);
HeapDataOutputStream hdos = new HeapDataOutputStream(new byte[1024]);
DataSerializer.writeObjectAsByteArray(so, hdos);
DataInputStream in = createInput(hdos);
@@ -88,7 +88,7 @@ public class OffHeapWriteObjectAsByteArrayJUnitTest {
public void testStringChunk() throws IOException, ClassNotFoundException {
byte[] expected = EntryEventImpl.serialize("1234567890");
StoredObject so = createStoredObject(expected, true, false);
- assertTrue(so instanceof ObjectStoredInMemory);
+ assertTrue(so instanceof OffHeapStoredObject);
HeapDataOutputStream hdos = new HeapDataOutputStream(new byte[1024]);
DataSerializer.writeObjectAsByteArray(so, hdos);
DataInputStream in = createInput(hdos);
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OldFreeListOffHeapRegionJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OldFreeListOffHeapRegionJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OldFreeListOffHeapRegionJUnitTest.java
index 8fecf8a..e9ca59d 100755
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OldFreeListOffHeapRegionJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/OldFreeListOffHeapRegionJUnitTest.java
@@ -41,7 +41,7 @@ public class OldFreeListOffHeapRegionJUnitTest extends OffHeapRegionBase {
@Override
public int perObjectOverhead() {
- return ObjectStoredInMemory.HEADER_SIZE;
+ return OffHeapStoredObject.HEADER_SIZE;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java
index c9415c6..51bc0a2 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternIntegrationTest.java
@@ -163,7 +163,7 @@ public class SimpleMemoryAllocatorFillPatternIntegrationTest {
private int totalAllocation = 0;
// List of Chunks allocated by this thread
- private List<ObjectStoredInMemory> chunks = new LinkedList<ObjectStoredInMemory>();
+ private List<OffHeapStoredObject> chunks = new LinkedList<OffHeapStoredObject>();
// Time to end thread execution
private long endTime = System.currentTimeMillis() + RUN_TIME_IN_MILLIS;
@@ -173,7 +173,7 @@ public class SimpleMemoryAllocatorFillPatternIntegrationTest {
*/
private void allocate() {
int allocation = chunkSizer.allocationSize();
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) allocator.allocate(allocation);
+ OffHeapStoredObject chunk = (OffHeapStoredObject) allocator.allocate(allocation);
// This should always work just after allocation
chunk.validateFill();
@@ -186,7 +186,7 @@ public class SimpleMemoryAllocatorFillPatternIntegrationTest {
* Frees a random chunk from the Chunk list.
*/
private void free() {
- ObjectStoredInMemory chunk = chunks.remove(random.nextInt(chunks.size()));
+ OffHeapStoredObject chunk = chunks.remove(random.nextInt(chunks.size()));
totalAllocation -= chunk.getSize();
/*
@@ -200,7 +200,7 @@ public class SimpleMemoryAllocatorFillPatternIntegrationTest {
* Writes canned data to a random Chunk from the Chunk list.
*/
private void write() {
- ObjectStoredInMemory chunk = chunks.get(random.nextInt(chunks.size()));
+ OffHeapStoredObject chunk = chunks.get(random.nextInt(chunks.size()));
chunk.writeDataBytes(0, WRITE_BYTES);
}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/b6eca046/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java
----------------------------------------------------------------------
diff --git a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java
index d28f989..c61f2f4 100644
--- a/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java
+++ b/geode-core/src/test/java/com/gemstone/gemfire/internal/offheap/SimpleMemoryAllocatorFillPatternJUnitTest.java
@@ -101,7 +101,7 @@ public class SimpleMemoryAllocatorFillPatternJUnitTest {
* Pull a chunk off the fragment. This will have no fill because
* it is a "fresh" chunk.
*/
- ObjectStoredInMemory chunk = (ObjectStoredInMemory) this.allocator.allocate(chunkSize);
+ OffHeapStoredObject chunk = (OffHeapStoredObject) this.allocator.allocate(chunkSize);
/*
* Chunk should have valid fill from initial fragment allocation.
@@ -109,7 +109,7 @@ public class SimpleMemoryAllocatorFillPatternJUnitTest {
chunk.validateFill();
// "Dirty" the chunk so the release has something to fill over
- chunk.writeDataBytes(ObjectStoredInMemory.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
+ chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
// This should free the Chunk (ref count == 1)
chunk.release();
@@ -118,20 +118,20 @@ public class SimpleMemoryAllocatorFillPatternJUnitTest {
* This chunk should have a fill because it was reused from the
* free list (assuming no fragmentation at this point...)
*/
- chunk = (ObjectStoredInMemory) this.allocator.allocate(chunkSize);
+ chunk = (OffHeapStoredObject) this.allocator.allocate(chunkSize);
// Make sure we have a fill this time
chunk.validateFill();
// Give the fill code something to write over during the release
- chunk.writeDataBytes(ObjectStoredInMemory.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
+ chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
chunk.release();
// Again, make sure the release implemented the fill
chunk.validateFill();
// "Dirty up" the free chunk
- chunk.writeDataBytes(ObjectStoredInMemory.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
+ chunk.writeDataBytes(OffHeapStoredObject.MIN_CHUNK_SIZE + 1, WRITE_BYTES);
catchException(chunk).validateFill();
assertTrue(caughtException() instanceof IllegalStateException);
@@ -149,14 +149,14 @@ public class SimpleMemoryAllocatorFillPatternJUnitTest {
/*
* Stores our allocated memory.
*/
- ObjectStoredInMemory[] allocatedChunks = new ObjectStoredInMemory[COMPACTION_CHUNKS];
+ OffHeapStoredObject[] allocatedChunks = new OffHeapStoredObject[COMPACTION_CHUNKS];
/*
* Use up most of our memory
* Our memory looks like [ ][ ][ ]
*/
for(int i =0;i < allocatedChunks.length;++i) {
- allocatedChunks[i] = (ObjectStoredInMemory) this.allocator.allocate(COMPACTION_CHUNK_SIZE);
+ allocatedChunks[i] = (OffHeapStoredObject) this.allocator.allocate(COMPACTION_CHUNK_SIZE);
allocatedChunks[i].validateFill();
}
@@ -173,7 +173,7 @@ public class SimpleMemoryAllocatorFillPatternJUnitTest {
* our initial chunks. This should force a compaction causing our
* memory to look like [ ][ ].
*/
- ObjectStoredInMemory slightlyLargerChunk = (ObjectStoredInMemory) this.allocator.allocate(FORCE_COMPACTION_CHUNK_SIZE);
+ OffHeapStoredObject slightlyLargerChunk = (OffHeapStoredObject) this.allocator.allocate(FORCE_COMPACTION_CHUNK_SIZE);
/*
* Make sure the compacted memory has the fill validation.