Skip to content

Commit

Permalink
Refactor test framework to test cluster messaging for all protocols.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Feb 1, 2015
1 parent cae174a commit 23eaebe
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 219 deletions.
Expand Up @@ -15,9 +15,11 @@
*/
package net.kuujo.copycat.collections;

import net.jodah.concurrentunit.ConcurrentTestCase;
import net.kuujo.copycat.cluster.ClusterConfig;
import net.kuujo.copycat.log.BufferedLog;
import net.kuujo.copycat.protocol.Consistency;
import net.kuujo.copycat.test.ResourceTest;
import net.kuujo.copycat.protocol.LocalProtocol;
import net.kuujo.copycat.test.TestCluster;
import org.testng.annotations.Test;

Expand All @@ -30,18 +32,19 @@
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
@Test
public class AsyncMapTest extends ResourceTest<AsyncMap<String, String>> {

@Override
protected TestCluster<AsyncMap<String, String>> createCluster() {
return TestCluster.of((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig().withLog(new BufferedLog())));
}
public class AsyncMapTest extends ConcurrentTestCase {

/**
* Tests putting a value in an asynchronous map and then reading the value.
*/
public void testAsyncMapPutGet() throws Throwable {
TestCluster<AsyncMap<String, String>> cluster = TestCluster.of((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig().withLog(new BufferedLog())));
TestCluster<AsyncMap<String, String>> cluster = TestCluster.<AsyncMap<String, String>>builder()
.withActiveMembers(3)
.withPassiveMembers(2)
.withUriFactory(id -> String.format("local://test%d", id))
.withClusterFactory(members -> new ClusterConfig().withProtocol(new LocalProtocol()).withMembers(members))
.withResourceFactory((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig().withLog(new BufferedLog())))
.build();
expectResume();
cluster.open().thenRun(this::resume);
await(5000);
Expand All @@ -61,7 +64,13 @@ public void testAsyncMapPutGet() throws Throwable {
* Tests putting a value in an asynchronous map and then removing it.
*/
public void testAsyncMapPutRemove() throws Throwable {
TestCluster<AsyncMap<String, String>> cluster = TestCluster.of((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig().withLog(new BufferedLog())));
TestCluster<AsyncMap<String, String>> cluster = TestCluster.<AsyncMap<String, String>>builder()
.withActiveMembers(3)
.withPassiveMembers(2)
.withUriFactory(id -> String.format("local://test%d", id))
.withClusterFactory(members -> new ClusterConfig().withProtocol(new LocalProtocol()).withMembers(members))
.withResourceFactory((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig().withLog(new BufferedLog())))
.build();
expectResume();
cluster.open().thenRun(this::resume);
await(5000);
Expand All @@ -86,9 +95,13 @@ public void testAsyncMapPutRemove() throws Throwable {
* Tests getting a value from a passive member of the cluster.
*/
public void testAsyncMapGetFromPassiveMember() throws Throwable {
TestCluster<AsyncMap<String, String>> cluster = TestCluster.of((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig()
.withConsistency(Consistency.WEAK)
.withLog(new BufferedLog())));
TestCluster<AsyncMap<String, String>> cluster = TestCluster.<AsyncMap<String, String>>builder()
.withActiveMembers(3)
.withPassiveMembers(2)
.withUriFactory(id -> String.format("local://test%d", id))
.withClusterFactory(members -> new ClusterConfig().withProtocol(new LocalProtocol()).withMembers(members))
.withResourceFactory((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig().withConsistency(Consistency.WEAK).withLog(new BufferedLog())))
.build();

expectResume();
cluster.open().thenRun(this::resume);
Expand All @@ -114,12 +127,14 @@ public void testAsyncMapGetFromPassiveMember() throws Throwable {
* Tests putting enough entries in the map's log to roll over the log to a new segment.
*/
public void testAsyncMapPutMany() throws Throwable {
TestCluster<AsyncMap<String, String>> cluster = TestCluster.of((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig()
.withConsistency(Consistency.WEAK)
.withLog(new BufferedLog()
.withSegmentSize(1024)
.withFlushOnWrite(true))));

TestCluster<AsyncMap<String, String>> cluster = TestCluster.<AsyncMap<String, String>>builder()
.withActiveMembers(3)
.withPassiveMembers(2)
.withUriFactory(id -> String.format("local://test%d", id))
.withClusterFactory(members -> new ClusterConfig().withProtocol(new LocalProtocol()).withMembers(members))
.withResourceFactory((uri, config) -> AsyncMap.create("test", uri, config, new AsyncMapConfig().withConsistency(Consistency.WEAK).withLog(new BufferedLog().withSegmentInterval(1024).withFlushOnWrite(true))))
.build();

expectResume();
cluster.open().thenRun(this::resume);
await(5000);
Expand Down
Expand Up @@ -15,8 +15,10 @@
*/
package net.kuujo.copycat.collections;

import net.jodah.concurrentunit.ConcurrentTestCase;
import net.kuujo.copycat.cluster.ClusterConfig;
import net.kuujo.copycat.log.BufferedLog;
import net.kuujo.copycat.test.ResourceTest;
import net.kuujo.copycat.protocol.LocalProtocol;
import net.kuujo.copycat.test.TestCluster;
import org.testng.annotations.Test;

Expand All @@ -26,18 +28,20 @@
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
@Test
public class AsyncSetTest extends ResourceTest<AsyncSet<String>> {

@Override
protected TestCluster<AsyncSet<String>> createCluster() {
return TestCluster.of((uri, config) -> AsyncSet.<String>create("test", uri, config, new AsyncSetConfig().withLog(new BufferedLog())));
}
public class AsyncSetTest extends ConcurrentTestCase {

/**
* Sets adding and removing an item in the set.
*/
public void testSetAddRemove() throws Throwable {
TestCluster<AsyncSet<String>> cluster = TestCluster.of((uri, config) -> AsyncSet.<String>create("test", uri, config, new AsyncSetConfig().withLog(new BufferedLog())));
TestCluster<AsyncSet<String>> cluster = TestCluster.<AsyncSet<String>>builder()
.withActiveMembers(3)
.withPassiveMembers(2)
.withUriFactory(id -> String.format("local://test%d", id))
.withClusterFactory(members -> new ClusterConfig().withProtocol(new LocalProtocol()).withMembers(members))
.withResourceFactory((uri, config) -> AsyncSet.create("test", uri, config, new AsyncSetConfig().withLog(new BufferedLog())))
.build();

expectResume();
cluster.open().thenRun(this::resume);
await(5000);
Expand Down
19 changes: 11 additions & 8 deletions event-log/src/test/java/net/kuujo/copycat/event/EventLogTest.java
Expand Up @@ -15,8 +15,10 @@
*/
package net.kuujo.copycat.event;

import net.jodah.concurrentunit.ConcurrentTestCase;
import net.kuujo.copycat.cluster.ClusterConfig;
import net.kuujo.copycat.log.BufferedLog;
import net.kuujo.copycat.test.ResourceTest;
import net.kuujo.copycat.protocol.LocalProtocol;
import net.kuujo.copycat.test.TestCluster;
import org.testng.annotations.Test;

Expand All @@ -26,18 +28,19 @@
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
@Test
public class EventLogTest extends ResourceTest<EventLog<String>> {

@Override
protected TestCluster<EventLog<String>> createCluster() {
return TestCluster.of((uri, config) -> EventLog.create("test", uri, config, new EventLogConfig().withLog(new BufferedLog())));
}
public class EventLogTest extends ConcurrentTestCase {

/**
* Tests that a passive member receives events for the event log.
*/
public void testPassiveEvents() throws Throwable {
TestCluster<EventLog<String>> cluster = TestCluster.of((uri, config) -> EventLog.create("test", uri, config, new EventLogConfig().withLog(new BufferedLog())));
TestCluster<EventLog<String>> cluster = TestCluster.<EventLog<String>>builder()
.withActiveMembers(3)
.withPassiveMembers(2)
.withUriFactory(id -> String.format("local://test%d", id))
.withClusterFactory(members -> new ClusterConfig().withProtocol(new LocalProtocol()).withMembers(members))
.withResourceFactory((uri, config) -> EventLog.create("test", uri, config, new EventLogConfig().withLog(new BufferedLog())))
.build();
expectResume();
cluster.open().thenRun(this::resume);
await(15000);
Expand Down
Expand Up @@ -33,8 +33,8 @@ protected Protocol createProtocol() {
}

@Override
protected String createUri() {
return "tcp://localhost:5555";
protected String createUri(int id) {
return String.format("tcp://localhost:555%d", id);
}

}
13 changes: 3 additions & 10 deletions state-log/src/test/java/net/kuujo/copycat/state/StateLogTest.java
Expand Up @@ -15,20 +15,13 @@
*/
package net.kuujo.copycat.state;

import net.kuujo.copycat.log.BufferedLog;
import net.kuujo.copycat.test.ResourceTest;
import net.kuujo.copycat.test.TestCluster;
import org.testng.annotations.Test;

/**
* State log test.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class StateLogTest extends ResourceTest<StateLog<String>> {

@Override
protected TestCluster<StateLog<String>> createCluster() {
return TestCluster.of((uri, config) -> StateLog.<String>create("test", uri, config, new StateLogConfig().withLog(new BufferedLog())));
}

@Test
public class StateLogTest {
}
Expand Up @@ -15,46 +15,13 @@
*/
package net.kuujo.copycat.state;

import net.kuujo.copycat.log.BufferedLog;
import net.kuujo.copycat.test.ResourceTest;
import net.kuujo.copycat.test.TestCluster;
import org.testng.annotations.Test;

/**
* State machine test.
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class StateMachineTest extends ResourceTest<StateMachine<StateMachineTest.TestState>> {

public static interface TestState {
void put(Object value);
Object get();
void except();
}

public static class TestStateImpl implements TestState {
private StateContext<TestState> context;
@Initializer
public void init(StateContext<TestState> context) {
this.context = context;
}
@Override
public void put(Object value) {
context.put("value", value);
}
@Override
public Object get() {
return context.get("value");
}
@Override
public void except() {
throw new RuntimeException();
}
}

@Override
protected TestCluster<StateMachine<TestState>> createCluster() {
return TestCluster.of((uri, config) -> StateMachine.create("test", uri, config, new StateMachineConfig().withStateType(TestState.class).withInitialState(TestStateImpl.class).withLog(new BufferedLog())));
}

@Test
public class StateMachineTest {
}

0 comments on commit 23eaebe

Please sign in to comment.