Skip to content

Commit

Permalink
Add test module for test cluster and other test tools.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Apr 26, 2015
1 parent 057352e commit d8bffa6
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 68 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -74,6 +74,7 @@
<module>netty</module>
<module>resources</module>
<module>all</module>
<module>test</module>
</modules>

<dependencies>
Expand Down
6 changes: 6 additions & 0 deletions raft/pom.xml
Expand Up @@ -32,6 +32,12 @@
<artifactId>copycat-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>net.kuujo.copycat</groupId>
<artifactId>copycat-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Expand Up @@ -62,11 +62,11 @@ public void deleteDirectory() {
* Tests opening protocols.
*/
public void testOpen() throws Throwable {
RaftTestMemberRegistry registry = new RaftTestMemberRegistry();
TestMemberRegistry registry = new TestMemberRegistry();

RaftTestCluster cluster1 = buildCluster(1, Member.Type.ACTIVE, 3, registry);
RaftTestCluster cluster2 = buildCluster(2, Member.Type.ACTIVE, 3, registry);
RaftTestCluster cluster3 = buildCluster(3, Member.Type.ACTIVE, 3, registry);
TestCluster cluster1 = buildCluster(1, Member.Type.ACTIVE, 3, registry);
TestCluster cluster2 = buildCluster(2, Member.Type.ACTIVE, 3, registry);
TestCluster cluster3 = buildCluster(3, Member.Type.ACTIVE, 3, registry);

RaftProtocol protocol1 = buildProtocol(1, cluster1);
RaftProtocol protocol2 = buildProtocol(2, cluster2);
Expand All @@ -85,11 +85,11 @@ public void testOpen() throws Throwable {
* Tests leader elect events.
*/
public void testLeaderElectEventOnAll() throws Throwable {
RaftTestMemberRegistry registry = new RaftTestMemberRegistry();
TestMemberRegistry registry = new TestMemberRegistry();

RaftTestCluster cluster1 = buildCluster(1, Member.Type.ACTIVE, 3, registry);
RaftTestCluster cluster2 = buildCluster(2, Member.Type.ACTIVE, 3, registry);
RaftTestCluster cluster3 = buildCluster(3, Member.Type.ACTIVE, 3, registry);
TestCluster cluster1 = buildCluster(1, Member.Type.ACTIVE, 3, registry);
TestCluster cluster2 = buildCluster(2, Member.Type.ACTIVE, 3, registry);
TestCluster cluster3 = buildCluster(3, Member.Type.ACTIVE, 3, registry);

RaftProtocol protocol1 = buildProtocol(1, cluster1);
RaftProtocol protocol2 = buildProtocol(2, cluster2);
Expand All @@ -113,9 +113,9 @@ public void testLeaderElectEventOnAll() throws Throwable {

await();

RaftTestCluster cluster4 = buildCluster(4, Member.Type.PASSIVE, 4, registry);
RaftTestCluster cluster5 = buildCluster(5, Member.Type.PASSIVE, 4, registry);
RaftTestCluster cluster6 = buildCluster(6, Member.Type.REMOTE, 4, registry);
TestCluster cluster4 = buildCluster(4, Member.Type.PASSIVE, 4, registry);
TestCluster cluster5 = buildCluster(5, Member.Type.PASSIVE, 4, registry);
TestCluster cluster6 = buildCluster(6, Member.Type.REMOTE, 4, registry);

RaftProtocol protocol4 = buildProtocol(4, cluster4);
RaftProtocol protocol5 = buildProtocol(5, cluster5);
Expand All @@ -138,11 +138,11 @@ public void testLeaderElectEventOnAll() throws Throwable {
* Tests electing a new leader after a network partition.
*/
public void testElectNewLeaderAfterPartition() throws Throwable {
RaftTestMemberRegistry registry = new RaftTestMemberRegistry();
TestMemberRegistry registry = new TestMemberRegistry();

RaftTestCluster cluster1 = buildCluster(1, Member.Type.ACTIVE, 3, registry);
RaftTestCluster cluster2 = buildCluster(2, Member.Type.ACTIVE, 3, registry);
RaftTestCluster cluster3 = buildCluster(3, Member.Type.ACTIVE, 3, registry);
TestCluster cluster1 = buildCluster(1, Member.Type.ACTIVE, 3, registry);
TestCluster cluster2 = buildCluster(2, Member.Type.ACTIVE, 3, registry);
TestCluster cluster3 = buildCluster(3, Member.Type.ACTIVE, 3, registry);

RaftProtocol protocol1 = buildProtocol(1, cluster1);
RaftProtocol protocol2 = buildProtocol(2, cluster2);
Expand Down Expand Up @@ -201,7 +201,7 @@ public void accept(Event event) {
* Tests performing a command on a leader node.
*/
private void testCommandOnLeader(int nodes, Persistence persistence, Consistency consistency) throws Throwable {
RaftTestMemberRegistry registry = new RaftTestMemberRegistry();
TestMemberRegistry registry = new TestMemberRegistry();

CommitHandler commitHandler = (key, entry, result) -> {
threadAssertEquals(key.readLong(), Long.valueOf(1234));
Expand All @@ -212,7 +212,7 @@ private void testCommandOnLeader(int nodes, Persistence persistence, Consistency

Map<Integer, RaftProtocol> protocols = new HashMap<>();
for (int i = 1; i <= nodes; i++) {
RaftTestCluster cluster = buildCluster(i, Member.Type.ACTIVE, nodes, registry);
TestCluster cluster = buildCluster(i, Member.Type.ACTIVE, nodes, registry);
RaftProtocol protocol = buildProtocol(i, cluster);
protocol.commitHandler(commitHandler);
protocols.put(i, protocol);
Expand Down Expand Up @@ -492,7 +492,7 @@ public void testThreeNodeDefaultDefaultCommandOnLeader() throws Throwable {
* Tests performing a command on a follower node.
*/
public void testCommandOnFollower(int nodes, Persistence persistence, Consistency consistency) throws Throwable {
RaftTestMemberRegistry registry = new RaftTestMemberRegistry();
TestMemberRegistry registry = new TestMemberRegistry();

CommitHandler commitHandler = (key, entry, result) -> {
threadAssertEquals(key.readLong(), Long.valueOf(1234));
Expand All @@ -503,7 +503,7 @@ public void testCommandOnFollower(int nodes, Persistence persistence, Consistenc

Map<Integer, RaftProtocol> protocols = new HashMap<>();
for (int i = 1; i <= nodes; i++) {
RaftTestCluster cluster = buildCluster(i, Member.Type.ACTIVE, nodes, registry);
TestCluster cluster = buildCluster(i, Member.Type.ACTIVE, nodes, registry);
RaftProtocol protocol = buildProtocol(i, cluster);
protocol.commitHandler(commitHandler);
protocols.put(i, protocol);
Expand Down Expand Up @@ -708,7 +708,7 @@ public void testThreeNodeDefaultDefaultCommandOnFollower() throws Throwable {
* Tests a command on a passive node.
*/
public void testCommandOnPassive(int activeNodes, int passiveNodes, Persistence persistence, Consistency consistency) throws Throwable {
RaftTestMemberRegistry registry = new RaftTestMemberRegistry();
TestMemberRegistry registry = new TestMemberRegistry();

CommitHandler commitHandler = (key, entry, result) -> {
threadAssertEquals(key.readLong(), Long.valueOf(1234));
Expand All @@ -719,7 +719,7 @@ public void testCommandOnPassive(int activeNodes, int passiveNodes, Persistence

Map<Integer, RaftProtocol> activeProtocols = new HashMap<>();
for (int i = 1; i <= activeNodes; i++) {
RaftTestCluster cluster = buildCluster(i, Member.Type.ACTIVE, activeNodes, registry);
TestCluster cluster = buildCluster(i, Member.Type.ACTIVE, activeNodes, registry);
RaftProtocol protocol = buildProtocol(i, cluster);
protocol.commitHandler(commitHandler);
activeProtocols.put(i, protocol);
Expand Down Expand Up @@ -748,7 +748,7 @@ public void accept(Event event) {

Map<Integer, RaftProtocol> passiveProtocols = new HashMap<>();
for (int i = activeNodes + 1; i <= activeNodes + passiveNodes; i++) {
RaftTestCluster cluster = buildCluster(i, Member.Type.PASSIVE, activeNodes + 1, registry);
TestCluster cluster = buildCluster(i, Member.Type.PASSIVE, activeNodes + 1, registry);
RaftProtocol protocol = buildProtocol(i, cluster);
protocol.commitHandler(commitHandler);
passiveProtocols.put(i, protocol);
Expand Down Expand Up @@ -1177,7 +1177,7 @@ public void testPartialActivePartialPassiveDefaultDefaultCommandOnPassive() thro
* Tests a command on a remote node.
*/
public void testCommandOnRemote(int activeNodes, int passiveNodes, Persistence persistence, Consistency consistency) throws Throwable {
RaftTestMemberRegistry registry = new RaftTestMemberRegistry();
TestMemberRegistry registry = new TestMemberRegistry();

CommitHandler commitHandler = (key, entry, result) -> {
threadAssertEquals(key.readLong(), Long.valueOf(1234));
Expand All @@ -1201,7 +1201,7 @@ public void accept(Event event) {
expectResumes(activeNodes * 2);

for (int i = 1; i <= activeNodes; i++) {
RaftTestCluster cluster = buildCluster(i, Member.Type.ACTIVE, activeNodes, registry);
TestCluster cluster = buildCluster(i, Member.Type.ACTIVE, activeNodes, registry);
RaftProtocol protocol = buildProtocol(i, cluster);
protocol.commitHandler(commitHandler).addListener(createListener.apply(protocol));
protocol.open().thenRun(this::resume);
Expand All @@ -1212,15 +1212,15 @@ public void accept(Event event) {
expectResumes(passiveNodes * 2);

for (int i = activeNodes + 1; i <= activeNodes + passiveNodes; i++) {
RaftTestCluster cluster = buildCluster(i, Member.Type.PASSIVE, activeNodes + 1, registry);
TestCluster cluster = buildCluster(i, Member.Type.PASSIVE, activeNodes + 1, registry);
RaftProtocol protocol = buildProtocol(i, cluster);
protocol.commitHandler(commitHandler).addListener(createListener.apply(protocol));
protocol.open().thenRun(this::resume);
}

await();

RaftTestCluster cluster = buildCluster(activeNodes + passiveNodes + 1, Member.Type.REMOTE, 4, registry);
TestCluster cluster = buildCluster(activeNodes + passiveNodes + 1, Member.Type.REMOTE, 4, registry);
RaftProtocol protocol = buildProtocol(activeNodes + passiveNodes + 1, cluster);

expectResume();
Expand Down Expand Up @@ -1322,18 +1322,18 @@ public void testDefaultDefaultCommandOnRemote() throws Throwable {
/**
* Builds a Raft test cluster.
*/
private RaftTestCluster buildCluster(int id, Member.Type type, int nodes, RaftTestMemberRegistry registry) {
RaftTestCluster.Builder builder = RaftTestCluster.builder()
private TestCluster buildCluster(int id, Member.Type type, int nodes, TestMemberRegistry registry) {
TestCluster.Builder builder = TestCluster.builder()
.withRegistry(registry)
.withLocalMember(RaftTestLocalMember.builder()
.withLocalMember(TestLocalMember.builder()
.withId(id)
.withType(type)
.withAddress(String.format("test-%d", id))
.build());

for (int i = 1; i <= nodes; i++) {
if (i != id) {
builder.addRemoteMember(RaftTestRemoteMember.builder()
builder.addRemoteMember(TestRemoteMember.builder()
.withId(i)
.withType(Member.Type.ACTIVE)
.withAddress(String.format("test-%d", i))
Expand Down
65 changes: 65 additions & 0 deletions test/pom.xml
@@ -0,0 +1,65 @@
<!--
~ Copyright 2015 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>net.kuujo.copycat</groupId>
<artifactId>copycat-parent</artifactId>
<version>0.5.0-SNAPSHOT</version>
</parent>

<artifactId>copycat-test</artifactId>
<name>Copycat Test Tools</name>

<dependencies>
<dependency>
<groupId>net.kuujo.copycat</groupId>
<artifactId>copycat-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- This plugin forces the generation of jar containing streaming test classes, so that the tests
classes of external modules can use them. The two execution profiles are necessary - first one for 'mvn
package', second one for 'mvn test-compile'. Ideally, 'mvn compile' should not compile test classes and
therefore should not need this. However, an open Maven bug (http://jira.codehaus.org/browse/MNG-3559)
causes the compilation to fail if streaming test-jar is not generated. Hence, the second execution profile
for 'mvn test-compile'. -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
<execution>
<id>test-jar-on-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Expand Up @@ -25,7 +25,7 @@
*
* @author <a href="http://github.com/kuujo">Jordan Halterman</a>
*/
public class RaftTestCluster extends AbstractCluster {
public class TestCluster extends AbstractCluster {

/**
* Returns a new builder.
Expand All @@ -34,9 +34,9 @@ public static Builder builder() {
return new Builder();
}

private final RaftTestMemberRegistry registry;
private final TestMemberRegistry registry;

public RaftTestCluster(RaftTestLocalMember localMember, Collection<? extends RaftTestRemoteMember> remoteMembers, RaftTestMemberRegistry registry) {
public TestCluster(TestLocalMember localMember, Collection<? extends TestRemoteMember> remoteMembers, TestMemberRegistry registry) {
super(localMember, remoteMembers);
this.registry = registry;
localMember.init(registry);
Expand All @@ -45,22 +45,22 @@ public RaftTestCluster(RaftTestLocalMember localMember, Collection<? extends Raf

@Override
protected AbstractRemoteMember createRemoteMember(AbstractMember.Info info) {
return new RaftTestRemoteMember((RaftTestMember.Info) info, localMember.serializer.copy(), new ExecutionContext(String.format("copycat-cluster-%d", info.id()))).init(registry);
return new TestRemoteMember((TestMember.Info) info, localMember.serializer.copy(), new ExecutionContext(String.format("copycat-cluster-%d", info.id()))).init(registry);
}

@Override
public RaftTestMember member(int id) {
return (RaftTestMember) super.member(id);
public TestMember member(int id) {
return (TestMember) super.member(id);
}

/**
* Partitions members from the given member.
*/
public void partition(int id) {
if (localMember.id() == id) {
remoteMembers.values().forEach(m -> ((RaftTestRemoteMember) m).partition());
remoteMembers.values().forEach(m -> ((TestRemoteMember) m).partition());
} else {
RaftTestRemoteMember member = (RaftTestRemoteMember) remoteMembers.get(id);
TestRemoteMember member = (TestRemoteMember) remoteMembers.get(id);
if (member != null) {
member.partition();
}
Expand All @@ -72,9 +72,9 @@ public void partition(int id) {
*/
public void heal(int id) {
if (localMember.id() == id) {
remoteMembers.values().forEach(m -> ((RaftTestRemoteMember) m).heal());
remoteMembers.values().forEach(m -> ((TestRemoteMember) m).heal());
} else {
RaftTestRemoteMember member = (RaftTestRemoteMember) remoteMembers.get(id);
TestRemoteMember member = (TestRemoteMember) remoteMembers.get(id);
if (member != null) {
member.heal();
}
Expand All @@ -84,8 +84,8 @@ public void heal(int id) {
/**
* Raft test cluster builder.
*/
public static class Builder extends AbstractCluster.Builder<Builder, RaftTestLocalMember, RaftTestRemoteMember> {
private RaftTestMemberRegistry registry;
public static class Builder extends AbstractCluster.Builder<Builder, TestLocalMember, TestRemoteMember> {
private TestMemberRegistry registry;

private Builder() {
}
Expand All @@ -96,16 +96,16 @@ private Builder() {
* @param registry The test member registry.
* @return The test cluster builder.
*/
public Builder withRegistry(RaftTestMemberRegistry registry) {
public Builder withRegistry(TestMemberRegistry registry) {
this.registry = registry;
return this;
}

@Override
public RaftTestCluster build() {
public TestCluster build() {
if (registry == null)
throw new ConfigurationException("member registry must be provided");
return new RaftTestCluster(localMember, remoteMembers, registry);
return new TestCluster(localMember, remoteMembers, registry);
}
}

Expand Down

0 comments on commit d8bffa6

Please sign in to comment.