use of com.google.common.reflect.TypeToken in project cdap by caskdata.
the class StreamHandlerTest method testSimpleStreamEnqueue.
@Test
public void testSimpleStreamEnqueue() throws Exception {
// Create new stream.
HttpURLConnection urlConn = openURL(createURL("streams/test_stream_enqueue"), HttpMethod.PUT);
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
// Enqueue 10 entries
for (int i = 0; i < 10; ++i) {
urlConn = openURL(createURL("streams/test_stream_enqueue"), HttpMethod.POST);
urlConn.setDoOutput(true);
urlConn.addRequestProperty("test_stream_enqueue.header1", Integer.toString(i));
urlConn.getOutputStream().write(Integer.toString(i).getBytes(Charsets.UTF_8));
Assert.assertEquals(HttpResponseStatus.OK.getCode(), urlConn.getResponseCode());
urlConn.disconnect();
}
// Fetch 10 entries
urlConn = openURL(createURL("streams/test_stream_enqueue/events?limit=10"), HttpMethod.GET);
List<StreamEvent> events = GSON.fromJson(new String(ByteStreams.toByteArray(urlConn.getInputStream()), Charsets.UTF_8), new TypeToken<List<StreamEvent>>() {
}.getType());
for (int i = 0; i < 10; i++) {
StreamEvent event = events.get(i);
int actual = Integer.parseInt(Charsets.UTF_8.decode(event.getBody()).toString());
Assert.assertEquals(i, actual);
Assert.assertEquals(Integer.toString(i), event.getHeaders().get("header1"));
}
urlConn.disconnect();
}
use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class ObjectsViewTest method unrelatedStreamDoesNotConflict.
@Test
@SuppressWarnings("unchecked")
public void unrelatedStreamDoesNotConflict() throws Exception {
//begin tests
CorfuRuntime r = getDefaultRuntime();
Map<String, String> smrMap = r.getObjectsView().build().setStreamName("map a").setTypeToken(new TypeToken<SMRMap<String, String>>() {
}).open();
IStreamView streamB = r.getStreamsView().get(CorfuRuntime.getStreamID("b"));
smrMap.put("a", "b");
streamB.append(new SMREntry("hi", new Object[] { "hello" }, Serializers.PRIMITIVE));
//this TX should not conflict
assertThat(smrMap).doesNotContainKey("b");
r.getObjectsView().TXBegin();
String b = smrMap.get("a");
smrMap.put("b", b);
r.getObjectsView().TXEnd();
assertThat(smrMap).containsEntry("b", "b");
}
use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class TXsFromTwoRuntimesTest method staggeredTXsNoConflict.
@Test
public void staggeredTXsNoConflict() throws Exception {
final int nTXs = 5;
final Semaphore sem0 = new Semaphore(0), sem1 = new Semaphore(0);
// create two parallel worlds, with separate runtimes.
// both instantiate the same shared map
//
final Thread thread1 = new Thread(() -> {
CorfuRuntime myruntime = new CorfuRuntime(getDefaultEndpoint());
myruntime.connect();
ISMRMap<Integer, Integer> mymap = myruntime.getObjectsView().build().setStreamName(// stream name
"nonidepmpotentmaptest").setTypeToken(// object TokenType class
new TypeToken<SMRMap<Integer, Integer>>() {
}).open();
assertThat(mymap.get("world1")).isEqualTo(null);
for (int t = 0; t < nTXs; t++) {
myruntime.getObjectsView().TXBegin();
mymap.put(nTXs + t, t);
myruntime.getObjectsView().TXEnd();
}
// expect to see nTXS entries in this map
assertThat(mymap.size()).isEqualTo(nTXs);
// now allow for thread0 to commit its own transaction
sem0.release();
// next, wait for the commit to have completed
try {
sem1.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
// expect to (still) see nTXS+1 entries in this map
assertThat(mymap.size()).isEqualTo(nTXs + 1);
assertThat(mymap.get(0)).isEqualTo(0);
});
final Thread thread0 = new Thread(() -> {
CorfuRuntime myruntime = new CorfuRuntime(getDefaultEndpoint());
myruntime.connect();
ISMRMap<Integer, Integer> mymap = myruntime.getObjectsView().build().setStreamName(// stream name
"nonidepmpotentmaptest").setTypeToken(// object TokenType class
new TypeToken<SMRMap<Integer, Integer>>() {
}).open();
// start a transaction and then hand over to thread 1
myruntime.getObjectsView().TXBegin();
assertThat(mymap.get(0)).isEqualTo(null);
mymap.computeIfAbsent(0, (K) -> {
// should be computed deterministically, does it?
return mymap.size();
});
// enable thread1: it will do nTXS increments on the stream
thread1.start();
boolean isAbort = false;
try {
// wait for thread 1 to do its work;
// completion is indicated thru sem0
sem0.acquire();
myruntime.getObjectsView().TXEnd();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TransactionAbortedException te) {
isAbort = true;
}
// release thread1 to complete
sem1.release();
// expect to abort
assertThat(isAbort).isFalse();
// this currently fails, due to incorrect sync on commit in VersionLockedObject
// expect to see nTXs+1 entries on the stream
assertThat(mymap.size()).isEqualTo(nTXs + 1);
assertThat(mymap.get(0)).isEqualTo(0);
});
thread0.start();
final long WAIT_TIME = 10000;
try {
thread0.join(WAIT_TIME);
thread1.join(WAIT_TIME);
} catch (InterruptedException ie) {
throw new RuntimeException();
}
}
use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class TXsFromTwoRuntimesTest method staggeredTXsConflict.
@Test
public void staggeredTXsConflict() throws Exception {
final int nTXs = 5;
final Semaphore sem0 = new Semaphore(0), sem1 = new Semaphore(0);
// create two parallel worlds, with separate runtimes.
// both instantiate the same shared map
//
final Thread thread1 = new Thread(() -> {
CorfuRuntime myruntime = new CorfuRuntime(getDefaultEndpoint());
myruntime.connect();
ISMRMap<Integer, Integer> mymap = myruntime.getObjectsView().build().setStreamName(// stream name
"nonidepmpotentmaptest").setTypeToken(// object TokenType class
new TypeToken<SMRMap<Integer, Integer>>() {
}).open();
assertThat(mymap.get("world1")).isEqualTo(null);
for (int t = 0; t < nTXs; t++) {
myruntime.getObjectsView().TXBegin();
mymap.put(nTXs + t, t);
myruntime.getObjectsView().TXEnd();
}
// expect to see nTXS entries in this map
assertThat(mymap.size()).isEqualTo(nTXs);
// now allow for thread0 to commit its own transaction
sem0.release();
// next, wait for the commit to have completed
try {
sem1.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
// expect to (still) see nTXS entries in this map
assertThat(mymap.size()).isEqualTo(nTXs);
});
final Thread thread0 = new Thread(() -> {
CorfuRuntime myruntime = new CorfuRuntime(getDefaultEndpoint());
myruntime.connect();
SMRMap<Integer, Integer> mymap = myruntime.getObjectsView().build().setStreamName(// stream name
"nonidepmpotentmaptest").setTypeToken(// object TokenType class
new TypeToken<SMRMap<Integer, Integer>>() {
}).open();
// start a transaction and then hand over to thread 1
myruntime.getObjectsView().TXBegin();
assertThat(mymap.get(nTXs)).isEqualTo(null);
mymap.put(0, mymap.size());
// enable thread1: it will do nTXS increments on the stream
thread1.start();
boolean isAbort = false;
try {
// wait for thread 1 to do its work;
// completion is indicated thru sem0
sem0.acquire();
myruntime.getObjectsView().TXEnd();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TransactionAbortedException te) {
isAbort = true;
}
// release thread1 to complete
sem1.release();
// expect to abort
assertThat(isAbort).isTrue();
// expect to see nTXs entries on the stream
assertThat(mymap.size()).isEqualTo(nTXs);
});
thread0.start();
final long WAIT_TIME = 10000;
try {
thread0.join(WAIT_TIME);
thread1.join(WAIT_TIME);
} catch (InterruptedException ie) {
throw new RuntimeException();
}
}
use of com.google.common.reflect.TypeToken in project CorfuDB by CorfuDB.
the class UndoTest method ckMultiStreamRollback2.
/**
* In this test, a variant of multi-stream interleaving is
* tested.
*
* transactions are started on two threads, 1, 2.
* Then two things happen:
*
* 1. some updates are committed
* 2. then t2 is resumed and makes optimistic updates
*
*
* then t1 resumes and should roll back both the optimistic updates
* adn these commits.
*
* @throws Exception
*/
@Test
public void ckMultiStreamRollback2() throws Exception {
ArrayList<Map> maps = new ArrayList<>();
final int nmaps = 3;
for (int i = 0; i < nmaps; i++) maps.add((SMRMap<Integer, String>) instantiateCorfuObject(new TypeToken<SMRMap<Integer, String>>() {
}, "test stream" + i));
// before t1 starts
crossStream(maps, normalValue);
// t1 starts transaction.
// snapshot should include all the keys inserted above
t(t1, () -> {
WWTXBegin();
// size() is called to make the TX obtains a snapshot at this point,
maps.get(0).size();
// and does not wait to lazily obtain it later, when it reads for
// the first time
});
// t2 starts transaction.
t(t2, () -> {
WWTXBegin();
// size() is called to make the TX obtains a snapshot at this point,
maps.get(0).size();
// and does not wait to lazily obtain it later, when it reads for
// the first time
});
// t3 modifies everything
t(t3, () -> crossStream(maps, specialValue));
// now, t2 optimistically modifying everything, but
// not yet committing
t(t2, () -> {
for (Map m : maps) m.put(specialKey, specialValue2);
});
// t1 should undo everything by t2 and by t3
t(t1, () -> {
for (Map m : maps) {
assertThat(m.get(specialKey)).isEqualTo(normalValue);
assertThat(m.get(specialKey + 1)).isEqualTo(normalValue);
}
});
// main thread, t2's work should be committed
for (Map m : maps) {
assertThat(m.get(specialKey)).isEqualTo(specialValue);
assertThat(m.get(specialKey + 1)).isEqualTo(specialValue);
}
}
Aggregations