Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added simple counter test #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/run_example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 $*
"$JAVA" -cp "$CLASSPATH" $JVMFLAGS org.corfudb.example.airline.AirlineExample $*
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <corfu-config>]\n"
+" simplecounter (-h | --help)\n\n"
+"Options:\n"
+" -c <corfu-config> 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<String, Object> 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;
}
}
54 changes: 54 additions & 0 deletions src/test/java/org/corfudb/example/counter/SimpleCounterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.corfudb.example.counter;

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.
*/
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();
System.out.printf("counter value: " + cnt.getCnt());
}
@Test
public void canSetAndGetSharedCounter()
throws Exception
{
SimpleCounterExample simpleCounter = new SimpleCounterExample(getDefaultRuntime());
SimpleCounterExample.SharedCounter cnt = simpleCounter.getSharedCounter();
cnt.setCnt(44);
System.out.printf("counter value: " + cnt.getCnt());
assertThat(cnt.getCnt() == 44);
}
}