Skip to content

Commit

Permalink
ArC: fix InstanceHandle.close() to behave as specified in strict mode
Browse files Browse the repository at this point in the history
This means that it delegates to `destroy()` not only for `@Dependent`
beans, but for all beans.

To avoid breaking existing users, this is only done in the strict mode.
  • Loading branch information
Ladicek committed May 29, 2023
1 parent 588f287 commit b50f56d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ default InjectableBean<T> getBean() {
*/
@Override
default void close() {
// https://github.com/quarkusio/quarkus/issues/33665
if (Arc.container().strictCompatibility()) {
destroy();
return;
}

InjectableBean<T> bean = getBean();
if (bean == null || Dependent.class.equals(bean.getScope())) {
destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,6 @@
<exclude name="testInterceptorsImplementInterceptorInterface"/>
</methods>
</class>
<class name="org.jboss.cdi.tck.tests.lookup.dynamic.handle.InstanceHandleTest">
<methods>
<exclude name="testGetHandle"/>
<exclude name="testHandles"/>
</methods>
</class>
<class name="org.jboss.cdi.tck.tests.context.dependent.DependentContextTest">
<methods>
<!-- https://github.com/jakartaee/cdi-tck/pull/452 -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.jupiter.api.extension.ExtensionContext;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcInitConfig;
import io.quarkus.arc.ComponentsProvider;
import io.quarkus.arc.ResourceReferenceProvider;
import io.quarkus.arc.processor.AlternativePriorities;
Expand Down Expand Up @@ -468,7 +469,7 @@ public void writeResource(Resource resource) throws IOException {
.setContextClassLoader(testClassLoader);

// Now we are ready to initialize Arc
Arc.initialize();
Arc.initialize(ArcInitConfig.builder().setStrictCompatibility(strictCompatibility).build());

} catch (Throwable e) {
if (shouldFail) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.quarkus.arc.test.instance.destroy;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.test.ArcTestContainer;

public class InstanceHandleDestroyTest {
@RegisterExtension
public ArcTestContainer container = new ArcTestContainer.Builder()
.beanClasses(MyDependentBean.class, MyAppScopedBean.class)
.strictCompatibility(true)
.build();

@Test
public void testDestroy() {
assertFalse(MyDependentBean.DESTROYED.get());
try (InstanceHandle<MyDependentBean> handle = Arc.container().instance(MyDependentBean.class)) {
assertNotNull(handle.get().toString());
}
assertTrue(MyDependentBean.DESTROYED.get());

// normal-scoped
String oldId;
assertFalse(MyAppScopedBean.DESTROYED.get());
try (InstanceHandle<MyAppScopedBean> handle = Arc.container().instance(MyAppScopedBean.class)) {
assertNotNull(handle.get().toString());
oldId = handle.get().getId();
}
assertTrue(MyAppScopedBean.DESTROYED.get());

String newId = Arc.container().instance(MyAppScopedBean.class).get().getId();
assertNotEquals(oldId, newId);
}

@Dependent
static class MyDependentBean {
static final AtomicBoolean DESTROYED = new AtomicBoolean(false);

@PreDestroy
void destroy() {
DESTROYED.set(true);
}
}

@ApplicationScoped
static class MyAppScopedBean {
static final AtomicBoolean DESTROYED = new AtomicBoolean(false);

String id;

String getId() {
return id;
}

@PostConstruct
void init() {
this.id = UUID.randomUUID().toString();
}

@PreDestroy
void destroy() {
DESTROYED.set(true);
}
}
}

0 comments on commit b50f56d

Please sign in to comment.