Skip to content

Commit c165b1d

Browse files
author
Johnu George
committed
MNEMONIC-105: Adding Testcases for durable buffers and chunks
1 parent 7a8c80f commit c165b1d

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

mnemonic-collections/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<artifactId>mnemonic-core</artifactId>
3939
<version>${project.version}</version>
4040
</dependency>
41+
<dependency>
42+
<groupId>org.apache.commons</groupId>
43+
<artifactId>commons-lang3</artifactId>
44+
</dependency>
4145
<dependency>
4246
<groupId>org.flowcomputing.commons</groupId>
4347
<artifactId>commons-primitives</artifactId>

mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableHashMapNGTest.java

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,29 @@
1919

2020
import java.util.Iterator;
2121
import java.nio.ByteBuffer;
22+
import java.util.Random;
23+
import java.util.zip.Checksum;
24+
import java.util.zip.CRC32;
2225

2326
import org.apache.mnemonic.Utils;
2427
import org.apache.mnemonic.RestorableAllocator;
2528
import org.apache.mnemonic.NonVolatileMemAllocator;
29+
import org.apache.mnemonic.OutOfHybridMemory;
2630
import org.apache.mnemonic.EntityFactoryProxy;
2731
import org.apache.mnemonic.DurableType;
32+
import org.apache.mnemonic.DurableBuffer;
33+
import org.apache.mnemonic.DurableChunk;
2834
import org.apache.mnemonic.Reclaim;
2935
import org.apache.mnemonic.Durable;
3036
import org.apache.commons.lang3.tuple.Pair;
37+
import org.apache.commons.lang3.RandomUtils;
3138
import org.testng.annotations.AfterClass;
3239
import org.testng.annotations.BeforeClass;
3340
import org.testng.annotations.Test;
3441
import org.testng.AssertJUnit;
42+
import org.testng.Assert;
43+
44+
import sun.misc.Unsafe;
3545

3646
/**
3747
*
@@ -41,10 +51,49 @@
4151
public class DurableHashMapNGTest {
4252
private long cKEYCAPACITY;
4353
private NonVolatileMemAllocator m_act;
54+
private Random rand;
55+
private Unsafe unsafe;
4456
private long mInitialCapacity = 1;
4557

58+
protected DurableBuffer<NonVolatileMemAllocator>
59+
genuptBuffer(NonVolatileMemAllocator act, Checksum cs, int size) {
60+
DurableBuffer<NonVolatileMemAllocator> ret = null;
61+
ret = act.createBuffer(size, false);
62+
if (null == ret) {
63+
throw new OutOfHybridMemory("Create Durable Buffer Failed.");
64+
}
65+
ret.get().clear();
66+
byte[] rdbytes = RandomUtils.nextBytes(size);
67+
Assert.assertNotNull(rdbytes);
68+
ret.get().put(rdbytes);
69+
cs.update(rdbytes, 0, rdbytes.length);
70+
return ret;
71+
}
72+
73+
protected DurableChunk<NonVolatileMemAllocator>
74+
genuptChunk(NonVolatileMemAllocator act, Checksum cs, long size) {
75+
DurableChunk<NonVolatileMemAllocator> ret = null;
76+
ret = act.createChunk(size, false);
77+
if (null == ret) {
78+
throw new OutOfHybridMemory("Create Durable Chunk Failed.");
79+
}
80+
byte b;
81+
for (int i = 0; i < ret.getSize(); ++i) {
82+
b = (byte) rand.nextInt(255);
83+
unsafe.putByte(ret.get() + i, b);
84+
cs.update(b);
85+
}
86+
return ret;
87+
}
88+
89+
protected int genRandSize() {
90+
return rand.nextInt(1024 * 1024) + 1024 * 1024;
91+
}
92+
4693
@BeforeClass
47-
public void setUp() {
94+
public void setUp() throws Exception {
95+
rand = Utils.createRandom();
96+
unsafe = Utils.getUnsafe();
4897
m_act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024,
4998
"./pobj_hashmaps.dat", true);
5099
cKEYCAPACITY = m_act.handlerCapacity();
@@ -510,4 +559,88 @@ public void testMapIterator() {
510559
map.destroy();
511560
}
512561

562+
@Test(enabled = true)
563+
public void testMapValueBuffer() {
564+
DurableType gtypes[] = {DurableType.STRING, DurableType.BUFFER};
565+
DurableHashMap<String, DurableBuffer> map = DurableHashMapFactory.create(m_act, null, gtypes, 1, false);
566+
long bufVal;
567+
568+
Checksum bufferCheckSum = new CRC32();
569+
bufferCheckSum.reset();
570+
571+
Long handler = map.getHandler();
572+
for (int i = 0; i < 10; i++) {
573+
map.put("buffer" + i, genuptBuffer(m_act, bufferCheckSum, genRandSize()));
574+
}
575+
576+
bufVal = bufferCheckSum.getValue();
577+
578+
bufferCheckSum.reset();
579+
for (int i = 0; i < 10; i++) {
580+
DurableBuffer<NonVolatileMemAllocator> db = map.get("buffer" + i);
581+
Assert.assertNotNull(db);
582+
byte buf[] = new byte[db.get().capacity()];
583+
db.get().get(buf);
584+
bufferCheckSum.update(buf, 0, buf.length);
585+
}
586+
Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
587+
588+
bufferCheckSum.reset();
589+
DurableHashMap<String, DurableBuffer> restoredMap = DurableHashMapFactory.restore(m_act,
590+
null, gtypes, handler, false);
591+
for (int i = 0; i < 10; i++) {
592+
DurableBuffer<NonVolatileMemAllocator> db = restoredMap.get("buffer" + i);
593+
Assert.assertNotNull(db);
594+
byte buf[] = new byte[db.get().capacity()];
595+
db.get().get(buf);
596+
bufferCheckSum.update(buf, 0, buf.length);
597+
}
598+
Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
599+
600+
restoredMap.destroy();
601+
}
602+
603+
@Test(enabled = true)
604+
public void testMapValueChunk() {
605+
DurableType gtypes[] = {DurableType.STRING, DurableType.CHUNK};
606+
DurableHashMap<String, DurableChunk> map = DurableHashMapFactory.create(m_act, null, gtypes, 1, false);
607+
long chunkVal;
608+
609+
Checksum chunkCheckSum = new CRC32();
610+
chunkCheckSum.reset();
611+
612+
Long handler = map.getHandler();
613+
for (int i = 0; i < 10; i++) {
614+
map.put("chunk" + i, genuptChunk(m_act, chunkCheckSum, genRandSize()));
615+
}
616+
617+
chunkVal = chunkCheckSum.getValue();
618+
chunkCheckSum.reset();
619+
620+
for (int i = 0; i < 10; i++) {
621+
DurableChunk<NonVolatileMemAllocator> dc = map.get("chunk" + i);
622+
for (int j = 0; j < dc.getSize(); ++j) {
623+
byte b = unsafe.getByte(dc.get() + j);
624+
chunkCheckSum.update(b);
625+
}
626+
}
627+
chunkVal = chunkCheckSum.getValue();
628+
Assert.assertEquals(chunkCheckSum.getValue(), chunkVal);
629+
630+
chunkCheckSum.reset();
631+
DurableHashMap<String, DurableChunk> restoredMap = DurableHashMapFactory.restore(m_act,
632+
null, gtypes, handler, false);
633+
634+
for (int i = 0; i < 10; i++) {
635+
DurableChunk<NonVolatileMemAllocator> dc = restoredMap.get("chunk" + i);
636+
for (int j = 0; j < dc.getSize(); ++j) {
637+
byte b = unsafe.getByte(dc.get() + j);
638+
chunkCheckSum.update(b);
639+
}
640+
}
641+
chunkVal = chunkCheckSum.getValue();
642+
Assert.assertEquals(chunkCheckSum.getValue(), chunkVal);
643+
644+
restoredMap.destroy();
645+
}
513646
}

0 commit comments

Comments
 (0)