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

[WFARQ-185] Add a ReloadIfRequired annotation for ServerSetupTask's. #500

Merged
merged 1 commit into from
Oct 1, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.as.arquillian.api;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.jboss.as.arquillian.container.ManagementClient;

/**
* An annotation for a {@link ServerSetupTask} which indicates the server should be reloaded, if required, when the
* task completes its {@link ServerSetupTask#setup(ManagementClient, String)} or ends in an error. The same will happen
* for the {@link ServerSetupTask#tearDown(ManagementClient, String)}.
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface ReloadIfRequired {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.jboss.as.arquillian.container;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayDeque;
Expand Down Expand Up @@ -33,9 +34,11 @@
import org.jboss.arquillian.test.spi.event.enrichment.EnrichmentEvent;
import org.jboss.arquillian.test.spi.event.suite.AfterClass;
import org.jboss.arquillian.test.spi.event.suite.BeforeClass;
import org.jboss.as.arquillian.api.ReloadIfRequired;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.as.arquillian.api.ServerSetupTask;
import org.jboss.logging.Logger;
import org.wildfly.plugin.tools.server.ServerManager;

/**
* Observes the {@link BeforeDeploy}, {@link AfterUnDeploy} and {@link AfterClass} lifecycle events to ensure
Expand Down Expand Up @@ -64,6 +67,9 @@ public class ServerSetupObserver {
@Inject
private Event<EnrichmentEvent> enrichmentEvent;

@Inject
private Instance<ServerManager> serverManager;

private final Map<String, ServerSetupTaskHolder> setupTasks = new HashMap<>();
private boolean afterClassRun = false;

Expand Down Expand Up @@ -109,7 +115,7 @@ public synchronized void handleBeforeDeployment(@Observes BeforeDeploy event, Co
}

final ManagementClient client = managementClient.get();
final ServerSetupTaskHolder holder = new ServerSetupTaskHolder(client, container.getName());
final ServerSetupTaskHolder holder = new ServerSetupTaskHolder(serverManager.get(), client, container.getName());
executeSetup(holder, setup, containerName, event.getDeployment());
}

Expand Down Expand Up @@ -227,12 +233,15 @@ private void executeSetup(final ServerSetupTaskHolder holder, ServerSetup setup,
private class ServerSetupTaskHolder {

private final ManagementClient client;
private final ServerManager serverManager;
private final Deque<ServerSetupTask> setupTasks;
private final Set<DeploymentDescription> deployments;
private final String containerName;;

private ServerSetupTaskHolder(final ManagementClient client, final String containerName) {
private ServerSetupTaskHolder(final ServerManager serverManager, final ManagementClient client,
final String containerName) {
this.client = client;
this.serverManager = serverManager;
setupTasks = new ArrayDeque<>();
deployments = new HashSet<>();
this.containerName = containerName;
Expand All @@ -246,7 +255,13 @@ void setup(final ServerSetup setup, final String containerName) throws Throwable
final ServerSetupTask task = ctor.newInstance();
enrich(task, clazz.getMethod("setup", ManagementClient.class, String.class));
setupTasks.add(task);
task.setup(client, containerName);
try {
task.setup(client, containerName);
} finally {
if (task.getClass().isAnnotationPresent(ReloadIfRequired.class) && serverManager != null) {
serverManager.reloadIfRequired();
}
}
}
}

Expand All @@ -266,6 +281,14 @@ public void tearDown(final String containerName) {
// already been turned off; here we want to ensure all tear down work proceeds.

log.errorf(e, "Setup task failed during tear down. Offending class '%s'", task);
} finally {
if (task.getClass().isAnnotationPresent(ReloadIfRequired.class) && serverManager != null) {
try {
serverManager.reloadIfRequired();
} catch (IOException e) {
log.errorf(e, "Failed to reload server. The server may still be in reload-required state.");
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.wildfly.arquillian.integration.test.junit5.server.setup;

import java.io.IOException;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.as.arquillian.api.ContainerResource;
import org.jboss.as.arquillian.api.ReloadIfRequired;
import org.jboss.as.arquillian.api.ServerSetup;
import org.jboss.as.arquillian.api.ServerSetupTask;
import org.jboss.as.arquillian.container.ManagementClient;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.as.controller.client.helpers.Operations;
import org.jboss.dmr.ModelNode;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.wildfly.arquillian.junit.annotations.WildFlyArquillian;
import org.wildfly.plugin.tools.server.ServerManager;

/**
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
@WildFlyArquillian
@RunAsClient
@ServerSetup(ReloadIfRequiredAnnotationTestCase.TestSetupTask.class)
public class ReloadIfRequiredAnnotationTestCase {

@ReloadIfRequired
public static class TestSetupTask implements ServerSetupTask {
private final ModelNode address = Operations.createAddress("subsystem", "remoting");
private final String attributeName = "max-inbound-channels";
private volatile int currentValue;

@ContainerResource
private ServerManager serverManager;

@Override
public void setup(final ManagementClient client, final String containerId) throws Exception {
currentValue = executeOperation(client, Operations.createReadAttributeOperation(address, attributeName)).asInt();
// Increase the current value which should put the server in a state of reload-required
executeOperation(client, Operations.createWriteAttributeOperation(address, attributeName, currentValue + 10));
// Check the server state is in a reload required state
Assertions.assertEquals(ClientConstants.CONTROLLER_PROCESS_STATE_RELOAD_REQUIRED, serverManager.serverState());
}

@Override
public void tearDown(final ManagementClient managementClient, final String containerId) throws Exception {
// Reset the old value
executeOperation(managementClient, Operations.createWriteAttributeOperation(address, attributeName, currentValue));
// Check the server state is in a reload required state
Assertions.assertEquals(ClientConstants.CONTROLLER_PROCESS_STATE_RELOAD_REQUIRED, serverManager.serverState());
}
}

@ContainerResource
private static ManagementClient client;

@Deployment
public static WebArchive deployment() {
return ShrinkWrap.create(WebArchive.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}

@AfterAll
public static void checkControllerState() throws Exception {
// Check the server state is in a reload required state
checkServerStateIsRunning();
}

@Test
public void checkServerReloaded() throws Exception {
// Check the server state is not in a reload required state
checkServerStateIsRunning();
}

private static void checkServerStateIsRunning() throws IOException {
final ModelNode op = Operations.createReadAttributeOperation(new ModelNode().setEmptyList(), "server-state");
final ModelNode result = client.getControllerClient().execute(op);
if (Operations.isSuccessfulOutcome(result)) {
Assertions.assertEquals(ClientConstants.CONTROLLER_PROCESS_STATE_RUNNING, Operations.readResult(result)
.asString());
} else {
Assertions.fail("Checking the server state failed: " + Operations.getFailureDescription(result).asString());
}
}
}