Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetch test #46

Merged
merged 1 commit into from
Jun 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/java/.checkstyle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>

<fileset-config file-format-version="1.2.0" simple-config="false" sync-formatter="false">
<local-check-config name="maven-checkstyle-plugin validate" location="file:/Users/areese/dev/github/mdbm/src/java/style.xml" type="remote" description="maven-checkstyle-plugin configuration validate">
<local-check-config name="maven-checkstyle-plugin validate" location="file:/Users/areese/dev/github/areese/mdbm/src/java/style.xml" type="remote" description="maven-checkstyle-plugin configuration validate">
<property name="checkstyle.cache.file" value="${project_loc}/target/checkstyle-cachefile"/>
<property name="checkstyle.suppressions.file" value="/Users/areese/dev/github/mdbm/src/java/target/checkstyle-suppressions-validate.xml"/>
<property name="checkstyle.header.file" value="/Users/areese/dev/github/mdbm/src/java/target/checkstyle-header-validate.txt"/>
<property name="checkstyle.suppressions.file" value="/Users/areese/dev/github/areese/mdbm/src/java/target/checkstyle-suppressions-validate.xml"/>
<property name="checkstyle.header.file" value="/Users/areese/dev/github/areese/mdbm/src/java/target/checkstyle-header-validate.txt"/>
</local-check-config>
<fileset name="java-sources-validate" enabled="true" check-config-name="maven-checkstyle-plugin validate" local="true">
<file-match-pattern match-pattern="src/main/java/.*\.java" include-pattern="true"/>
Expand Down
297 changes: 150 additions & 147 deletions src/java/src/test/java/com/yahoo/db/mdbm/TestSimpleMdbm.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,127 +24,129 @@
import com.yahoo.db.mdbm.exceptions.MdbmNoEntryException;

public abstract class TestSimpleMdbm {
public static final String testMdbmV3Path = "test/resources/testv3.mdbm";
public static final String createMdbmV3Path = "target/create_testv3.mdbm";
public static final String fetchMdbmV3Path = "target/fetch_testv3.mdbm";
public static final String fetchMdbmV3PathSingleArch = "target/fetch_testv3_single_arch.mdbm";
public static final String iteratorMdbmV3PathA = "target/iterator_testv3.mdbm";

@Test(dataProvider = "createMdbms")
public static MdbmInterface testCreate(String path, int flags, boolean close) throws MdbmException {
MdbmInterface mdbm = null;

String key = "abc";
String value = "def";
try {
File f = new File(path);
f.delete();
Assert.assertFalse(f.exists());

mdbm = MdbmProvider.open(path, flags, 0755, 0, 0);
Assert.assertFalse(mdbm.isClosed());

mdbm.storeString(key, value, Store.MDBM_REPLACE);
String ret = mdbm.fetchString(key);
Assert.assertEquals(ret, value);
try {
ret = mdbm.fetchString("nothere");
} catch (MdbmNoEntryException e) {
}

return mdbm;
} finally {
if (close && null != mdbm)
mdbm.close();
}

}

@Test(dataProvider = "versions")
public void testVersion(String path) throws MdbmException {
MdbmInterface mdbm = null;
try {
mdbm = MdbmProvider.open(path, Open.MDBM_O_RDONLY, 0755, 0, 0);
} catch (java.lang.AssertionError ae) {
dumpStats(path);
throw ae;
} finally {
if (null != mdbm)
mdbm.close();
}
}

@Test
public void testEmptyFirst() throws MdbmException {
MdbmInterface mdbm = null;
MdbmIterator iter = null;
try {
mdbm = MdbmProvider.open(testMdbmV3Path, Open.MDBM_O_RDONLY, 0755, 0, 0);
iter = mdbm.iterator();

Assert.assertNotNull(iter);

MdbmKvPair kv = mdbm.first(iter);

Assert.assertNull(kv);
kv = mdbm.next(iter);

Assert.assertNull(kv);
} finally {
if (null != mdbm)
mdbm.close();
if (null != iter)
iter.close();
}
}

@Test(dataProvider = "iterateMdbms")
public static void testIterate(String path, int flags) throws MdbmException {
MdbmInterface mdbm = null;

try {
mdbm = testCreate(path, flags, false);
int max = 10;
for (int i = 0; i < max; i++) {
String s = Integer.toString(i);
mdbm.storeString(s, s, 0);
}
for (int i = 0; i < max; i++) {
String k = Integer.toString(i);
String v = mdbm.fetchString(k);
Assert.assertNotNull(v);
}
String k = Integer.toString(max);
String v = mdbm.fetchString(k);
Assert.assertNull(v);
} finally {
if (null != mdbm)
mdbm.close();
}
}

@Test
public void testFetchString() throws MdbmException, UnsupportedEncodingException {
String key = new String("testkey");
MdbmDatum datum = new MdbmDatum(key.getBytes("UTF-8"));
MdbmInterface mdbm = null;
try {
mdbm =
MdbmProvider.open(fetchMdbmV3Path, Open.MDBM_CREATE_V3 | Open.MDBM_O_RDWR
| Open.MDBM_O_CREAT, 0755, 0, 0);
mdbm.plock(datum, 0);
mdbm.storeString(key, key, Constants.MDBM_REPLACE);
mdbm.punlock(datum, 0);
Assert.assertEquals(mdbm.fetchString(key), key);
} finally {
if (null != mdbm)
mdbm.close();
}
}

@Test
public static final String testMdbmV3Path = "test/resources/testv3.mdbm";
public static final String createMdbmV3Path = "target/create_testv3.mdbm";
public static final String fetchMdbmV3Path = "target/fetch_testv3.mdbm";
public static final String fetchMdbmV3PathSingleArch = "target/fetch_testv3_single_arch.mdbm";
public static final String iteratorMdbmV3PathA = "target/iterator_testv3.mdbm";

@Test(dataProvider = "createMdbms")
public static MdbmInterface testCreate(String path, int flags, boolean close)
throws MdbmException {
MdbmInterface mdbm = null;

String key = "abc";
String value = "def";
try {
File f = new File(path);
f.delete();
Assert.assertFalse(f.exists());

mdbm = MdbmProvider.open(path, flags, 0755, 0, 0);
Assert.assertFalse(mdbm.isClosed());

mdbm.storeString(key, value, Store.MDBM_REPLACE);
String ret = mdbm.fetchString(key);
Assert.assertEquals(ret, value);
try {
ret = mdbm.fetchString("nothere");
} catch (MdbmNoEntryException e) {
}

return mdbm;
} finally {
if (close && null != mdbm)
mdbm.close();
}

}

@Test(dataProvider = "versions")
public void testVersion(String path) throws MdbmException {
MdbmInterface mdbm = null;
try {
mdbm = MdbmProvider.open(path, Open.MDBM_O_RDONLY, 0755, 0, 0);
} catch (java.lang.AssertionError ae) {
dumpStats(path);
throw ae;
} finally {
if (null != mdbm)
mdbm.close();
}
}

@Test
public void testEmptyFirst() throws MdbmException {
MdbmInterface mdbm = null;
MdbmIterator iter = null;
try {
mdbm = MdbmProvider.open(testMdbmV3Path, Open.MDBM_O_RDONLY, 0755,
0, 0);
iter = mdbm.iterator();

Assert.assertNotNull(iter);

MdbmKvPair kv = mdbm.first(iter);

Assert.assertNull(kv);
kv = mdbm.next(iter);

Assert.assertNull(kv);
} finally {
if (null != mdbm)
mdbm.close();
if (null != iter)
iter.close();
}
}

@Test(dataProvider = "iterateMdbms")
public static void testIterate(String path, int flags) throws MdbmException {
MdbmInterface mdbm = null;

try {
mdbm = testCreate(path, flags, false);
int max = 10;
for (int i = 0; i < max; i++) {
String s = Integer.toString(i);
mdbm.storeString(s, s, 0);
}
for (int i = 0; i < max; i++) {
String k = Integer.toString(i);
String v = mdbm.fetchString(k);
Assert.assertNotNull(v);
}
String k = Integer.toString(max);
String v = mdbm.fetchString(k);
Assert.assertNull(v);
} finally {
if (null != mdbm)
mdbm.close();
}
}

@Test
public void testFetchString() throws MdbmException,
UnsupportedEncodingException {
String key = "testkey";
MdbmDatum datum = new MdbmDatum(key.getBytes("UTF-8"));
MdbmInterface mdbm = null;
try {
mdbm = MdbmProvider.open(fetchMdbmV3Path, Open.MDBM_CREATE_V3
| Open.MDBM_O_RDWR | Open.MDBM_O_CREAT, 0755, 0, 0);
mdbm.plock(datum, 0);
mdbm.storeString(key, key, Constants.MDBM_REPLACE);
mdbm.punlock(datum, 0);
Assert.assertEquals(mdbm.fetchString(key), key);
} finally {
if (null != mdbm)
mdbm.close();
}
}

@Test
public void testFetch() throws MdbmException, UnsupportedEncodingException {
String key = new String("testkey");
String key = "testkey";
MdbmDatum datum = new MdbmDatum(key.getBytes("UTF-8"));
MdbmInterface mdbm = null;
try {
Expand All @@ -165,9 +167,9 @@ public void testFetch() throws MdbmException, UnsupportedEncodingException {
}
}

@Test
@Test
public void testFetchWithoutIterator() throws MdbmException, UnsupportedEncodingException {
String key = new String("testkey");
String key = "testkey";
MdbmDatum datum = new MdbmDatum(key.getBytes("UTF-8"));
MdbmInterface mdbm = null;
try {
Expand All @@ -188,29 +190,30 @@ public void testFetchWithoutIterator() throws MdbmException, UnsupportedEncoding
}
}

public static void dumpStats(String file) {
try {
// ProcessBuilder pb = new ProcessBuilder("mdbm_stat",
// "-H", file);
// pb.redirectErrorStream(true);
// Process p = pb.start();
Process p = Runtime.getRuntime().exec("mdbm_stat" + " -H " + file);

Thread.sleep(1000);
BufferedReader isr = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream())));
String line = null;
while ((line = isr.readLine()) != null) {
System.err.println(line);
}

p.exitValue();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void dumpStats(String file) {
try {
// ProcessBuilder pb = new ProcessBuilder("mdbm_stat",
// "-H", file);
// pb.redirectErrorStream(true);
// Process p = pb.start();
Process p = Runtime.getRuntime().exec("mdbm_stat" + " -H " + file);

Thread.sleep(1000);
BufferedReader isr = new BufferedReader(new InputStreamReader(
new BufferedInputStream(p.getInputStream())));
String line = null;
while ((line = isr.readLine()) != null) {
System.err.println(line);
}

p.exitValue();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}