Skip to content

Commit

Permalink
Add scale up/down tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuujo committed Jan 3, 2018
1 parent 38b3046 commit ddfe4c3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/src/test/java/io/atomix/core/AbstractAtomixTest.java
Expand Up @@ -61,6 +61,13 @@ public static void setupAtomix() throws Exception {
* Creates an Atomix instance.
*/
protected static Atomix createAtomix(Node.Type type, int id, Integer... ids) {
return createAtomix(ids.length, type, id, ids);
}

/**
* Creates an Atomix instance.
*/
protected static Atomix createAtomix(int numPartitions, Node.Type type, int id, Integer... ids) {
Node localNode = Node.builder(String.valueOf(id))
.withType(type)
.withEndpoint(Endpoint.from("localhost", BASE_PORT + id))
Expand All @@ -78,6 +85,7 @@ protected static Atomix createAtomix(Node.Type type, int id, Integer... ids) {
.withDataDirectory(new File("target/test-logs/" + id))
.withLocalNode(localNode)
.withBootstrapNodes(bootstrapNodes)
.withCoordinationPartitions(numPartitions)
.withDataPartitions(3) // Lower number of partitions for faster testing
.build();
}
Expand Down
82 changes: 82 additions & 0 deletions core/src/test/java/io/atomix/core/AtomixTest.java
@@ -0,0 +1,82 @@
/*
* Copyright 2018-present Open Networking Foundation
*
* 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.
*/
package io.atomix.core;

import io.atomix.cluster.Node;
import io.atomix.utils.concurrent.Futures;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
* Atomix test.
*/
public class AtomixTest extends AbstractAtomixTest {
private List<Atomix> instances;

@Before
public void setupInstances() throws Exception {
deleteData();
instances = new ArrayList<>();
}

@After
public void teardownInstances() throws Exception {
List<CompletableFuture<Void>> futures = instances.stream().map(Atomix::stop).collect(Collectors.toList());
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
} catch (Exception e) {
// Do nothing
}
deleteData();
}

protected CompletableFuture<Atomix> startAtomix(int numPartitions, Node.Type type, int id, Integer... ids) {
Atomix atomix = createAtomix(numPartitions, type, id, ids);
instances.add(atomix);
return atomix.start();
}

/**
* Tests scaling up a cluster.
*/
@Test
public void testScaleUp() throws Exception {
Atomix atomix1 = startAtomix(3, Node.Type.DATA, 1, 1).join();
Atomix atomix2 = startAtomix(3, Node.Type.DATA, 2, 1, 2).join();
Atomix atomix3 = startAtomix(3, Node.Type.DATA, 3, 1, 2, 3).join();
}

/**
* Tests scaling down a cluster.
*/
@Test
public void testScaleDown() throws Exception {
List<CompletableFuture<Atomix>> futures = new ArrayList<>();
futures.add(startAtomix(3, Node.Type.DATA, 1, 1, 2, 3));
futures.add(startAtomix(3, Node.Type.DATA, 2, 1, 2, 3));
futures.add(startAtomix(3, Node.Type.DATA, 3, 1, 2, 3));
Futures.allOf(futures).join();
instances.get(0).stop().join();
instances.get(1).stop().join();
instances.get(2).stop().join();
}
}
Expand Up @@ -144,7 +144,7 @@ CompletableFuture<Void> update(PartitionMetadata metadata, PartitionManagementSe
if (server == null && metadata.members().contains(managementService.getClusterService().getLocalNode().id())) {
server = createServer(managementService);
return server.join(metadata.members());
} else if (server != null && server.isRunning()) {
} else if (server != null && !metadata.members().contains(managementService.getClusterService().getLocalNode().id())) {
return server.leave().thenRun(() -> server = null);
}
return CompletableFuture.completedFuture(null);
Expand Down

0 comments on commit ddfe4c3

Please sign in to comment.