use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class WriteWriteTXConcurrencyTest method simpleWWTest.
@Test
public void simpleWWTest() {
//Instantiate a Corfu Stream named "A" dedicated to an SMRmap object.
SMRMap<String, Integer> map = (SMRMap<String, Integer>) instantiateCorfuObject(new TypeToken<SMRMap<String, Integer>>() {
}, "A");
AtomicInteger valA = new AtomicInteger(0), valB = new AtomicInteger(0);
t(0, () -> WWTXBegin());
t(0, () -> map.put("a", 1));
t(0, () -> map.put("b", 1));
t(1, () -> WWTXBegin());
t(1, () -> {
Integer ga = map.get("a");
if (ga != null)
valA.set(ga);
});
t(1, () -> {
Integer gb = map.get("b");
if (gb != null)
valB.set(gb);
});
t(1, () -> TXEnd());
t(0, () -> TXEnd());
assertThat(valA.get()).isEqualTo(valB.get());
}
use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class SMRMapTest method readSetDiffFromWriteSet.
@Test
@SuppressWarnings("unchecked")
public void readSetDiffFromWriteSet() throws Exception {
Map<String, String> testMap = getRuntime().getObjectsView().build().setStreamName("test1").setTypeToken(new TypeToken<SMRMap<String, String>>() {
}).open();
Map<String, String> testMap2 = getRuntime().getObjectsView().build().setStreamName("test2").setTypeToken(new TypeToken<SMRMap<String, String>>() {
}).open();
testMap.put("a", "b");
testMap2.put("a", "c");
Semaphore s1 = new Semaphore(0);
Semaphore s2 = new Semaphore(0);
scheduleConcurrently(1, threadNumber -> {
s1.tryAcquire(PARAMETERS.TIMEOUT_NORMAL.toMillis(), TimeUnit.MILLISECONDS);
getRuntime().getObjectsView().TXBegin();
testMap2.put("a", "d");
getRuntime().getObjectsView().TXEnd();
s2.release();
});
scheduleConcurrently(1, threadNumber -> {
getRuntime().getObjectsView().TXBegin();
testMap.compute("b", (k, v) -> testMap2.get("a"));
s1.release();
s2.tryAcquire(PARAMETERS.TIMEOUT_NORMAL.toMillis(), TimeUnit.MILLISECONDS);
assertThatThrownBy(() -> getRuntime().getObjectsView().TXEnd()).isInstanceOf(TransactionAbortedException.class);
});
executeScheduled(PARAMETERS.CONCURRENCY_TWO, PARAMETERS.TIMEOUT_NORMAL);
}
use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class SMRMapTest method canWriteScanAndFilterToSingle.
@Test
@SuppressWarnings("unchecked")
public void canWriteScanAndFilterToSingle() throws Exception {
Map<String, String> corfuInstancesMap = getRuntime().getObjectsView().build().setStreamName("test").setTypeToken(new TypeToken<SMRMap<String, String>>() {
}).open();
corfuInstancesMap.clear();
assertThat(corfuInstancesMap.put("a", "CorfuServer")).isNull();
assertThat(corfuInstancesMap.put("b", "CorfuClient")).isNull();
assertThat(corfuInstancesMap.put("c", "CorfuClient")).isNull();
assertThat(corfuInstancesMap.put("d", "CorfuServer")).isNull();
List<String> corfuServerList = ((SMRMap) corfuInstancesMap).scanAndFilter(p -> p.equals("CorfuServer"));
assertThat(corfuServerList.size()).isEqualTo(2);
for (String corfuInstance : corfuServerList) {
assertThat(corfuInstance).isEqualTo("CorfuServer");
}
}
use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class CompileProxyTest method testObjectCounterReadConcurrency.
/**
* the test interleaves reads and writes to a shared counter.
*
* The test uses the interleaving engine to interleave numTasks*threads state machines.
* A simple state-machine is built. It randomly chooses to either read or append the counter.
* On a read, the last written value is expected.
*
* @throws Exception
*/
@Test
public void testObjectCounterReadConcurrency() throws Exception {
CorfuSharedCounter sharedCounter = getDefaultRuntime().getObjectsView().build().setStreamName("my stream").setTypeToken(new TypeToken<CorfuSharedCounter>() {
}).open();
int numTasks = PARAMETERS.NUM_ITERATIONS_LOW;
Random r = new Random(PARAMETERS.SEED);
sharedCounter.setValue(-1);
AtomicInteger lastUpdate = new AtomicInteger(-1);
assertThat(sharedCounter.getValue()).isEqualTo(lastUpdate.get());
// only one step: randomly choose between read/append of the shared counter
addTestStep((task_num) -> {
if (r.nextBoolean()) {
sharedCounter.setValue(task_num);
// remember the last written value
lastUpdate.set(task_num);
} else {
// expect to read the value in lastUpdate
assertThat(sharedCounter.getValue()).isEqualTo(lastUpdate.get());
}
});
// invoke the interleaving engine
scheduleInterleaved(PARAMETERS.CONCURRENCY_SOME, PARAMETERS.CONCURRENCY_SOME * numTasks);
}
use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class CorfuSMRObjectProxyTest method canOpenObjectWithTwoRuntimes.
@Test
public void canOpenObjectWithTwoRuntimes() throws Exception {
getDefaultRuntime();
final int TEST_VALUE = 42;
TestClass testClass = (TestClass) instantiateCorfuObject(new TypeToken<TestClass>() {
}, "test");
testClass.set(TEST_VALUE);
assertThat(testClass.get()).isEqualTo(TEST_VALUE);
CorfuRuntime runtime2 = new CorfuRuntime(getDefaultEndpoint());
runtime2.connect();
TestClass testClass2 = (TestClass) instantiateCorfuObject(runtime2, new TypeToken<TestClass>() {
}, "test");
assertThat(testClass2.get()).isEqualTo(TEST_VALUE);
}
Aggregations