-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ArC: fix InstanceHandle.close() to behave as specified in strict mode
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
Showing
4 changed files
with
89 additions
and
7 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
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
81 changes: 81 additions & 0 deletions
81
...c/tests/src/test/java/io/quarkus/arc/test/instance/destroy/InstanceHandleDestroyTest.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,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); | ||
} | ||
} | ||
} |