-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
After use, destroy Dependent-scoped instances obtained via CdiLookupS…
…ervice
- Loading branch information
Showing
7 changed files
with
176 additions
and
11 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
server/implementation-cdi/src/main/java/io/smallrye/graphql/cdi/CDIManagedInstance.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,30 @@ | ||
package io.smallrye.graphql.cdi; | ||
|
||
import javax.enterprise.inject.Instance; | ||
|
||
import io.smallrye.graphql.spi.ManagedInstance; | ||
|
||
public class CDIManagedInstance<T> implements ManagedInstance<T> { | ||
|
||
private final Instance<T> instance; | ||
private final T object; | ||
private final boolean isDependentScoped; | ||
|
||
CDIManagedInstance(Instance<T> instance, boolean isDependentScoped) { | ||
this.instance = instance; | ||
this.isDependentScoped = isDependentScoped; | ||
this.object = instance.get(); | ||
} | ||
|
||
@Override | ||
public T get() { | ||
return object; | ||
} | ||
|
||
@Override | ||
public void destroyIfNecessary() { | ||
if (isDependentScoped) { | ||
instance.destroy(object); | ||
} | ||
} | ||
} |
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
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
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
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
9 changes: 9 additions & 0 deletions
9
server/implementation/src/main/java/io/smallrye/graphql/spi/ManagedInstance.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,9 @@ | ||
package io.smallrye.graphql.spi; | ||
|
||
public interface ManagedInstance<T> { | ||
|
||
T get(); | ||
|
||
void destroyIfNecessary(); | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...ration-tests/src/test/java/io/smallrye/graphql/tests/issue1307/DependentScopeApiTest.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,70 @@ | ||
package io.smallrye.graphql.tests.issue1307; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.URL; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import javax.annotation.PreDestroy; | ||
import javax.enterprise.context.Dependent; | ||
|
||
import org.eclipse.microprofile.graphql.GraphQLApi; | ||
import org.eclipse.microprofile.graphql.Query; | ||
import org.jboss.arquillian.container.test.api.Deployment; | ||
import org.jboss.arquillian.junit.Arquillian; | ||
import org.jboss.arquillian.test.api.ArquillianResource; | ||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.EmptyAsset; | ||
import org.jboss.shrinkwrap.api.spec.WebArchive; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import graphql.Assert; | ||
import io.smallrye.graphql.tests.GraphQLAssured; | ||
|
||
/** | ||
* Test to verify that a @Dependent-scoped GraphQL API will get each instance destroyed after use, | ||
* to prevent leaks | ||
*/ | ||
@RunWith(Arquillian.class) | ||
public class DependentScopeApiTest { | ||
|
||
@Deployment | ||
public static WebArchive deployment() { | ||
return ShrinkWrap.create(WebArchive.class, "dependent-scope-test.war") | ||
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") | ||
.addClasses(DependentScopedApi.class); | ||
} | ||
|
||
@ArquillianResource | ||
URL testingURL; | ||
|
||
@Test | ||
public void test() { | ||
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL); | ||
String response = graphQLAssured | ||
.post("query {hello}"); | ||
|
||
assertThat(response).isEqualTo("{\"data\":{\"hello\":\"hi\"}}"); | ||
Assert.assertTrue(DependentScopedApi.DESTROYED.get(), | ||
() -> "Instance of the API class has to be destroyed after each request"); | ||
} | ||
|
||
@GraphQLApi | ||
@Dependent | ||
public static class DependentScopedApi { | ||
|
||
public static AtomicBoolean DESTROYED = new AtomicBoolean(); | ||
|
||
@Query | ||
public String hello() { | ||
return "hi"; | ||
} | ||
|
||
@PreDestroy | ||
public void onDestroy() { | ||
DESTROYED.set(true); | ||
} | ||
|
||
} | ||
} |