Skip to content

Commit

Permalink
[hotfix] Minor refactoring of OperationsOnFreedSegmentTest
Browse files Browse the repository at this point in the history
  • Loading branch information
azagrebin committed Oct 22, 2019
1 parent 8e0b35c commit d154ae5
Showing 1 changed file with 27 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.nio.ByteBuffer;
import java.util.function.BiConsumer;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
Expand All @@ -41,9 +42,9 @@ public class OperationsOnFreedSegmentTest {

@Test
public void testSingleSegmentOperationsHeapSegment() throws Exception {
testOpsOnFreedSegment(new HeapMemorySegment(new byte[PAGE_SIZE]));
testOpsOnFreedSegment(MemorySegmentFactory.wrap(new byte[PAGE_SIZE]));
testOpsOnFreedSegment(MemorySegmentFactory.allocateUnpooledOffHeapMemory(PAGE_SIZE));
for (MemorySegment segment : createTestSegments()) {
testOpsOnFreedSegment(segment);
}
}

@Test
Expand Down Expand Up @@ -86,78 +87,51 @@ public void testCompare() {

@Test
public void testCopyTo() {
MemorySegment aliveHeap = new HeapMemorySegment(new byte[PAGE_SIZE]);
MemorySegment aliveHybridHeap = MemorySegmentFactory.wrap(new byte[PAGE_SIZE]);
MemorySegment aliveHybridOffHeap = MemorySegmentFactory.allocateUnpooledOffHeapMemory(PAGE_SIZE);
testAliveVsFree(this::testCopy);
}

MemorySegment freedHeap = new HeapMemorySegment(new byte[PAGE_SIZE]);
MemorySegment freedHybridHeap = MemorySegmentFactory.wrap(new byte[PAGE_SIZE]);
MemorySegment freedHybridOffHeap = MemorySegmentFactory.allocateUnpooledOffHeapMemory(PAGE_SIZE);
freedHeap.free();
freedHybridHeap.free();
freedHybridOffHeap.free();
@Test
public void testSwap() {
testAliveVsFree(this::testSwap);
}

MemorySegment[] alive = { aliveHeap, aliveHybridHeap, aliveHybridOffHeap };
MemorySegment[] free = { freedHeap, freedHybridHeap, freedHybridOffHeap };
private static void testAliveVsFree(BiConsumer<MemorySegment, MemorySegment> testOperation) {
MemorySegment[] alive = createTestSegments();
MemorySegment[] free = createTestSegments();
for (MemorySegment segment : free) {
segment.free();
}

// alive with free
for (MemorySegment seg1 : alive) {
for (MemorySegment seg2 : free) {
testCopy(seg1, seg2);
testOperation.accept(seg1, seg2);
}
}

// free with alive
for (MemorySegment seg1 : free) {
for (MemorySegment seg2 : alive) {
testCopy(seg1, seg2);
testOperation.accept(seg1, seg2);
}
}

// free with free
for (MemorySegment seg1 : free) {
for (MemorySegment seg2 : free) {
testCopy(seg1, seg2);
testOperation.accept(seg1, seg2);
}
}
}

@Test
public void testSwap() {
MemorySegment aliveHeap = new HeapMemorySegment(new byte[PAGE_SIZE]);
MemorySegment aliveHybridHeap = MemorySegmentFactory.wrap(new byte[PAGE_SIZE]);
MemorySegment aliveHybridOffHeap = MemorySegmentFactory.allocateUnpooledOffHeapMemory(PAGE_SIZE);

MemorySegment freedHeap = new HeapMemorySegment(new byte[PAGE_SIZE]);
MemorySegment freedHybridHeap = MemorySegmentFactory.wrap(new byte[PAGE_SIZE]);
MemorySegment freedHybridOffHeap = MemorySegmentFactory.allocateUnpooledOffHeapMemory(PAGE_SIZE);
freedHeap.free();
freedHybridHeap.free();
freedHybridOffHeap.free();

MemorySegment[] alive = { aliveHeap, aliveHybridHeap, aliveHybridOffHeap };
MemorySegment[] free = { freedHeap, freedHybridHeap, freedHybridOffHeap };

// alive with free
for (MemorySegment seg1 : alive) {
for (MemorySegment seg2 : free) {
testSwap(seg1, seg2);
}
}
private static MemorySegment[] createTestSegments() {
MemorySegment heap = new HeapMemorySegment(new byte[PAGE_SIZE]);
MemorySegment hybridHeap = MemorySegmentFactory.wrap(new byte[PAGE_SIZE]);
MemorySegment hybridOffHeap = MemorySegmentFactory.allocateUnpooledOffHeapMemory(PAGE_SIZE);

// free with alive
for (MemorySegment seg1 : free) {
for (MemorySegment seg2 : alive) {
testSwap(seg1, seg2);
}
}
MemorySegment[] segments = { heap, hybridHeap, hybridOffHeap };

// free with free
for (MemorySegment seg1 : free) {
for (MemorySegment seg2 : free) {
testSwap(seg1, seg2);
}
}
return segments;
}

private void testOpsOnFreedSegment(MemorySegment segment) throws Exception {
Expand Down Expand Up @@ -943,8 +917,8 @@ private void testOpsOnFreedSegment(MemorySegment segment) throws Exception {
// --------- ByteBuffer -----------

for (ByteBuffer bbuf : new ByteBuffer[] {
ByteBuffer.allocate(55),
ByteBuffer.allocateDirect(55) }) {
ByteBuffer.allocate(55),
ByteBuffer.allocateDirect(55) }) {

try {
segment.get(0, bbuf, 17);
Expand Down

0 comments on commit d154ae5

Please sign in to comment.