use of org.corfudb.runtime.view.ObjectsView in project CorfuDB by CorfuDB.
the class SMRMultiLogunitTest method transactionalManyWritesThenRead.
/**
* Multi Threaded.
* Verify reads after multiple transactional writes done concurrently (using 2 threads)
*
* @throws TransactionAbortedException
*/
@Test(expected = TransactionAbortedException.class)
public void transactionalManyWritesThenRead() throws TransactionAbortedException {
int numKeys = ONE_THOUSAND;
ObjectsView view = getRuntime().getObjectsView();
final CountDownLatch barrier = new CountDownLatch(2);
Map<String, String> testMap = getRuntime().getObjectsView().build().setStreamName("test").setTypeToken(new TypeToken<SMRMap<String, String>>() {
}).open();
// concurrently run two conflicting transactions: one or the other should succeed without overlap
scheduleConcurrently((ignored_step) -> {
view.TXBegin();
for (int i = 0; i < numKeys; i++) {
String key = "key" + String.valueOf(i);
String val = "value0_" + String.valueOf(i);
testMap.put(key, val);
if (i == 0) {
barrier.countDown();
barrier.await();
}
if (i % ONE_HUNDRED == 0) {
Thread.yield();
}
}
view.TXEnd();
});
scheduleConcurrently((ignored_step) -> {
view.TXBegin();
for (int i = 0; i < numKeys; i++) {
String key = "key" + String.valueOf(i);
String val = "value1_" + String.valueOf(i);
testMap.put(key, val);
if (i == 0) {
barrier.countDown();
barrier.await();
}
if (i % ONE_HUNDRED == 0) {
Thread.yield();
}
}
view.TXEnd();
});
try {
executeScheduled(2, PARAMETERS.TIMEOUT_NORMAL);
} catch (TransactionAbortedException e) {
throw e;
} catch (Exception e) {
e.printStackTrace();
}
// check that all the values are either value0_ or value1_ not a mix
String base = "invalid";
for (int i = 0; i < numKeys; i++) {
String key = "key" + String.valueOf(i);
String val = testMap.get(key);
if (val != null) {
if (i == 0) {
int underscore = val.indexOf("_");
assertNotEquals(-1, underscore);
base = val.substring(0, underscore);
System.out.println("base is " + base);
}
}
assertEquals(true, val.contains(base));
}
}
use of org.corfudb.runtime.view.ObjectsView in project CorfuDB by CorfuDB.
the class SMRMultiLogunitTest method multiThreadedManyWritesThenRead.
/**
* Multi Threaded.
* Verify reads after multiple non-transactional writes done concurrently (using 2 threads)
*
*/
@Test
public void multiThreadedManyWritesThenRead() {
int numKeys = ONE_THOUSAND;
ObjectsView view = getRuntime().getObjectsView();
Map<String, String> testMap = getRuntime().getObjectsView().build().setStreamName("test").setTypeToken(new TypeToken<SMRMap<String, String>>() {
}).open();
AtomicInteger threadsComplete = new AtomicInteger(0);
addTestStep((step) -> {
for (int i = 0; i < numKeys; i++) {
String key = "key" + String.valueOf(i);
String val = "value" + String.valueOf(step) + "_" + String.valueOf(i);
testMap.put(key, val);
if (i % ONE_HUNDRED == 0) {
Thread.yield();
}
}
threadsComplete.incrementAndGet();
});
try {
scheduleThreaded(2, 2);
} catch (Exception e) {
e.printStackTrace();
}
assertEquals(2, threadsComplete.get());
for (int i = 0; i < numKeys; i++) {
String key = "key" + String.valueOf(i);
String val = testMap.get(key);
assertNotNull(val);
}
}
Aggregations