Skip to content

Commit

Permalink
IGNITE-15579 Use test class and test method names as a part of cluste…
Browse files Browse the repository at this point in the history
…r node names (#359)
  • Loading branch information
sashapolo committed Sep 29, 2021
1 parent d590f4d commit b40025b
Show file tree
Hide file tree
Showing 26 changed files with 694 additions and 511 deletions.
Expand Up @@ -29,6 +29,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.io.TempDir;
import picocli.CommandLine;

Expand Down Expand Up @@ -58,7 +59,7 @@ public class ITConfigCommandTest extends AbstractCliTest {

/** */
@BeforeEach
private void setup(@TempDir Path workDir) throws IOException {
void setup(@TempDir Path workDir, TestInfo testInfo) throws IOException {
// TODO: IGNITE-15131 Must be replaced by receiving the actual port configs from the started node.
// This approach still can produce the port, which will be unavailable at the moment of node start.
restPort = getAvailablePort();
Expand All @@ -69,20 +70,32 @@ private void setup(@TempDir Path workDir) throws IOException {
"rest.port=" + restPort + "\n" + "rest.portRange=0" + "\n" +
"clientConnector.port=" + clientPort + "\n" + "clientConnector.portRange=0";

IgnitionManager.start("node1", configStr, workDir);
IgnitionManager.start(testNodeName(testInfo, networkPort), configStr, workDir);

ctx = ApplicationContext.run(Environment.TEST);

err = new ByteArrayOutputStream();
out = new ByteArrayOutputStream();
}

/** */
@AfterEach
private void tearDown() {
IgnitionManager.stop("node1");
void tearDown(TestInfo testInfo) {
IgnitionManager.stop(testNodeName(testInfo, networkPort));
ctx.stop();
}

/**
* Creates a unique Ignite node name for the given test.
*/
private static String testNodeName(TestInfo testInfo, int port) {
return testInfo.getTestClass()
.map(Class::getCanonicalName)
.flatMap(clsName -> testInfo.getTestMethod().map(method -> clsName + '#' + method.getName()))
.map(name -> name + ':' + port)
.orElseThrow();
}

@Test
public void setAndGetWithManualHost() {
int exitCode = cmd(ctx).execute(
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.apache.ignite.lang.IgniteInternalException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.TestInfo;

import static java.lang.Thread.sleep;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -257,4 +258,18 @@ public static String randomString(Random rnd, int len) {

return sb.toString();
}

/**
* Creates a unique Ignite node name for the given test.
*/
public static String testNodeName(TestInfo testInfo, int port) {
return testInfo.getTestClass()
.map(Class::getCanonicalName)
.map(name -> testInfo.getTestMethod()
.map(method -> name + '#' + method.getName())
.orElse(name)
)
.map(name -> name + ':' + port)
.orElseThrow();
}
}
Expand Up @@ -58,6 +58,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
Expand Down Expand Up @@ -194,13 +195,13 @@ public class ITMetaStorageServiceTest {
* Run {@code NODES} cluster nodes.
*/
@BeforeEach
public void beforeTest() throws Exception {
public void beforeTest(TestInfo testInfo) throws Exception {
var nodeFinder = new LocalPortRangeNodeFinder(NODE_PORT_BASE, NODE_PORT_BASE + NODES);

nodeFinder.findNodes().stream()
.map(
addr -> ClusterServiceTestUtils.clusterService(
addr.toString(),
testInfo,
addr.port(),
nodeFinder,
SERIALIZATION_REGISTRY,
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.apache.ignite.utils.ClusterServiceTestUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -60,13 +61,13 @@ void tearDown() {
* Tests that restarting nodes get discovered in an established topology.
*/
@Test
public void testRestarts() {
public void testRestarts(TestInfo testInfo) {
final int initPort = 3344;

var nodeFinder = new LocalPortRangeNodeFinder(initPort, initPort + 5);

services = nodeFinder.findNodes().stream()
.map(addr -> startNetwork(addr, nodeFinder))
.map(addr -> startNetwork(testInfo, addr, nodeFinder))
.collect(Collectors.toCollection(ArrayList::new)); // ensure mutability

for (ClusterService service : services) {
Expand All @@ -86,11 +87,11 @@ public void testRestarts() {
services.get(idx1).stop();

LOG.info("Starting {}", addresses.get(idx0));
ClusterService svc0 = startNetwork(addresses.get(idx0), nodeFinder);
ClusterService svc0 = startNetwork(testInfo, addresses.get(idx0), nodeFinder);
services.set(idx0, svc0);

LOG.info("Starting {}", addresses.get(idx1));
ClusterService svc2 = startNetwork(addresses.get(idx1), nodeFinder);
ClusterService svc2 = startNetwork(testInfo, addresses.get(idx1), nodeFinder);
services.set(idx1, svc2);

for (ClusterService service : services) {
Expand All @@ -104,13 +105,14 @@ public void testRestarts() {
/**
* Creates a {@link ClusterService} using the given local address and the node finder.
*
* @param testInfo Test info.
* @param addr Node address.
* @param nodeFinder Node finder.
* @return Created Cluster Service.
*/
private ClusterService startNetwork(NetworkAddress addr, NodeFinder nodeFinder) {
private ClusterService startNetwork(TestInfo testInfo, NetworkAddress addr, NodeFinder nodeFinder) {
ClusterService clusterService = ClusterServiceTestUtils.clusterService(
addr.toString(),
testInfo,
addr.port(),
nodeFinder,
serializationRegistry,
Expand Down
Expand Up @@ -50,6 +50,7 @@
import org.apache.ignite.utils.ClusterServiceTestUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import reactor.core.publisher.Mono;

import static org.apache.ignite.internal.testframework.matchers.CompletableFutureMatcher.willBe;
Expand Down Expand Up @@ -87,12 +88,12 @@ public void tearDown() {
* @throws Exception in case of errors.
*/
@Test
public void messageWasSentToAllMembersSuccessfully() throws Exception {
public void messageWasSentToAllMembersSuccessfully(TestInfo testInfo) throws Exception {
Map<String, TestMessage> messageStorage = new ConcurrentHashMap<>();

var messageReceivedLatch = new CountDownLatch(3);

testCluster = new Cluster(3);
testCluster = new Cluster(3, testInfo);

for (ClusterService member : testCluster.members) {
member.messagingService().addMessageHandler(
Expand Down Expand Up @@ -128,8 +129,8 @@ public void messageWasSentToAllMembersSuccessfully() throws Exception {
* @throws Exception If failed.
*/
@Test
public void testShutdown() throws Exception {
testShutdown0(false);
public void testShutdown(TestInfo testInfo) throws Exception {
testShutdown0(testInfo, false);
}

/**
Expand All @@ -138,8 +139,8 @@ public void testShutdown() throws Exception {
* @throws Exception If failed.
*/
@Test
public void testForcefulShutdown() throws Exception {
testShutdown0(true);
public void testForcefulShutdown(TestInfo testInfo) throws Exception {
testShutdown0(testInfo, true);
}

/**
Expand All @@ -148,8 +149,8 @@ public void testForcefulShutdown() throws Exception {
* @throws Exception in case of errors.
*/
@Test
public void testSendMessageToSelf() throws Exception {
testCluster = new Cluster(1);
public void testSendMessageToSelf(TestInfo testInfo) throws Exception {
testCluster = new Cluster(1, testInfo);
testCluster.startAwait();

ClusterService member = testCluster.members.get(0);
Expand Down Expand Up @@ -196,8 +197,8 @@ private Data(TestMessage message, NetworkAddress sender, String correlationId) {
* @throws Exception in case of errors.
*/
@Test
public void testInvokeMessageToSelf() throws Exception {
testCluster = new Cluster(1);
public void testInvokeMessageToSelf(TestInfo testInfo) throws Exception {
testCluster = new Cluster(1, testInfo);
testCluster.startAwait();

ClusterService member = testCluster.members.get(0);
Expand Down Expand Up @@ -228,8 +229,8 @@ public void testInvokeMessageToSelf() throws Exception {
* the corresponding future completes exceptionally.
*/
@Test
public void testInvokeDuringStop() throws InterruptedException {
testCluster = new Cluster(2);
public void testInvokeDuringStop(TestInfo testInfo) throws InterruptedException {
testCluster = new Cluster(2, testInfo);
testCluster.startAwait();

ClusterService member0 = testCluster.members.get(0);
Expand Down Expand Up @@ -287,8 +288,8 @@ private static class MockNetworkMessage implements NetworkMessage, Serializable
* @throws Exception in case of errors.
*/
@Test
public void testMessageGroupsHandlers() throws Exception {
testCluster = new Cluster(2);
public void testMessageGroupsHandlers(TestInfo testInfo) throws Exception {
testCluster = new Cluster(2, testInfo);
testCluster.startAwait();

ClusterService node1 = testCluster.members.get(0);
Expand Down Expand Up @@ -337,11 +338,12 @@ public void testMessageGroupsHandlers() throws Exception {
/**
* Tests shutdown.
*
* @param testInfo Test info.
* @param forceful Whether shutdown should be forceful.
* @throws Exception If failed.
*/
private void testShutdown0(boolean forceful) throws Exception {
testCluster = new Cluster(2);
private void testShutdown0(TestInfo testInfo, boolean forceful) throws Exception {
testCluster = new Cluster(2, testInfo);
testCluster.startAwait();

ClusterService alice = testCluster.members.get(0);
Expand Down Expand Up @@ -423,8 +425,9 @@ private static final class Cluster {
* Creates a test cluster with the given amount of members.
*
* @param numOfNodes Amount of cluster members.
* @param testInfo Test info.
*/
Cluster(int numOfNodes) {
Cluster(int numOfNodes, TestInfo testInfo) {
startupLatch = new CountDownLatch(numOfNodes - 1);

int initialPort = 3344;
Expand All @@ -434,21 +437,24 @@ private static final class Cluster {
var isInitial = new AtomicBoolean(true);

members = nodeFinder.findNodes().stream()
.map(addr -> startNode(addr, nodeFinder, isInitial.getAndSet(false)))
.map(addr -> startNode(testInfo, addr, nodeFinder, isInitial.getAndSet(false)))
.collect(Collectors.toUnmodifiableList());
}

/**
* Start cluster node.
*
* @param testInfo Test info.
* @param addr Node address.
* @param nodeFinder Node finder.
* @param initial Whether this node is the first one.
* @return Started cluster node.
*/
private ClusterService startNode(NetworkAddress addr, NodeFinder nodeFinder, boolean initial) {
private ClusterService startNode(
TestInfo testInfo, NetworkAddress addr, NodeFinder nodeFinder, boolean initial
) {
ClusterService clusterSvc = ClusterServiceTestUtils.clusterService(
addr.toString(),
testInfo,
addr.port(),
nodeFinder,
serializationRegistry,
Expand Down
Expand Up @@ -31,6 +31,9 @@
import org.apache.ignite.network.NodeFinder;
import org.apache.ignite.network.TopologyService;
import org.apache.ignite.network.serialization.MessageSerializationRegistry;
import org.junit.jupiter.api.TestInfo;

import static org.apache.ignite.internal.testframework.IgniteTestUtils.testNodeName;

/**
* Test utils that provide sort of cluster service mock that manages required node configuration internally.
Expand All @@ -42,20 +45,20 @@ public class ClusterServiceTestUtils {
* Manages configuration manager lifecycle: on cluster service start starts node configuration manager,
* on cluster service stop - stops node configuration manager.
*
* @param nodeName Local name.
* @param testInfo Test info.
* @param port Local port.
* @param nodeFinder Node finder for discovering the initial cluster members.
* @param msgSerializationRegistry Message serialization registry.
* @param clusterSvcFactory Cluster service factory.
*/
public static ClusterService clusterService(
String nodeName,
TestInfo testInfo,
int port,
NodeFinder nodeFinder,
MessageSerializationRegistry msgSerializationRegistry,
ClusterServiceFactory clusterSvcFactory
) {
var ctx = new ClusterLocalConfiguration(nodeName, msgSerializationRegistry);
var ctx = new ClusterLocalConfiguration(testNodeName(testInfo, port), msgSerializationRegistry);

ConfigurationManager nodeConfigurationMgr = new ConfigurationManager(
Collections.singleton(NetworkConfiguration.KEY),
Expand Down
Expand Up @@ -70,21 +70,29 @@ void onMembershipEvent(MembershipEvent event) {
fireAppearedEvent(member);
}
else if (event.isRemoved()) {
members.compute(member.address(), // Ignore stale remove event.
(k, v) -> v.id().equals(member.id()) ? null : v);
members.compute(member.address(), (addr, node) -> {
// Ignore stale remove event.
if (node == null || node.id().equals(member.id()))
return null;
else
return node;
});

LOG.info("Node left: " + member);

fireDisappearedEvent(member);
}

StringBuilder snapshotMsg = new StringBuilder("Topology snapshot [nodes=").append(members.size()).append("]\n");
if (LOG.isInfoEnabled()) {
StringBuilder snapshotMsg = new StringBuilder("Topology snapshot [nodes=")
.append(members.size())
.append("]\n");

for (ClusterNode node : members.values()) {
snapshotMsg.append(" ^-- ").append(node).append('\n');
}
for (ClusterNode node : members.values())
snapshotMsg.append(" ^-- ").append(node).append('\n');

LOG.info(snapshotMsg.toString().trim());
LOG.info(snapshotMsg.toString().trim());
}
}

/**
Expand Down

0 comments on commit b40025b

Please sign in to comment.