use of junitparams.Parameters in project aion by aionnetwork.
the class TrieTest method testInsertUpdateDeleteGet.
/**
* Tests correct retrieval using {@link TrieImpl#get(String)} after an
* initial insert with {@link TrieImpl#update(String, String)} and after a
* sequential update using {@link TrieImpl#update(String, String)}. Uses
* diverse combinations of keys and values from
* {@link #keyValue1Value2Parameters()}.
*/
@Test
@Parameters(method = "keyValue1Value2Parameters")
public void testInsertUpdateDeleteGet(String key, String value1, String value2) {
// create a new trie object without database
TrieImpl trie = new TrieImpl(null);
// -------------------------------------------------------------------------------------------------------------
// insert (key,value1) pair into the trie
trie.update(key, value1);
// ensure the correct value is retrieved
assertThat(value1).isEqualTo(new String(trie.get(key)));
// -------------------------------------------------------------------------------------------------------------
// update to (key,value2)
trie.update(key, value2);
// ensure correct value retrieval after update
assertThat(value2).isEqualTo(new String(trie.get(key)));
// -------------------------------------------------------------------------------------------------------------
// delete (key,value2)
trie.update(key, "");
// ensure correct value retrieval after update
assertThat(value1).isNotEqualTo(new String(trie.get(key)));
assertThat(value2).isNotEqualTo(new String(trie.get(key)));
}
use of junitparams.Parameters in project aion by aionnetwork.
the class TrieTest method testDeleteAll.
/**
* @param pairs
* @implNote By design the keys are distinct due to the use of HashMap.
*/
@Test
@Parameters(method = "keyValuePairsParameters")
public void testDeleteAll(HashMap<String, String> pairs) {
boolean print = false;
if (print) {
System.out.println("Number of pairs = " + pairs.size());
}
TrieImpl trie = new TrieImpl(new MockDB("TestDeleteAll"));
// empty at start
assertThat(Hex.toHexString(trie.getRootHash())).isEqualTo(ROOT_HASH_EMPTY);
String key, value;
for (Map.Entry<String, String> entry : pairs.entrySet()) {
key = entry.getKey();
value = entry.getValue();
if (print) {
System.out.println("(" + key + "," + value + ")");
}
// insert (key,value)
trie.update(key, value);
}
// not empty after inserts
assertThat(Hex.toHexString(trie.getRootHash())).isNotEqualTo(ROOT_HASH_EMPTY);
// ensure that everything is still there
for (String deleteKey : pairs.keySet()) {
trie.delete(deleteKey);
}
// empty at end
assertThat(Hex.toHexString(trie.getRootHash())).isEqualTo(ROOT_HASH_EMPTY);
}
use of junitparams.Parameters in project aion by aionnetwork.
the class AccessWithExceptionTest method testGetWithNullKey.
@Test(expected = IllegalArgumentException.class)
@Parameters(method = "databaseInstanceDefinitions")
public void testGetWithNullKey(Properties dbDef) {
// create database
dbDef.setProperty("db_name", DatabaseTestUtils.dbName + DatabaseTestUtils.getNext());
IByteArrayKeyValueDatabase db = DatabaseFactory.connect(dbDef);
assertThat(db.open()).isTrue();
// attempt get with null key
db.get(null);
}
use of junitparams.Parameters in project aion by aionnetwork.
the class AccessWithExceptionTest method testCommitWithClosedDatabase.
@Test(expected = RuntimeException.class)
@Parameters(method = "databaseInstanceDefinitions")
public void testCommitWithClosedDatabase(Properties dbDef) {
// create database
dbDef.setProperty("db_name", DatabaseTestUtils.dbName + DatabaseTestUtils.getNext());
IByteArrayKeyValueDatabase db = DatabaseFactory.connect(dbDef);
assertThat(db.isOpen()).isFalse();
// TODO: differentiate between not supported and closed
// attempt commit on closed db
db.commit();
}
use of junitparams.Parameters in project aion by aionnetwork.
the class ConcurrencyTest method testConcurrentAccessOnOpenDatabase.
@Test
@Parameters(method = "databaseInstanceDefinitions")
public void testConcurrentAccessOnOpenDatabase(Properties dbDef) throws InterruptedException {
dbDef.setProperty("db_name", DatabaseTestUtils.dbName + getNext());
// open database
IByteArrayKeyValueDatabase db = DatabaseFactory.connect(dbDef);
assertThat(db.open()).isTrue();
// create distinct threads with
List<Runnable> threads = new ArrayList<>();
int threadSetCount = CONCURRENT_THREADS / 8;
if (threadSetCount < 3) {
threadSetCount = 3;
}
for (int i = 0; i < threadSetCount; i++) {
// 1. thread that checks empty
addThread4IsEmpty(threads, db);
// 2. thread that gets keys
addThread4Keys(threads, db);
String keyStr = "key-" + i + ".";
// 3. thread that gets entry
addThread4Get(threads, db, keyStr);
// 4. thread that puts entry
addThread4Put(threads, db, keyStr);
// 5. thread that deletes entry
addThread4Delete(threads, db, keyStr);
// 6. thread that puts entries
addThread4PutBatch(threads, db, keyStr);
// 7. thread that deletes entry
addThread4DeleteBatch(threads, db, keyStr);
// 8. thread that checks size
addThread4Size(threads, db);
}
// run threads and check for exceptions
assertConcurrent("Testing concurrent access. ", threads, TIME_OUT);
// check that db is unlocked after updates
assertThat(db.isLocked()).isFalse();
// ensuring close
db.close();
assertThat(db.isClosed()).isTrue();
}
Aggregations