From c5ed0fe2e614be7e7193b8c93c18fdf6bbfe4da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Stolarczuk?= Date: Thu, 22 Apr 2021 18:46:02 +0200 Subject: [PATCH] tests: make test path configurable by maven property fixes: #88 --- README.md | 6 ++++ pmemkv-binding/pom.xml | 22 +++++++++++++ .../test/java/io/pmem/pmemkv/CmapTest.java | 32 ++++++++++--------- .../java/io/pmem/pmemkv/DatabaseTest.java | 31 ++++++++++++------ .../java/io/pmem/pmemkv/ExceptionTest.java | 27 ++++++++++++---- .../test/java/io/pmem/pmemkv/TestUtils.java | 9 ++++-- 6 files changed, 93 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 2e3674fd..0da9bba3 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,12 @@ This library includes a set of automated tests that exercise all functionality. LD_LIBRARY_PATH= mvn test ``` +to execute tests on non-default path (`/dev/shm`), setup desired directory, e.g.: + +```sh +LD_LIBRARY_PATH= 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) diff --git a/pmemkv-binding/pom.xml b/pmemkv-binding/pom.xml index 42b7a486..71f69251 100644 --- a/pmemkv-binding/pom.xml +++ b/pmemkv-binding/pom.xml @@ -63,6 +63,28 @@ + + org.apache.maven.plugins + maven-enforcer-plugin + 3.0.0-M2 + + + enforce-property + + enforce + + + + + test.db.dir + Property test.db.dir was not set! Default test path "/dev/shm" will be used! + + + false + + + + diff --git a/pmemkv-binding/src/test/java/io/pmem/pmemkv/CmapTest.java b/pmemkv-binding/src/test/java/io/pmem/pmemkv/CmapTest.java index 5d31cfa4..181d2626 100644 --- a/pmemkv-binding/src/test/java/io/pmem/pmemkv/CmapTest.java +++ b/pmemkv-binding/src/test/java/io/pmem/pmemkv/CmapTest.java @@ -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 db = createDB(ENGINE, file, new ByteBufferConverter()); + Database db = createDB(ENGINE, DB_PATH, new ByteBufferConverter()); assertFalse(db.exists(stringToByteBuffer("key1"))); db.put(stringToByteBuffer("key1"), stringToByteBuffer("value1")); @@ -35,7 +41,7 @@ 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"); @@ -43,12 +49,10 @@ public void testCreateAndOpen() { @Test public void throwsExceptionOnStartWhenOpeningNonExistentFile() { - String file = folder.getRoot() + File.pathSeparator + "testfile"; - Database 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 */ @@ -59,8 +63,7 @@ public void throwsExceptionOnStartWhenOpeningNonExistentFile() { @Test public void throwsExceptionOnSortedCountFuncs() { - String file = folder.getRoot() + File.pathSeparator + "testfile"; - Database db = createDB(ENGINE, file, new ByteBufferConverter()); + Database db = createDB(ENGINE, DB_PATH, new ByteBufferConverter()); ByteBuffer key1 = stringToByteBuffer("key1"); ByteBuffer key2 = stringToByteBuffer("key2"); @@ -94,8 +97,7 @@ public void multipleThreadsDBTest() { final int threadsNumber = 8; final int numberOfElements = 100; - String file = folder.getRoot() + File.pathSeparator + "testfile"; - final Database db = createDB(ENGINE, file, new ByteBufferConverter()); + final Database db = createDB(ENGINE, DB_PATH, new ByteBufferConverter()); runParallel(threadsNumber, () -> { for (int j = 0; j < numberOfElements; ++j) { @@ -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 dbString = createDB(ENGINE, file1, new StringConverter(), 190, 190); final StringBuilder sb = new StringBuilder(numberOfElements + 1); sb.append('x'); @@ -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 dbByteBuffer = createDB(ENGINE, file2, new ByteBufferConverter(), 180, 180); for (int i = 0; i < numberOfElements; ++i) { diff --git a/pmemkv-binding/src/test/java/io/pmem/pmemkv/DatabaseTest.java b/pmemkv-binding/src/test/java/io/pmem/pmemkv/DatabaseTest.java index b3306088..17fca33c 100644 --- a/pmemkv-binding/src/test/java/io/pmem/pmemkv/DatabaseTest.java +++ b/pmemkv-binding/src/test/java/io/pmem/pmemkv/DatabaseTest.java @@ -3,12 +3,14 @@ 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.*; @@ -16,9 +18,20 @@ 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 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 @@ -324,7 +337,7 @@ public void usesGetAllAboveTest() { } @Test - public void usesGetAllBelowTest() { + public void usesGetBelowTest() { Database db = buildDB(ENGINE); db.put(stringToByteBuffer("A"), stringToByteBuffer("1")); db.put(stringToByteBuffer("AB"), stringToByteBuffer("2")); @@ -351,7 +364,7 @@ public void usesGetAllBelowTest() { } @Test - public void usesGetAllBetweenTest() { + public void usesGetBetweenTest() { Database db = buildDB(ENGINE); db.put(stringToByteBuffer("A"), stringToByteBuffer("1")); db.put(stringToByteBuffer("AB"), stringToByteBuffer("2")); @@ -510,7 +523,7 @@ public void usesGetAllMultiThreadedTest() { /* Test DB with non default cache buffers */ @Test public void usesNotDefaultCacheBuffersTest() { - Database db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), 5, 5); + Database db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), 5, 5); /* cache buffers should be used */ db.put(stringToByteBuffer("A"), stringToByteBuffer("A")); @@ -544,7 +557,7 @@ public void usesNotDefaultCacheBuffersTest() { public void usesVariousCacheBuffersSizeTest() { boolean exception_caught = false; try { - Database db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), -5, 5); + Database db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), -5, 5); } catch (IllegalArgumentException e) { exception_caught = true; } @@ -552,7 +565,7 @@ public void usesVariousCacheBuffersSizeTest() { exception_caught = false; try { - Database db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), 5, -5); + Database db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), 5, -5); } catch (IllegalArgumentException e) { exception_caught = true; } @@ -560,7 +573,7 @@ public void usesVariousCacheBuffersSizeTest() { exception_caught = false; try { - Database db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), 0, 0); + Database db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), 0, 0); } catch (IllegalArgumentException e) { exception_caught = true; } @@ -568,7 +581,7 @@ public void usesVariousCacheBuffersSizeTest() { exception_caught = false; try { - Database db = createDB(ENGINE, "/dev/shm", new ByteBufferConverter(), + Database db = createDB(ENGINE, DB_DIR, new ByteBufferConverter(), Integer.MAX_VALUE, Integer.MAX_VALUE); } catch (IllegalArgumentException e) { exception_caught = true; diff --git a/pmemkv-binding/src/test/java/io/pmem/pmemkv/ExceptionTest.java b/pmemkv-binding/src/test/java/io/pmem/pmemkv/ExceptionTest.java index eace32fa..b95528d6 100644 --- a/pmemkv-binding/src/test/java/io/pmem/pmemkv/ExceptionTest.java +++ b/pmemkv-binding/src/test/java/io/pmem/pmemkv/ExceptionTest.java @@ -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.*; @@ -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) { @@ -24,19 +28,27 @@ public CustomException(String message) { public class ExceptionTest { private final String ENGINE = "vsmap"; + private String DB_DIR = ""; private Database db; + /* Helper method, used in most of the tests in this file */ private Database buildDB(String engine) { return new Database.Builder(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++) { @@ -60,7 +72,7 @@ public void throwsExceptionOnStartWhenPathIsMissing() { boolean exception_occured = false; try { db = new Database.Builder(ENGINE) - .setSize(1073741824) + .setSize(DEFAULT_DB_SIZE) .build(); Assert.fail(); } catch (InvalidArgumentException kve) { @@ -78,7 +90,7 @@ public void throwsExceptionOnStartWhenSizeIsMissing() { boolean exception_occured = false; try { db = new Database.Builder(ENGINE) - .setPath("/dev/shm") + .setPath(DB_DIR) .build(); Assert.fail(); } catch (InvalidArgumentException kve) { @@ -112,7 +124,7 @@ public void throwsExceptionOnStartWhenPathIsInvalidTest() { boolean exception_occured = false; try { db = new Database.Builder(ENGINE) - .setSize(1073741824) + .setSize(DEFAULT_DB_SIZE) .setPath("/tmp/123/234/345/456/567/678/nope.nope") .build(); Assert.fail(); @@ -135,7 +147,7 @@ public void throwsExceptionOnStartWhenPathIsWrongTypeTest() { boolean exception_occured = false; try { db = new Database.Builder(ENGINE) - .setSize(1073741824) + .setSize(DEFAULT_DB_SIZE) .setPath("1234") .build(); Assert.fail(); @@ -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 */ diff --git a/pmemkv-binding/src/test/java/io/pmem/pmemkv/TestUtils.java b/pmemkv-binding/src/test/java/io/pmem/pmemkv/TestUtils.java index bed5eff1..047854e6 100644 --- a/pmemkv-binding/src/test/java/io/pmem/pmemkv/TestUtils.java +++ b/pmemkv-binding/src/test/java/io/pmem/pmemkv/TestUtils.java @@ -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; @@ -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 { public ByteBuffer toByteBuffer(String entry) {