@Test @SuppressWarnings("unchecked") public void canUseAnnotations() throws Exception { CorfuRuntime r = getDefaultRuntime(); TestClassUsingAnnotation test = r.getObjectsView() .build() .setStreamName("test") .setType(TestClassUsingAnnotation.class) .open(); assertThat(test.testFn1()).isTrue(); assertThat(test.testIncrement()).isTrue(); assertThat(test.getValue()).isNotZero(); // clear the cache, forcing a new object to be built. r.getObjectsView().getObjectCache().clear(); TestClassUsingAnnotation test2 = r.getObjectsView() .build() .setStreamName("test") .setType(TestClassUsingAnnotation.class) .open(); assertThat(test).isNotSameAs(test2); assertThat(test2.getValue()).isNotZero(); }
@Test @SuppressWarnings("unchecked") public void multipleWritesConsistencyTestConcurrent() throws Exception { getDefaultRuntime().connect(); Map<String, String> testMap = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("test"), TreeMap.class); testMap.clear(); int num_threads = 5; int num_records = 1_000; scheduleConcurrently( num_threads, threadNumber -> { int base = threadNumber * num_records; for (int i = base; i < base + num_records; i++) { assertThat(testMap.put(Integer.toString(i), Integer.toString(i))).isEqualTo(null); } }); executeScheduled(num_threads, 50, TimeUnit.SECONDS); Map<String, String> testMap2 = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("test"), TreeMap.class); scheduleConcurrently( num_threads, threadNumber -> { int base = threadNumber * num_records; for (int i = base; i < base + num_records; i++) { assertThat(testMap2.get(Integer.toString(i))).isEqualTo(Integer.toString(i)); } }); executeScheduled(num_threads, 50, TimeUnit.SECONDS); }
@Test @SuppressWarnings("unchecked") public void canWrapObjectWithPrimitiveTypes() throws Exception { // begin tests CorfuRuntime r = getDefaultRuntime().connect(); TestClassWithPrimitives test = r.getObjectsView().open("test", TestClassWithPrimitives.class); test.setPrimitive("hello world".getBytes()); assertThat(test.getPrimitive()).isEqualTo("hello world".getBytes()); }
@Test @SuppressWarnings("unchecked") public void canContainNullObjects() throws Exception { Map<String, String> testMap = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("a"), SMRMap.class); testMap.clear(); testMap.put("z", null); assertThat(testMap.get("z")).isEqualTo(null); Map<String, String> testMap2 = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("a"), SMRMap.class); assertThat(testMap2.get("z")).isEqualTo(null); }
/** * Deserialize an object from a given byte buffer. * * @param b The bytebuf to deserialize. * @return The deserialized object. */ @Override public Object deserialize(ByteBuf b, CorfuRuntime rt) { int classNameLength = b.readShort(); byte[] classNameBytes = new byte[classNameLength]; b.readBytes(classNameBytes, 0, classNameLength); String className = new String(classNameBytes); if (className.equals("null")) { return null; } else if (className.equals("CorfuObject")) { int SMRClassNameLength = b.readShort(); byte[] SMRClassNameBytes = new byte[SMRClassNameLength]; b.readBytes(SMRClassNameBytes, 0, SMRClassNameLength); String SMRClassName = new String(SMRClassNameBytes); try { return rt.getObjectsView() .build() .setStreamID(new UUID(b.readLong(), b.readLong())) .setType(Class.forName(SMRClassName)) .open(); } catch (ClassNotFoundException cnfe) { log.error("Exception during deserialization!", cnfe); throw new RuntimeException(cnfe); } } else { try (ByteBufInputStream bbis = new ByteBufInputStream(b)) { try (InputStreamReader r = new InputStreamReader(bbis)) { return gson.fromJson(r, Class.forName(className)); } } catch (IOException | ClassNotFoundException ie) { log.error("Exception during deserialization!", ie); throw new RuntimeException(ie); } } }
@Test @SuppressWarnings("unchecked") public void canReadWriteToSingle() throws Exception { getDefaultRuntime(); Map<String, String> testMap = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("test"), TreeMap.class); testMap.clear(); assertThat(testMap.put("a", "a")).isNull(); assertThat(testMap.put("a", "b")).isEqualTo("a"); assertThat(testMap.get("a")).isEqualTo("b"); Map<String, String> testMap2 = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("test"), TreeMap.class); assertThat(testMap2.get("a")).isEqualTo("b"); }
@Test @SuppressWarnings("unchecked") public void canUsePrimitiveSerializer() throws Exception { // begin tests CorfuRuntime r = getDefaultRuntime().connect(); TestClassWithPrimitives test = r.getObjectsView() .build() .setType(TestClassWithPrimitives.class) .setStreamName("test") .setSerializer(null) .open(); test.setPrimitive("hello world".getBytes()); assertThat(test.getPrimitive()).isEqualTo("hello world".getBytes()); }
@Test @SuppressWarnings("unchecked") public void modificationDuringTransactionCausesAbort() throws Exception { Map<String, String> testMap = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("A"), SMRMap.class); assertThat(testMap.put("a", "z")); getRuntime().getObjectsView().TXBegin(); assertThat(testMap.put("a", "a")).isEqualTo("z"); assertThat(testMap.put("a", "b")).isEqualTo("a"); assertThat(testMap.get("a")).isEqualTo("b"); CompletableFuture cf = CompletableFuture.runAsync( () -> { Map<String, String> testMap2 = getRuntime() .getObjectsView() .open( UUID.nameUUIDFromBytes("A".getBytes()), SMRMap.class, null, EnumSet.of(ObjectOpenOptions.NO_CACHE), SerializerType.JSON); testMap2.put("a", "f"); }); cf.join(); assertThatThrownBy(() -> getRuntime().getObjectsView().TXEnd()) .isInstanceOf(TransactionAbortedException.class); }
@Test public void postHandlersFire() throws Exception { CorfuRuntime r = getDefaultRuntime(); Map<String, String> test = r.getObjectsView().build().setType(SMRMap.class).setStreamName("test").open(); ICorfuSMRObject cObj = (ICorfuSMRObject) test; final AtomicInteger ai = new AtomicInteger(0); cObj.registerPostHandler( (String method, Object[] args, Object state) -> { ai.incrementAndGet(); }); test.put("a", "b"); test.get("a"); assertThat(ai.get()).isEqualTo(1); }
@Test @SuppressWarnings("unchecked") public void unusedMutatorAccessor() throws Exception { Map<String, String> testMap = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("A"), SMRMap.class); testMap.put("a", "z"); }
@Test public void canOpenObjectWithTwoRuntimes() throws Exception { getDefaultRuntime(); TestClass testClass = getRuntime().getObjectsView().build().setStreamName("test").setType(TestClass.class).open(); testClass.set(52); assertThat(testClass.get()).isEqualTo(52); CorfuRuntime runtime2 = new CorfuRuntime(getDefaultEndpoint()); runtime2.connect(); TestClass testClass2 = runtime2.getObjectsView().build().setStreamName("test").setType(TestClass.class).open(); assertThat(testClass2.get()).isEqualTo(52); }
@Test @SuppressWarnings("unchecked") public void multipleWritesConsistencyTest() throws Exception { getDefaultRuntime().connect(); Map<String, String> testMap = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("test"), TreeMap.class); testMap.clear(); for (int i = 0; i < 10_000; i++) { assertThat(testMap.put(Integer.toString(i), Integer.toString(i))).isNull(); } Map<String, String> testMap2 = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("test"), TreeMap.class); for (int i = 0; i < 10_000; i++) { assertThat(testMap2.get(Integer.toString(i))).isEqualTo(Integer.toString(i)); } }
@Test @SuppressWarnings("unchecked") public void canUseCustomSerializer() throws Exception { // Register a custom serializer and use it with an SMR object Serializers.setCustomSerializer(SerializerType.JSON.getSerializer()); CorfuRuntime r = getDefaultRuntime(); Map<String, String> test = r.getObjectsView() .build() .setType(SMRMap.class) .setStreamName("test") .setSerializer(SerializerType.CUSTOM) .open(); test.put("a", "b"); test.get("a"); assertThat(test.get("a")).isEqualTo("b"); }
@Test @SuppressWarnings("unchecked") public void canContainOtherCorfuObjects() throws Exception { Map<String, String> testMap = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("a"), SMRMap.class); testMap.clear(); testMap.put("z", "e"); Map<String, Map<String, String>> testMap2 = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("b"), SMRMap.class); testMap2.put("a", testMap); assertThat(testMap2.get("a").get("z")).isEqualTo("e"); testMap2.get("a").put("y", "f"); assertThat(testMap.get("y")).isEqualTo("f"); Map<String, String> testMap3 = getRuntime().getObjectsView().open(CorfuRuntime.getStreamID("a"), SMRMap.class); assertThat(testMap3.get("y")).isEqualTo("f"); }
@Test @SuppressWarnings("unchecked") public void canUpdateSingleObjectTransacationallyWhenCached() throws Exception { r.setCacheDisabled(false); Map<String, String> testMap = getRuntime().getObjectsView().open(UUID.randomUUID(), SMRMap.class); getRuntime().getObjectsView().TXBegin(); assertThat(testMap.put("a", "a")).isNull(); assertThat(testMap.put("a", "b")).isEqualTo("a"); assertThat(testMap.get("a")).isEqualTo("b"); getRuntime().getObjectsView().TXEnd(); assertThat(testMap.get("a")).isEqualTo("b"); }
@Test @SuppressWarnings("unchecked") public void loadsFollowedByGetsConcurrent() throws Exception { r.setBackpointersDisabled(true); Map<String, String> testMap = getRuntime().getObjectsView().open(UUID.randomUUID(), SMRMap.class); final int num_threads = 5; final int num_records = 1_000; testMap.clear(); scheduleConcurrently( num_threads, threadNumber -> { int base = threadNumber * num_records; for (int i = base; i < base + num_records; i++) { assertThat(testMap.put(Integer.toString(i), Integer.toString(i))).isEqualTo(null); } }); long startTime = System.currentTimeMillis(); executeScheduled(num_threads, 30, TimeUnit.SECONDS); calculateRequestsPerSecond("WPS", num_records * num_threads, startTime); scheduleConcurrently( num_threads, threadNumber -> { int base = threadNumber * num_records; for (int i = base; i < base + num_records; i++) { assertThat(testMap.get(Integer.toString(i))).isEqualTo(Integer.toString(i)); } }); startTime = System.currentTimeMillis(); executeScheduled(num_threads, 30, TimeUnit.SECONDS); calculateRequestsPerSecond("RPS", num_records * num_threads, startTime); }