Skip to content

Commit

Permalink
Merge pull request #24539 from OndroMih/ondromih-executors-during-dep…
Browse files Browse the repository at this point in the history
…loyment

Tests to verify that tasks submitted to executor during deployment are executed
  • Loading branch information
arjantijms authored Aug 24, 2023
2 parents f970ec8 + baf0629 commit e793b95
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
5 changes: 5 additions & 0 deletions appserver/tests/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<artifactId>jakarta.ejb-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.enterprise.concurrent</groupId>
<artifactId>jakarta.enterprise.concurrent-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.test.app.concurrency.executor;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import jakarta.ejb.EJB;
import java.io.IOException;

@WebServlet("/")
public class ManagedExecutorTasksSubmittedDuringDeploymentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public static final String HEADER_EXECUTED = "executed";

@EJB
StartupBean startupBean;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setHeader(HEADER_EXECUTED, String.valueOf(startupBean.isSubmittedTaskExecuted()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.test.app.concurrency.executor;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import jakarta.ejb.Singleton;
import jakarta.ejb.Startup;
import jakarta.enterprise.concurrent.ManagedExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;

@Singleton
@Startup
public class StartupBean {

private boolean submittedTaskExecuted = false;

@Resource
ManagedExecutorService executor;

public boolean isSubmittedTaskExecuted() {
return submittedTaskExecuted;
}

@PostConstruct
public void startup() {
Thread startupThread = Thread.currentThread();
executor.submit(() -> {
submittedTaskExecuted = true;
startupThread.interrupt();
});
try {
Thread.sleep(10000);
Logger.getLogger(StartupBean.class.getName()).log(Level.SEVERE, "Timeout reached waiting for submitted task to execute");
} catch (InterruptedException ex) {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2023 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.main.test.app.concurrency.executor;

import java.io.File;
import java.io.IOException;
import java.lang.System.Logger;
import java.net.HttpURLConnection;

import org.glassfish.main.itest.tools.GlassFishTestEnvironment;
import org.glassfish.main.itest.tools.asadmin.Asadmin;
import org.glassfish.main.itest.tools.asadmin.AsadminResult;
import org.glassfish.main.itest.tools.asadmin.AsadminResultMatcher;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static java.lang.System.Logger.Level.INFO;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ManagedExecutorTasksSubmittedDuringDeploymentTest {

private static final Logger LOG = System.getLogger(ManagedExecutorTasksSubmittedDuringDeploymentTest.class.getName());
private static final Asadmin ASADMIN = GlassFishTestEnvironment.getAsadmin();
private static final String APP_NAME = ManagedExecutorTasksSubmittedDuringDeploymentTest.class.getSimpleName();

@BeforeAll
static void deploy() {
File war = createDeployment();
try {
AsadminResult result = ASADMIN.exec("deploy", "--contextroot", "/", "--name", APP_NAME,
war.getAbsolutePath());
assertThat(result, AsadminResultMatcher.asadminOK());
} finally {
war.delete();
}
}


@AfterAll
static void undeploy() {
AsadminResult result = ASADMIN.exec("undeploy", APP_NAME);
assertThat(result, AsadminResultMatcher.asadminOK());
}


@Test
void testSubmittedTaskExecuted() throws Exception {
HttpURLConnection connection = GlassFishTestEnvironment.openConnection(8080, "/");
connection.setRequestMethod("GET");
assertEquals(200, connection.getResponseCode());
assertEquals("true", connection.getHeaderField(ManagedExecutorTasksSubmittedDuringDeploymentServlet.HEADER_EXECUTED));
}


private static File createDeployment() {
WebArchive war = ShrinkWrap.create(WebArchive.class, ManagedExecutorTasksSubmittedDuringDeploymentTest.class.getSimpleName() + ".war")
.addClasses(ManagedExecutorTasksSubmittedDuringDeploymentServlet.class,
StartupBean.class);
LOG.log(INFO, war.toString(true));
try {
File tempFile = File.createTempFile(APP_NAME, ".war");
war.as(ZipExporter.class).exportTo(tempFile, true);
return tempFile;
} catch (IOException e) {
throw new IllegalStateException("Deployment failed - cannot load the input archive!", e);
}
}
}

0 comments on commit e793b95

Please sign in to comment.