Skip to content

Commit

Permalink
Merge pull request pmem#148 from lukaszstolarczuk/tests
Browse files Browse the repository at this point in the history
tests: make test path configurable by maven property
  • Loading branch information
lukaszstolarczuk authored Apr 23, 2021
2 parents ec44854 + c5ed0fe commit e9354c8
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 34 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ This library includes a set of automated tests that exercise all functionality.
LD_LIBRARY_PATH=<path_to_libs> mvn test
```

to execute tests on non-default path (`/dev/shm`), setup desired directory, e.g.:

```sh
LD_LIBRARY_PATH=<path_to_libs> mvn test -Dtest.db.dir=/my/test/dir
```

## Examples

We use `/dev/shm` with [emulated persistent memory](https://pmem.io/2016/02/22/pm-emulation.html)
Expand Down
22 changes: 22 additions & 0 deletions pmemkv-binding/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>test.db.dir</property>
<message>Property test.db.dir was not set! Default test path "/dev/shm" will be used!</message>
</requireProperty>
</rules>
<fail>false</fail>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
32 changes: 17 additions & 15 deletions pmemkv-binding/src/test/java/io/pmem/pmemkv/CmapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,34 @@
package io.pmem.pmemkv;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.*;

import java.io.File;
import java.nio.ByteBuffer;

import static org.junit.Assert.*;

import static io.pmem.pmemkv.TestUtils.*;

public class CmapTest {

private final String ENGINE = "cmap";
private String DB_PATH = "";

@Rule
public TemporaryFolder folder = new TemporaryFolder();
public TemporaryFolder testDir = new TemporaryFolder(DEFAULT_DB_DIR);

@Before
public void init() {
DB_PATH = testDir.getRoot() + File.separator + "testfile";
assertTrue(DB_PATH != null && !DB_PATH.isEmpty());
}

@Test
public void testCreateAndOpen() {
String file = folder.getRoot() + File.pathSeparator + "testfile";
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, file, new ByteBufferConverter());
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, DB_PATH, new ByteBufferConverter());

assertFalse(db.exists(stringToByteBuffer("key1")));
db.put(stringToByteBuffer("key1"), stringToByteBuffer("value1"));
Expand All @@ -35,20 +41,18 @@ public void testCreateAndOpen() {

db.stop();

db = openDB(ENGINE, file, new ByteBufferConverter());
db = openDB(ENGINE, DB_PATH, new ByteBufferConverter());
assertTrue(db.exists(stringToByteBuffer("key1")));
resBuff = db.getCopy(stringToByteBuffer("key1"));
assertEquals(byteBufferToString(resBuff), "value1");
}

@Test
public void throwsExceptionOnStartWhenOpeningNonExistentFile() {
String file = folder.getRoot() + File.pathSeparator + "testfile";

Database<ByteBuffer, ByteBuffer> db = null;

try {
db = openDB(ENGINE, file, new ByteBufferConverter());
db = openDB(ENGINE, DB_PATH, new ByteBufferConverter());
Assert.fail();
} catch (DatabaseException e) {
/* file doesn't exist, open should throw */
Expand All @@ -59,8 +63,7 @@ public void throwsExceptionOnStartWhenOpeningNonExistentFile() {

@Test
public void throwsExceptionOnSortedCountFuncs() {
String file = folder.getRoot() + File.pathSeparator + "testfile";
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, file, new ByteBufferConverter());
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, DB_PATH, new ByteBufferConverter());
ByteBuffer key1 = stringToByteBuffer("key1");
ByteBuffer key2 = stringToByteBuffer("key2");

Expand Down Expand Up @@ -94,8 +97,7 @@ public void multipleThreadsDBTest() {
final int threadsNumber = 8;
final int numberOfElements = 100;

String file = folder.getRoot() + File.pathSeparator + "testfile";
final Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, file, new ByteBufferConverter());
final Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, DB_PATH, new ByteBufferConverter());

runParallel(threadsNumber, () -> {
for (int j = 0; j < numberOfElements; ++j) {
Expand Down Expand Up @@ -130,7 +132,7 @@ public void multipleDBTypesTest() {
final int threadsNumber = 8;
final int numberOfElements = 200;

String file1 = folder.getRoot() + File.pathSeparator + "testfile1";
String file1 = DB_PATH + "1";
final Database<String, String> dbString = createDB(ENGINE, file1, new StringConverter(), 190, 190);
final StringBuilder sb = new StringBuilder(numberOfElements + 1);
sb.append('x');
Expand All @@ -139,7 +141,7 @@ public void multipleDBTypesTest() {
dbString.put(sb.substring(0, i), sb.substring(0, i + 1));
}

String file2 = folder.getRoot() + File.pathSeparator + "testfile2";
String file2 = DB_PATH + "2";
final Database<ByteBuffer, ByteBuffer> dbByteBuffer = createDB(ENGINE, file2, new ByteBufferConverter(), 180,
180);
for (int i = 0; i < numberOfElements; ++i) {
Expand Down
31 changes: 22 additions & 9 deletions pmemkv-binding/src/test/java/io/pmem/pmemkv/DatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@

package io.pmem.pmemkv;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.*;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;

import static java.nio.charset.StandardCharsets.UTF_8;

import static io.pmem.pmemkv.TestUtils.*;

public class DatabaseTest {

private final String ENGINE = "vsmap";
private String DB_DIR = "";

/* Helper method, used in most of the tests in this file */
private Database<ByteBuffer, ByteBuffer> buildDB(String engine) {
return openDB(engine, "/dev/shm", new ByteBufferConverter());
return openDB(engine, DB_DIR, new ByteBufferConverter());
}

@Rule
public TemporaryFolder testDir = new TemporaryFolder(DEFAULT_DB_DIR);

@Before
public void init() {
DB_DIR = testDir.getRoot().toString();
assertTrue(DB_DIR != null && !DB_DIR.isEmpty());
}

@Test
Expand Down Expand Up @@ -324,7 +337,7 @@ public void usesGetAllAboveTest() {
}

@Test
public void usesGetAllBelowTest() {
public void usesGetBelowTest() {
Database<ByteBuffer, ByteBuffer> db = buildDB(ENGINE);
db.put(stringToByteBuffer("A"), stringToByteBuffer("1"));
db.put(stringToByteBuffer("AB"), stringToByteBuffer("2"));
Expand All @@ -351,7 +364,7 @@ public void usesGetAllBelowTest() {
}

@Test
public void usesGetAllBetweenTest() {
public void usesGetBetweenTest() {
Database<ByteBuffer, ByteBuffer> db = buildDB(ENGINE);
db.put(stringToByteBuffer("A"), stringToByteBuffer("1"));
db.put(stringToByteBuffer("AB"), stringToByteBuffer("2"));
Expand Down Expand Up @@ -510,7 +523,7 @@ public void usesGetAllMultiThreadedTest() {
/* Test DB with non default cache buffers */
@Test
public void usesNotDefaultCacheBuffersTest() {
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), 5, 5);
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), 5, 5);

/* cache buffers should be used */
db.put(stringToByteBuffer("A"), stringToByteBuffer("A"));
Expand Down Expand Up @@ -544,31 +557,31 @@ public void usesNotDefaultCacheBuffersTest() {
public void usesVariousCacheBuffersSizeTest() {
boolean exception_caught = false;
try {
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), -5, 5);
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), -5, 5);
} catch (IllegalArgumentException e) {
exception_caught = true;
}
assertTrue(exception_caught);

exception_caught = false;
try {
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), 5, -5);
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), 5, -5);
} catch (IllegalArgumentException e) {
exception_caught = true;
}
assertTrue(exception_caught);

exception_caught = false;
try {
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), 0, 0);
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), 0, 0);
} catch (IllegalArgumentException e) {
exception_caught = true;
}
assertTrue(exception_caught);

exception_caught = false;
try {
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(),
Database<ByteBuffer, ByteBuffer> db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(),
Integer.MAX_VALUE, Integer.MAX_VALUE);
} catch (IllegalArgumentException e) {
exception_caught = true;
Expand Down
27 changes: 20 additions & 7 deletions pmemkv-binding/src/test/java/io/pmem/pmemkv/ExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.Test;
import static org.junit.Assert.*;

Expand All @@ -14,6 +16,8 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;

import static io.pmem.pmemkv.TestUtils.*;

@SuppressWarnings("serial")
class CustomException extends RuntimeException {
public CustomException(String message) {
Expand All @@ -24,19 +28,27 @@ public CustomException(String message) {
public class ExceptionTest {

private final String ENGINE = "vsmap";
private String DB_DIR = "";
private Database<ByteBuffer, ByteBuffer> db;

/* Helper method, used in most of the tests in this file */
private Database<ByteBuffer, ByteBuffer> buildDB(String engine) {
return new Database.Builder<ByteBuffer, ByteBuffer>(engine)
.setSize(1073741824)
.setPath("/dev/shm")
.setSize(DEFAULT_DB_SIZE)
.setPath(DB_DIR)
.setKeyConverter(new ByteBufferConverter())
.setValueConverter(new ByteBufferConverter())
.build();
}

@Rule
public TemporaryFolder testDir = new TemporaryFolder(DEFAULT_DB_DIR);

@Before
public void init() {
DB_DIR = testDir.getRoot().toString();
assertTrue(DB_DIR != null && !DB_DIR.isEmpty());

db = buildDB(ENGINE);
// Direct ByteBuffer
for (int i = 0; i < 0xFF; i++) {
Expand All @@ -60,7 +72,7 @@ public void throwsExceptionOnStartWhenPathIsMissing() {
boolean exception_occured = false;
try {
db = new Database.Builder<ByteBuffer, ByteBuffer>(ENGINE)
.setSize(1073741824)
.setSize(DEFAULT_DB_SIZE)
.build();
Assert.fail();
} catch (InvalidArgumentException kve) {
Expand All @@ -78,7 +90,7 @@ public void throwsExceptionOnStartWhenSizeIsMissing() {
boolean exception_occured = false;
try {
db = new Database.Builder<ByteBuffer, ByteBuffer>(ENGINE)
.setPath("/dev/shm")
.setPath(DB_DIR)
.build();
Assert.fail();
} catch (InvalidArgumentException kve) {
Expand Down Expand Up @@ -112,7 +124,7 @@ public void throwsExceptionOnStartWhenPathIsInvalidTest() {
boolean exception_occured = false;
try {
db = new Database.Builder<ByteBuffer, ByteBuffer>(ENGINE)
.setSize(1073741824)
.setSize(DEFAULT_DB_SIZE)
.setPath("/tmp/123/234/345/456/567/678/nope.nope")
.build();
Assert.fail();
Expand All @@ -135,7 +147,7 @@ public void throwsExceptionOnStartWhenPathIsWrongTypeTest() {
boolean exception_occured = false;
try {
db = new Database.Builder<ByteBuffer, ByteBuffer>(ENGINE)
.setSize(1073741824)
.setSize(DEFAULT_DB_SIZE)
.setPath("1234")
.build();
Assert.fail();
Expand Down Expand Up @@ -217,7 +229,8 @@ public void exceptionInGet() {
assertTrue(exception_occured);
}

/* Other */
/* Other exceptions */

@Test(expected = RuntimeException.class)
public void exceptionsHierarchy() {
/* All engines should derive from DatabaseException class */
Expand Down
9 changes: 6 additions & 3 deletions pmemkv-binding/src/test/java/io/pmem/pmemkv/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package io.pmem.pmemkv;

import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.ByteBuffer;
import java.util.ArrayList;

Expand All @@ -14,7 +14,10 @@ interface Callback {
}

class TestUtils {
private static final long DEFAULT_DB_SIZE = 1073741824;
public static final long DEFAULT_DB_SIZE = 1073741824;

/* Get test dir from command line or use default */
public static final File DEFAULT_DB_DIR = new File(System.getProperty("test.db.dir", "/dev/shm"));

public static class StringConverter implements Converter<String> {
public ByteBuffer toByteBuffer(String entry) {
Expand Down

0 comments on commit e9354c8

Please sign in to comment.