Skip to content

Commit

Permalink
quarkus: starts but beanManager is not accessible, something is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Dec 3, 2024
1 parent 8df7342 commit 8cdc591
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 15 deletions.
2 changes: 2 additions & 0 deletions recipes/java-recipes/quarkus-recipe/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ dependencies {
implementation(libs.couchbase.client)
implementation(libs.jackson.databind)
implementation(libs.jackson.core)
implementation(libs.logback.classic)
implementation(libs.slf4j.api)
implementation(projects.shared.application)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trendyol.stove.recipes.quarkus;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
Expand All @@ -8,9 +9,12 @@
@Path("/hello")
public class GreetingResource {

@Inject
HelloService helloService;

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello from Quarkus REST";
return helloService.hello();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.trendyol.stove.recipes.quarkus;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class HelloService {
public String hello() {
return "Hello from Quarkus Service";
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,71 @@
package com.trendyol.stove.recipes.quarkus;

import io.quarkus.arc.Arc;
import io.quarkus.dev.appstate.ApplicationStateNotification;
import io.quarkus.runtime.ApplicationLifecycleManager;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.annotations.QuarkusMain;
import io.quarkus.runtime.QuarkusApplication;
import jakarta.enterprise.inject.spi.BeanManager;

@QuarkusMain
public class QuarkusRecipeApp {
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;

import static io.quarkus.dev.appstate.ApplicationStateNotification.State.STARTED;

class QuarkusApp implements QuarkusApplication {
public static void main(String[] args) {
Quarkus.run(args);
var app = new QuarkusApp();
try {
app.run(args);
} catch (Exception e) {
Logger.getLogger(QuarkusApp.class.getName()).severe("Failed to start Quarkus: " + e.getMessage());
System.exit(-1);
}
}

@Override
public int run(String... args) {
QuarkusRecipeApp.run(args, "main");
return 0;
}
}


public class QuarkusRecipeApp {
public static BeanManager run(String[] args, String whereDoesItComeFrom) {
System.out.println("Where does it come from? " + whereDoesItComeFrom);

CompletableFuture<Boolean> startupComplete = new CompletableFuture<>();
ApplicationLifecycleManager.setAlreadyStartedCallback(started ->
startupComplete.complete(true)
);
Thread quarkusThread = new Thread(() -> {
Quarkus.run(args);
}, "quarkus-main");
quarkusThread.setDaemon(true);
quarkusThread.start();

try {
while (ApplicationStateNotification.getState() != STARTED) {
Thread.sleep(100);
}

while (ApplicationLifecycleManager.getCurrentApplication() == null) {
Thread.sleep(1000);
}

return Arc.container().beanManager();

} catch (Exception e) {
throw new RuntimeException("Failed to start Quarkus", e);
}
}

public static void stop() {
try {
Arc.shutdown();
} finally {
Quarkus.asyncExit();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
quarkus.profile=prod
quarkus.config.profile.parent=prod
quarkus.live-reload.enabled=false
quarkus.virtual-threads.enabled=true
quarkus.banner.enabled=false
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.trendyol.stove.recipes.quarkus.e2e.setup

import com.trendyol.stove.recipes.quarkus.QuarkusRecipeApp
import com.trendyol.stove.testing.e2e.bridge
import com.trendyol.stove.testing.e2e.http.*
import com.trendyol.stove.testing.e2e.system.*
import com.trendyol.stove.testing.e2e.system.abstractions.*
import com.trendyol.stove.testing.e2e.system.annotations.StoveDsl
import io.kotest.core.config.AbstractProjectConfig
import jakarta.enterprise.inject.spi.BeanManager
import kotlinx.coroutines.*

class Stove : AbstractProjectConfig() {
Expand All @@ -17,10 +19,12 @@ class Stove : AbstractProjectConfig() {
baseUrl = "http://localhost:8080"
)
}
bridge()
quarkus(
runner = { params ->
QuarkusRecipeApp.main(params)
}
QuarkusRecipeApp.run(params, "e2eTest")
},
withParameters = listOf()
)
}.run()
}
Expand All @@ -32,7 +36,7 @@ class Stove : AbstractProjectConfig() {

@StoveDsl
internal fun TestSystem.systemUnderTest(
runner: Runner<Unit>,
runner: Runner<BeanManager>,
withParameters: List<String> = listOf()
): ReadyTestSystem {
this.applicationUnderTest(QuarkusAppUnderTest(this, runner, withParameters))
Expand All @@ -41,25 +45,26 @@ internal fun TestSystem.systemUnderTest(

@StoveDsl
fun WithDsl.quarkus(
runner: Runner<Unit>,
runner: Runner<BeanManager>,
withParameters: List<String> = listOf()
): ReadyTestSystem = this.testSystem.systemUnderTest(runner, withParameters)

@Suppress("UNCHECKED_CAST")
class QuarkusAppUnderTest(
private val testSystem: TestSystem,
private val runner: Runner<Unit>,
private val runner: Runner<BeanManager>,
private val parameters: List<String>
) : ApplicationUnderTest<Unit> {
override suspend fun start(configurations: List<String>): Unit = coroutineScope {
) : ApplicationUnderTest<BeanManager> {
override suspend fun start(configurations: List<String>): BeanManager = coroutineScope {
val allConfigurations = (configurations + parameters).map { "--$it" }.toTypedArray()
runner(allConfigurations)
val di = runner(allConfigurations)
testSystem.activeSystems
.map { it.value }
.filter { it is RunnableSystemWithContext<*> || it is AfterRunAwareWithContext<*> }
.map { it as AfterRunAwareWithContext<Unit> }
.map { async(context = Dispatchers.IO) { it.afterRun(Unit) } }
.map { it as AfterRunAwareWithContext<BeanManager> }
.map { async(context = Dispatchers.IO) { it.afterRun(di) } }
.awaitAll()
di
}

override suspend fun stop(): Unit = Unit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.trendyol.stove.recipes.quarkus.e2e.tests

import com.trendyol.stove.recipes.quarkus.HelloService
import com.trendyol.stove.testing.e2e.http.http
import com.trendyol.stove.testing.e2e.system.TestSystem.Companion.validate
import com.trendyol.stove.testing.e2e.system.using
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

class IndexTests : FunSpec({
test("Index page should return 200") {
validate {
using<HelloService> {
println(this)
}

http {
get<String>(
"/hello",
Expand Down

0 comments on commit 8cdc591

Please sign in to comment.