Skip to content

Commit

Permalink
Add support for single-writer databases to unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
archiecobbs committed Apr 22, 2018
1 parent d16bf60 commit 030d766
Showing 1 changed file with 44 additions and 16 deletions.
Expand Up @@ -84,26 +84,56 @@ protected KVDatabase[][] getDBs() {

protected abstract KVDatabase getKVDatabase();

// How many transactions in testNonconflictingTransactions()?
protected int getNonconflictingTransactionCount() {
return 10;
}

// How many loops in testParallelTransactions()?
protected int getParallelTransactionLoopCount() {
return 25;
}

// How many transactions in testParallelTransactions()?
protected int getParallelTransactionTaskCount() {
return 25;
}

// How many transactions in testSequentialTransactions()?
protected int getSequentialTransactionLoopCount() {
return 50;
}

// How many iterations in RandomTask?
protected int getRandomTaskMaxIterations() {
return 1000;
}

// Does this database permit write skew anomalies?
protected boolean allowsWriteSkewAnomaly() {
return false;
}

// Does this database allow tx.setReadOnly() after data has been accessed?
protected boolean supportsReadOnlyAfterDataAccess() {
return true;
}

// When there's a conflict, is it OK if BOTH transactions fail?
protected boolean allowBothTransactionsToFail() {
return false;
}

// Does this database tolerate multiple threads accessing a single transaction?
protected boolean transactionsAreThreadSafe() {
return true;
}

// Does this database support multiple simulatenous write transactions?
protected boolean supportsMultipleWriteTransactions() {
return true;
}

@Test(dataProvider = "kvdbs")
public void testSimpleStuff(KVDatabase store) throws Exception {

Expand Down Expand Up @@ -241,10 +271,6 @@ public void testReadOnly(KVDatabase store) throws Exception {
}
}

protected boolean supportsReadOnlyAfterDataAccess() {
return true;
}

private byte[] randomBytes(int index) {
final byte[] array = new byte[this.random.nextInt(10) + 1];
this.random.nextBytes(array);
Expand Down Expand Up @@ -405,13 +431,15 @@ public void testWriteSkewAnomaly(KVDatabase store) throws Exception {
}, kv("10", "01"), kv("20", "02"));
}

protected boolean allowsWriteSkewAnomaly() {
return false;
}

protected void testConflictingTransactions(KVDatabase store, String name,
Conflictor conflictor, KVPair expected1, KVPair expected2) throws Exception {

// Conflicts handled?
if (!this.supportsMultipleWriteTransactions()) {
this.log.info("skipping " + name + "() on " + store + ": database doesn't support simultaneous writers");
return;
}

// Clear database
this.log.info("starting {}() on {}", name, store);
this.tryNtimes(store, tx -> tx.removeRange(null, null));
Expand Down Expand Up @@ -520,12 +548,12 @@ private interface Conflictor {
Future<?>[] conflict(KVTransaction tx1, KVTransaction tx2) throws Exception;
}

protected boolean allowBothTransactionsToFail() {
return false;
}

@Test(dataProvider = "kvdbs")
public void testNonconflictingTransactions(KVDatabase store) throws Exception {
if (!this.supportsMultipleWriteTransactions()) {
this.log.info("skipping testNonconflictingTransactions() on " + store + ": database doesn't support simultaneous writers");
return;
}

// Clear database
this.log.info("starting testNonconflictingTransactions() on " + store);
Expand Down Expand Up @@ -593,6 +621,10 @@ public void testNonconflictingTransactions(KVDatabase store) throws Exception {
*/
@Test(dataProvider = "kvdbs")
public void testParallelTransactions(KVDatabase store) throws Exception {
if (!this.supportsMultipleWriteTransactions()) {
this.log.info("skipping testParallelTransactions() on " + store + ": database doesn't support simultaneous writers");
return;
}
this.testParallelTransactions(new KVDatabase[] { store });
}

Expand Down Expand Up @@ -714,10 +746,6 @@ public void testMultipleThreadsTransaction(KVDatabase store) throws Exception {
this.log.info("finished testMultipleThreadsTransaction() on " + store);
}

protected boolean transactionsAreThreadSafe() {
return true;
}

/**
* Test KVStore.apply().
*
Expand Down

0 comments on commit 030d766

Please sign in to comment.