-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24539 from OndroMih/ondromih-executors-during-dep…
…loyment Tests to verify that tasks submitted to executor during deployment are executed
- Loading branch information
Showing
4 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...n/test/app/concurrency/executor/ManagedExecutorTasksSubmittedDuringDeploymentServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...plication/src/main/java/org/glassfish/main/test/app/concurrency/executor/StartupBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...main/test/app/concurrency/executor/ManagedExecutorTasksSubmittedDuringDeploymentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |