From f90345e4b6138b06351a12dfb6be8013159a7855 Mon Sep 17 00:00:00 2001 From: dahliamalkhi Date: Tue, 28 Jun 2016 23:01:18 -0700 Subject: [PATCH 1/2] added simple counter test --- .travis.yml | 0 bin/airline | 1 - scripts/run_example.sh | 0 .../example/counter/SimpleCounterExample.java | 66 +++++++++++++++++++ .../example/counter/SimpleCounterTest.java | 51 ++++++++++++++ 5 files changed, 117 insertions(+), 1 deletion(-) mode change 100755 => 100644 .travis.yml delete mode 120000 bin/airline mode change 100755 => 100644 scripts/run_example.sh create mode 100644 src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java create mode 100644 src/test/java/org/corfudb/example/counter/SimpleCounterTest.java diff --git a/.travis.yml b/.travis.yml old mode 100755 new mode 100644 diff --git a/bin/airline b/bin/airline deleted file mode 120000 index 959dcbc..0000000 --- a/bin/airline +++ /dev/null @@ -1 +0,0 @@ -../scripts/run_example.sh \ No newline at end of file diff --git a/scripts/run_example.sh b/scripts/run_example.sh old mode 100755 new mode 100644 diff --git a/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java b/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java new file mode 100644 index 0000000..eee6e21 --- /dev/null +++ b/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java @@ -0,0 +1,66 @@ +package org.corfudb.example.org.corfudb.example.counter; + +import org.corfudb.runtime.CorfuRuntime; +import org.corfudb.runtime.object.Accessor; +import org.corfudb.runtime.object.ICorfuSMRObject; +import org.corfudb.runtime.object.Mutator; +import org.docopt.Docopt; + +import java.util.Map; + +/** + * Created by dmalkhi on 6/28/16. + */ +public class SimpleCounterExample { + + // This example uses docopt to simplify argument processing. This document below + // uses the docopt DSL and defines all the valid arguments to invoke the example + // program. It also sets reasonable defaults. See http://docopt.org for more details. + private static final String doc = + "a simple counter example over Corfu.\n\n" + +"Usage:\n" + +" simplecounter [-c ]\n" + +" simplecounter (-h | --help)\n\n" + +"Options:\n" + +" -c The configuration string for Corfu. [default: localhost:9000]\n" + +" --h --help show this screen\n"; + + + CorfuRuntime runtime; + + public SimpleCounterExample(CorfuRuntime runtime) { + this.runtime = runtime; + } + + public static class SharedCounter implements ICorfuSMRObject { + Integer cnt; + + + @Accessor + public Integer getCnt() { return cnt; } + + @Mutator + public void setCnt(int v) { cnt = v; } + + } + + public SharedCounter getSharedCounter() { + // create a shared counter of type Intefer with name "SimpleCounterExample" + SharedCounter SharedCounter = runtime.getObjectsView().build() + .setType(SharedCounter.class) + .setStreamName("SimpleCounterExample") + .open(); + return SharedCounter; + } + + public static void main(String[] args) throws Exception { + // Use docopt to parse the command line arguments. + Map opts = new Docopt(doc).parse(args); + CorfuRuntime runtime = new CorfuRuntime((String) opts.get("-c")) + .connect(); + SimpleCounterExample simpleCounterExample = new SimpleCounterExample(runtime); + SharedCounter cnt = simpleCounterExample.getSharedCounter(); + cnt.setCnt(44); + assert cnt.getCnt() == 44; + } +} diff --git a/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java b/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java new file mode 100644 index 0000000..89259d8 --- /dev/null +++ b/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java @@ -0,0 +1,51 @@ +package org.corfudb.example.counter; + +import org.corfudb.example.org.corfudb.example.counter.SimpleCounterExample; +import org.corfudb.runtime.view.AbstractViewTest; +import org.junit.Test; + +/** + * Created by dalia on 6/28/16. + */ +public class SimpleCounterTest extends AbstractViewTest { + @Override + public String getDefaultConfigurationString() { + return getDefaultEndpoint(); + } + + @Test + public void canOpenSharedCounter() + throws Exception + { + SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); + simpleCounter.getSharedCounter(); + + } + + @Test + public void canSetSharedCounter() + throws Exception + { + SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); + SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter(); + cnt.setCnt(44); + + } + @Test + public void canGetSharedCounter() + throws Exception + { + SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); + SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter(); + cnt.getCnt(); + } + @Test + public void canSetAndGetSharedCounter() + throws Exception + { + SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); + SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter(); + cnt.setCnt(44); + assert cnt.getCnt() == 44; + } +} From f02ff9b9f824e8740fbb42ed0d5cc2a5bc1165db Mon Sep 17 00:00:00 2001 From: Dahlia Malkhi Date: Wed, 29 Jun 2016 13:12:37 -0700 Subject: [PATCH 2/2] fixed various based on reviews --- .travis.yml | 0 bin/airline | 1 + scripts/run_example.sh | 2 +- .../org/corfudb/example/{ => airline}/AirlineExample.java | 5 +---- .../org/corfudb/example/counter/SimpleCounterExample.java | 2 +- .../org/corfudb/example/counter/SimpleCounterTest.java | 7 +++++-- 6 files changed, 9 insertions(+), 8 deletions(-) mode change 100644 => 100755 .travis.yml create mode 120000 bin/airline mode change 100644 => 100755 scripts/run_example.sh rename src/main/java/org/corfudb/example/{ => airline}/AirlineExample.java (98%) diff --git a/.travis.yml b/.travis.yml old mode 100644 new mode 100755 diff --git a/bin/airline b/bin/airline new file mode 120000 index 0000000..959dcbc --- /dev/null +++ b/bin/airline @@ -0,0 +1 @@ +../scripts/run_example.sh \ No newline at end of file diff --git a/scripts/run_example.sh b/scripts/run_example.sh old mode 100644 new mode 100755 index 9e38ceb..e5b071f --- a/scripts/run_example.sh +++ b/scripts/run_example.sh @@ -40,4 +40,4 @@ CORFUDB_HEAP="${CORFUDB_HEAP:-1000}" export CORFUDB_JVMFLAGS="-Xmx${CORFUDB_HEAP}m $SERVER_JVMFLAGS" RUN_AS=`basename $0` -"$JAVA" -cp "$CLASSPATH" $JVMFLAGS org.corfudb.example.AirlineExample $* \ No newline at end of file +"$JAVA" -cp "$CLASSPATH" $JVMFLAGS org.corfudb.example.airline.AirlineExample $* \ No newline at end of file diff --git a/src/main/java/org/corfudb/example/AirlineExample.java b/src/main/java/org/corfudb/example/airline/AirlineExample.java similarity index 98% rename from src/main/java/org/corfudb/example/AirlineExample.java rename to src/main/java/org/corfudb/example/airline/AirlineExample.java index 0bbf62a..1bd3a57 100644 --- a/src/main/java/org/corfudb/example/AirlineExample.java +++ b/src/main/java/org/corfudb/example/airline/AirlineExample.java @@ -1,7 +1,6 @@ -package org.corfudb.example; +package org.corfudb.example.airline; import com.google.common.collect.ImmutableMap; -import org.corfudb.example.airline.AirlineManager; import org.corfudb.example.airline.objects.Aircraft; import org.corfudb.example.airline.objects.Airplane; import org.corfudb.example.airline.objects.DailyFlightSchedule; @@ -11,10 +10,8 @@ import org.fusesource.jansi.Ansi; import org.fusesource.jansi.AnsiConsole; -import java.lang.reflect.Array; import java.time.LocalDateTime; import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import static org.fusesource.jansi.Ansi.ansi; diff --git a/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java b/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java index eee6e21..f8fe1d2 100644 --- a/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java +++ b/src/main/java/org/corfudb/example/org/corfudb/example/counter/SimpleCounterExample.java @@ -32,7 +32,7 @@ public SimpleCounterExample(CorfuRuntime runtime) { this.runtime = runtime; } - public static class SharedCounter implements ICorfuSMRObject { + public static class SharedCounter /* implements ICorfuSMRObject */ { Integer cnt; diff --git a/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java b/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java index 89259d8..8e01305 100644 --- a/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java +++ b/src/test/java/org/corfudb/example/counter/SimpleCounterTest.java @@ -3,6 +3,8 @@ import org.corfudb.example.org.corfudb.example.counter.SimpleCounterExample; import org.corfudb.runtime.view.AbstractViewTest; import org.junit.Test; +import static org.assertj.core.api.Assertions.assertThat; + /** * Created by dalia on 6/28/16. @@ -37,7 +39,7 @@ public void canGetSharedCounter() { SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter(); - cnt.getCnt(); + System.out.printf("counter value: " + cnt.getCnt()); } @Test public void canSetAndGetSharedCounter() @@ -46,6 +48,7 @@ public void canSetAndGetSharedCounter() SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime()); SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter(); cnt.setCnt(44); - assert cnt.getCnt() == 44; + System.out.printf("counter value: " + cnt.getCnt()); + assertThat(cnt.getCnt() == 44); } }