You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@grails.apache.org by "jamesfredley (via GitHub)" <gi...@apache.org> on 2026/06/30 17:46:11 UTC
[PR] Improve virtual thread cleanup for ThreadLocals [grails-core]
jamesfredley opened a new pull request, #15797:
URL: https://github.com/apache/grails-core/pull/15797
## Summary
- remove empty converter and datastore ThreadLocal holders when scoped state is cleared
- prevent SoftThreadLocalMap child threads from sharing parent map state
- add virtual-thread regression coverage for converter scoped configs, datastore deferred close lifecycle, and encoder chaining
## Verification
- `./gradlew :grails-converters:test --tests org.grails.web.converters.configuration.ConvertersConfigurationHolderSpec :grails-datastore-core:test --tests org.grails.datastore.mapping.core.DatastoreUtilsSpec :grails-encoder:test --tests org.grails.encoder.ChainedEncodersSpec`
- `./gradlew clean aggregateViolations :grails-test-report:check --continue`
- Combined report: 13623 tests, 0 failures, 0 errors, 267 skipped
- Unit report: 10729 tests, 0 failures, 0 errors, 258 skipped
- Integration/functional report: 2894 tests, 0 failures, 0 errors, 9 skipped
- Checkstyle, CodeNarc, PMD, SpotBugs: no violations found
- Review-gate: Oracle GREEN; Codex GREEN for source changes, with only untracked `.omo/` and `tmp-pr-response.md` artifact noise outside the shipping diff
## Performance/readiness notes
- Default converter lookup no longer allocates a per-thread map.
- Clearing the last scoped converter config removes the ThreadLocal entry.
- Deferred datastore close cleanup removes the ThreadLocal entry instead of leaving a null value.
- Existing encoder ThreadLocal cache remains intact to avoid an unmeasured regression.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve virtual thread cleanup for ThreadLocals [grails-core]
Posted by "testlens-app[bot] (via GitHub)" <gi...@apache.org>.
testlens-app[bot] commented on PR #15797:
URL: https://github.com/apache/grails-core/pull/15797#issuecomment-4847258594
## ✅ All tests passed ✅
🏷️ Commit: f52b3e19668e34f75cebfa3b50e08bb2922a6117
▶️ Tests: 9240 executed
⚪️ Checks: 44/44 completed
---
_Learn more about TestLens at [testlens.app](https://testlens.app)._
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve virtual thread cleanup for ThreadLocals [grails-core]
Posted by "jamesfredley (via GitHub)" <gi...@apache.org>.
jamesfredley commented on code in PR #15797:
URL: https://github.com/apache/grails-core/pull/15797#discussion_r3500966305
##########
grails-encoder/src/test/groovy/org/grails/encoder/ChainedEncodersSpec.groovy:
##########
@@ -97,4 +119,32 @@ class ChainedEncodersSpec extends Specification {
}
}
+
+ private static <T> T runOnVirtualThread(Closure<T> callable) {
+ def result = new AtomicReference<T>()
+ def error = new AtomicReference<Throwable>()
+ Runnable runnable = {
+ try {
Review Comment:
Addressed in f52b3e1966 by removing the duplicated runOnVirtualThread helper and using Java 21 virtual-thread executors directly in the specs, so the duplicated helper cannot drift. Focused tests and the full aggregate gate passed.
##########
grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/core/DatastoreUtilsSpec.groovy:
##########
@@ -39,4 +42,75 @@ class DatastoreUtilsSpec extends Specification {
env != null
resolver.getProperty('grails.foo') == 'baz'
}
+
+ void "deferred close lifecycle is isolated on a virtual thread"() {
+ given:
+ Datastore datastore = Mock()
+ Session session = Mock()
+ def repeatedCloseMessage = new AtomicReference<String>()
+
+ when:
+ runOnVirtualThread {
+ DatastoreUtils.initDeferredClose(datastore)
+ DatastoreUtils.closeSessionOrRegisterDeferredClose(session, datastore)
+ DatastoreUtils.processDeferredClose(datastore)
+ try {
+ DatastoreUtils.processDeferredClose(datastore)
+ }
+ catch (IllegalStateException e) {
+ repeatedCloseMessage.set(e.message)
+ }
+ }
+
+ then:
+ 1 * session.disconnect()
+ repeatedCloseMessage.get().contains('Deferred close not active')
+ }
+
+ void "soft thread local map does not expose parent entries to child threads"() {
+ given:
+ def threadLocal = new SoftThreadLocalMap()
+ threadLocal.get().put('tenant', 'parent')
+ def childTenant = new AtomicReference<String>()
+
+ when:
+ runOnVirtualThread {
+ childTenant.set(threadLocal.get().get('tenant') as String)
+ }
+
+ then:
+ childTenant.get() == null
+ threadLocal.get().get('tenant') == 'parent'
+
+ cleanup:
+ threadLocal.remove()
+ }
+
+ private static <T> T runOnVirtualThread(Closure<T> callable) {
+ def result = new AtomicReference<T>()
+ def error = new AtomicReference<Throwable>()
+ Runnable runnable = {
+ try {
Review Comment:
Addressed in f52b3e1966 by removing the duplicated runOnVirtualThread helper and using Java 21 virtual-thread executors directly in the specs, so the duplicated helper cannot drift. Focused tests and the full aggregate gate passed.
##########
grails-converters/src/test/groovy/org/grails/web/converters/configuration/ConvertersConfigurationHolderSpec.groovy:
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.grails.web.converters.configuration
+
+import java.util.concurrent.atomic.AtomicReference
+
+import grails.converters.JSON
+import grails.converters.XML
+
+import spock.lang.Specification
+
+class ConvertersConfigurationHolderSpec extends Specification {
+
+ void cleanup() {
+ ConvertersConfigurationHolder.clear()
+ }
+
+ void "thread-local converter configuration is scoped to a virtual thread"() {
+ given:
+ def defaultConfiguration = new DefaultConverterConfiguration<JSON>()
+ def scopedConfiguration = new DefaultConverterConfiguration<JSON>()
+ def scopedResult = new AtomicReference<ConverterConfiguration<JSON>>()
+ def clearedResult = new AtomicReference<ConverterConfiguration<JSON>>()
+ ConvertersConfigurationHolder.setDefaultConfiguration(JSON, defaultConfiguration)
+
+ when:
+ runOnVirtualThread {
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, scopedConfiguration)
+ scopedResult.set(ConvertersConfigurationHolder.getConverterConfiguration(JSON))
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, null)
+ clearedResult.set(ConvertersConfigurationHolder.getConverterConfiguration(JSON))
+ }
+
+ then:
+ scopedResult.get().is(scopedConfiguration)
+ clearedResult.get().is(defaultConfiguration)
+ ConvertersConfigurationHolder.getConverterConfiguration(JSON).is(defaultConfiguration)
+ }
+
+ void "default converter lookups do not allocate a thread-local configuration map"() {
+ given:
+ def defaultConfiguration = new DefaultConverterConfiguration<JSON>()
+ ConvertersConfigurationHolder.setDefaultConfiguration(JSON, defaultConfiguration)
+
+ expect:
+ ConvertersConfigurationHolder.getConverterConfiguration(JSON).is(defaultConfiguration)
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON) == null
+ }
+
+ void "clearing the last thread-local converter configuration removes the map"() {
+ given:
+ def scopedConfiguration = new DefaultConverterConfiguration<JSON>()
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, scopedConfiguration)
+
+ expect:
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON).is(scopedConfiguration)
+
+ when:
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, null)
+
+ then:
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON) == null
+ }
+
+ void "clearing one converter keeps other thread-local converter configurations"() {
+ given:
+ def jsonConfiguration = new DefaultConverterConfiguration<JSON>()
+ def xmlConfiguration = new DefaultConverterConfiguration<XML>()
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, jsonConfiguration)
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(XML, xmlConfiguration)
+
+ when:
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, null)
+
+ then:
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON) == null
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(XML).is(xmlConfiguration)
+
+ when:
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(XML, null)
+
+ then:
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(XML) == null
+ }
+
+ void "converter use restores a missing thread-local configuration after a named scope"() {
+ given:
+ def defaultConfiguration = new DefaultConverterConfiguration<JSON>()
+ def namedConfiguration = new DefaultConverterConfiguration<JSON>(defaultConfiguration)
+ ConvertersConfigurationHolder.setDefaultConfiguration(JSON, defaultConfiguration)
+ ConvertersConfigurationHolder.setNamedConverterConfiguration(JSON, 'named', namedConfiguration)
+ def scopedConfiguration = new AtomicReference<ConverterConfiguration<JSON>>()
+ def restoredConfiguration = new AtomicReference<ConverterConfiguration<JSON>>()
+
+ when:
+ JSON.use('named') {
+ scopedConfiguration.set(ConvertersConfigurationHolder.getConverterConfiguration(JSON))
+ }
+ restoredConfiguration.set(ConvertersConfigurationHolder.getConverterConfiguration(JSON))
+
+ then:
+ scopedConfiguration.get().is(namedConfiguration)
+ restoredConfiguration.get().is(defaultConfiguration)
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON) == null
+ }
+
+ private static <T> T runOnVirtualThread(Closure<T> callable) {
+ def result = new AtomicReference<T>()
+ def error = new AtomicReference<Throwable>()
+ Runnable runnable = {
+ try {
Review Comment:
Addressed in f52b3e1966 by removing the duplicated runOnVirtualThread helper and using Java 21 virtual-thread executors directly in the specs, so the duplicated helper cannot drift. Focused tests and the full aggregate gate passed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve virtual thread cleanup for ThreadLocals [grails-core]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #15797:
URL: https://github.com/apache/grails-core/pull/15797#issuecomment-4846839337
## [Codecov](https://app.codecov.io/gh/apache/grails-core/pull/15797?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:x: Patch coverage is `94.73684%` with `1 line` in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 49.4528%. Comparing base ([`bb8b1ee`](https://app.codecov.io/gh/apache/grails-core/commit/bb8b1ee03c14b2adbfdc7d928d432666495513da?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`f52b3e1`](https://app.codecov.io/gh/apache/grails-core/commit/f52b3e19668e34f75cebfa3b50e08bb2922a6117?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
:warning: Report is 21 commits behind head on 8.0.x.
| [Files with missing lines](https://app.codecov.io/gh/apache/grails-core/pull/15797?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
|---|---|---|
| [...s/configuration/ConvertersConfigurationHolder.java](https://app.codecov.io/gh/apache/grails-core/pull/15797?src=pr&el=tree&filepath=grails-converters%2Fsrc%2Fmain%2Fgroovy%2Forg%2Fgrails%2Fweb%2Fconverters%2Fconfiguration%2FConvertersConfigurationHolder.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Z3JhaWxzLWNvbnZlcnRlcnMvc3JjL21haW4vZ3Jvb3Z5L29yZy9ncmFpbHMvd2ViL2NvbnZlcnRlcnMvY29uZmlndXJhdGlvbi9Db252ZXJ0ZXJzQ29uZmlndXJhdGlvbkhvbGRlci5qYXZh) | 94.1176% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/apache/grails-core/pull/15797?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/grails-core/pull/15797?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## 8.0.x #15797 +/- ##
================================================
+ Coverage 0 49.4528% +49.4528%
- Complexity 0 16638 +16638
================================================
Files 0 1944 +1944
Lines 0 92379 +92379
Branches 0 16130 +16130
================================================
+ Hits 0 45684 +45684
- Misses 0 39597 +39597
- Partials 0 7098 +7098
```
| [Files with missing lines](https://app.codecov.io/gh/apache/grails-core/pull/15797?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
|---|---|---|
| [.../grails/datastore/mapping/core/DatastoreUtils.java](https://app.codecov.io/gh/apache/grails-core/pull/15797?src=pr&el=tree&filepath=grails-datastore-core%2Fsrc%2Fmain%2Fgroovy%2Forg%2Fgrails%2Fdatastore%2Fmapping%2Fcore%2FDatastoreUtils.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Z3JhaWxzLWRhdGFzdG9yZS1jb3JlL3NyYy9tYWluL2dyb292eS9vcmcvZ3JhaWxzL2RhdGFzdG9yZS9tYXBwaW5nL2NvcmUvRGF0YXN0b3JlVXRpbHMuamF2YQ==) | `62.7803% <100.0000%> (ø)` | |
| [...ils/datastore/mapping/core/SoftThreadLocalMap.java](https://app.codecov.io/gh/apache/grails-core/pull/15797?src=pr&el=tree&filepath=grails-datastore-core%2Fsrc%2Fmain%2Fgroovy%2Forg%2Fgrails%2Fdatastore%2Fmapping%2Fcore%2FSoftThreadLocalMap.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Z3JhaWxzLWRhdGFzdG9yZS1jb3JlL3NyYy9tYWluL2dyb292eS9vcmcvZ3JhaWxzL2RhdGFzdG9yZS9tYXBwaW5nL2NvcmUvU29mdFRocmVhZExvY2FsTWFwLmphdmE=) | `100.0000% <100.0000%> (ø)` | |
| [...s/configuration/ConvertersConfigurationHolder.java](https://app.codecov.io/gh/apache/grails-core/pull/15797?src=pr&el=tree&filepath=grails-converters%2Fsrc%2Fmain%2Fgroovy%2Forg%2Fgrails%2Fweb%2Fconverters%2Fconfiguration%2FConvertersConfigurationHolder.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Z3JhaWxzLWNvbnZlcnRlcnMvc3JjL21haW4vZ3Jvb3Z5L29yZy9ncmFpbHMvd2ViL2NvbnZlcnRlcnMvY29uZmlndXJhdGlvbi9Db252ZXJ0ZXJzQ29uZmlndXJhdGlvbkhvbGRlci5qYXZh) | `85.8974% <94.1176%> (ø)` | |
... and [1941 files with indirect coverage changes](https://app.codecov.io/gh/apache/grails-core/pull/15797/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve virtual thread cleanup for ThreadLocals [grails-core]
Posted by "jamesfredley (via GitHub)" <gi...@apache.org>.
jamesfredley commented on PR #15797:
URL: https://github.com/apache/grails-core/pull/15797#issuecomment-4847081572
@jdaugherty I checked the history and current usage. `ConvertersConfigurationHolder` has effectively been the singleton app-level holder since the converter configuration holder was introduced: it owns the default configuration and the named configurations for JSON/XML.
The `ThreadLocal` is not the app-wide store for those definitions. It is the per-execution override used by `JSON.use('name') { ... }` / `XML.use('name') { ... }`, and by `JSON.use('name')` / `XML.use('name')`, to select a named converter configuration for the current rendering path without changing the global default.
If the active converter config were only a singleton/global, a scoped call like `JSON.use('deep') { ... }` would temporarily change converter behavior process-wide. Concurrent requests or async rendering paths could then observe the wrong marshaller set, pretty-print setting, circular-reference behavior, etc. The ThreadLocal keeps that selected active config isolated to the current execution while the singleton holder still supplies the shared defaults and named configs.
So this PR is not introducing ThreadLocal state or changing that contract. It keeps the existing default/named singleton model, but makes the current-thread override cheaper and safer for virtual-thread usage by not allocating an empty map for default lookups and by removing the ThreadLocal entry once scoped state is cleared.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve virtual thread cleanup for ThreadLocals [grails-core]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #15797:
URL: https://github.com/apache/grails-core/pull/15797#discussion_r3500795446
##########
grails-encoder/src/test/groovy/org/grails/encoder/ChainedEncodersSpec.groovy:
##########
@@ -97,4 +119,32 @@ class ChainedEncodersSpec extends Specification {
}
}
+
+ private static <T> T runOnVirtualThread(Closure<T> callable) {
+ def result = new AtomicReference<T>()
+ def error = new AtomicReference<Throwable>()
+ Runnable runnable = {
+ try {
Review Comment:
`runOnVirtualThread` is duplicated verbatim in multiple test specs in this PR (here, DatastoreUtilsSpec, and ConvertersConfigurationHolderSpec). Keeping three independent copies increases the chance they drift (e.g., when adjusting reflection/fallback behavior) and makes future changes harder.
##########
grails-datastore-core/src/test/groovy/org/grails/datastore/mapping/core/DatastoreUtilsSpec.groovy:
##########
@@ -39,4 +42,75 @@ class DatastoreUtilsSpec extends Specification {
env != null
resolver.getProperty('grails.foo') == 'baz'
}
+
+ void "deferred close lifecycle is isolated on a virtual thread"() {
+ given:
+ Datastore datastore = Mock()
+ Session session = Mock()
+ def repeatedCloseMessage = new AtomicReference<String>()
+
+ when:
+ runOnVirtualThread {
+ DatastoreUtils.initDeferredClose(datastore)
+ DatastoreUtils.closeSessionOrRegisterDeferredClose(session, datastore)
+ DatastoreUtils.processDeferredClose(datastore)
+ try {
+ DatastoreUtils.processDeferredClose(datastore)
+ }
+ catch (IllegalStateException e) {
+ repeatedCloseMessage.set(e.message)
+ }
+ }
+
+ then:
+ 1 * session.disconnect()
+ repeatedCloseMessage.get().contains('Deferred close not active')
+ }
+
+ void "soft thread local map does not expose parent entries to child threads"() {
+ given:
+ def threadLocal = new SoftThreadLocalMap()
+ threadLocal.get().put('tenant', 'parent')
+ def childTenant = new AtomicReference<String>()
+
+ when:
+ runOnVirtualThread {
+ childTenant.set(threadLocal.get().get('tenant') as String)
+ }
+
+ then:
+ childTenant.get() == null
+ threadLocal.get().get('tenant') == 'parent'
+
+ cleanup:
+ threadLocal.remove()
+ }
+
+ private static <T> T runOnVirtualThread(Closure<T> callable) {
+ def result = new AtomicReference<T>()
+ def error = new AtomicReference<Throwable>()
+ Runnable runnable = {
+ try {
Review Comment:
`runOnVirtualThread` is duplicated verbatim in multiple test specs in this PR. Duplicated helpers tend to diverge over time and make it easy for one module’s tests to get updated while others are missed.
##########
grails-converters/src/test/groovy/org/grails/web/converters/configuration/ConvertersConfigurationHolderSpec.groovy:
##########
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.grails.web.converters.configuration
+
+import java.util.concurrent.atomic.AtomicReference
+
+import grails.converters.JSON
+import grails.converters.XML
+
+import spock.lang.Specification
+
+class ConvertersConfigurationHolderSpec extends Specification {
+
+ void cleanup() {
+ ConvertersConfigurationHolder.clear()
+ }
+
+ void "thread-local converter configuration is scoped to a virtual thread"() {
+ given:
+ def defaultConfiguration = new DefaultConverterConfiguration<JSON>()
+ def scopedConfiguration = new DefaultConverterConfiguration<JSON>()
+ def scopedResult = new AtomicReference<ConverterConfiguration<JSON>>()
+ def clearedResult = new AtomicReference<ConverterConfiguration<JSON>>()
+ ConvertersConfigurationHolder.setDefaultConfiguration(JSON, defaultConfiguration)
+
+ when:
+ runOnVirtualThread {
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, scopedConfiguration)
+ scopedResult.set(ConvertersConfigurationHolder.getConverterConfiguration(JSON))
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, null)
+ clearedResult.set(ConvertersConfigurationHolder.getConverterConfiguration(JSON))
+ }
+
+ then:
+ scopedResult.get().is(scopedConfiguration)
+ clearedResult.get().is(defaultConfiguration)
+ ConvertersConfigurationHolder.getConverterConfiguration(JSON).is(defaultConfiguration)
+ }
+
+ void "default converter lookups do not allocate a thread-local configuration map"() {
+ given:
+ def defaultConfiguration = new DefaultConverterConfiguration<JSON>()
+ ConvertersConfigurationHolder.setDefaultConfiguration(JSON, defaultConfiguration)
+
+ expect:
+ ConvertersConfigurationHolder.getConverterConfiguration(JSON).is(defaultConfiguration)
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON) == null
+ }
+
+ void "clearing the last thread-local converter configuration removes the map"() {
+ given:
+ def scopedConfiguration = new DefaultConverterConfiguration<JSON>()
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, scopedConfiguration)
+
+ expect:
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON).is(scopedConfiguration)
+
+ when:
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, null)
+
+ then:
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON) == null
+ }
+
+ void "clearing one converter keeps other thread-local converter configurations"() {
+ given:
+ def jsonConfiguration = new DefaultConverterConfiguration<JSON>()
+ def xmlConfiguration = new DefaultConverterConfiguration<XML>()
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, jsonConfiguration)
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(XML, xmlConfiguration)
+
+ when:
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(JSON, null)
+
+ then:
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON) == null
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(XML).is(xmlConfiguration)
+
+ when:
+ ConvertersConfigurationHolder.setThreadLocalConverterConfiguration(XML, null)
+
+ then:
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(XML) == null
+ }
+
+ void "converter use restores a missing thread-local configuration after a named scope"() {
+ given:
+ def defaultConfiguration = new DefaultConverterConfiguration<JSON>()
+ def namedConfiguration = new DefaultConverterConfiguration<JSON>(defaultConfiguration)
+ ConvertersConfigurationHolder.setDefaultConfiguration(JSON, defaultConfiguration)
+ ConvertersConfigurationHolder.setNamedConverterConfiguration(JSON, 'named', namedConfiguration)
+ def scopedConfiguration = new AtomicReference<ConverterConfiguration<JSON>>()
+ def restoredConfiguration = new AtomicReference<ConverterConfiguration<JSON>>()
+
+ when:
+ JSON.use('named') {
+ scopedConfiguration.set(ConvertersConfigurationHolder.getConverterConfiguration(JSON))
+ }
+ restoredConfiguration.set(ConvertersConfigurationHolder.getConverterConfiguration(JSON))
+
+ then:
+ scopedConfiguration.get().is(namedConfiguration)
+ restoredConfiguration.get().is(defaultConfiguration)
+ ConvertersConfigurationHolder.getThreadLocalConverterConfiguration(JSON) == null
+ }
+
+ private static <T> T runOnVirtualThread(Closure<T> callable) {
+ def result = new AtomicReference<T>()
+ def error = new AtomicReference<Throwable>()
+ Runnable runnable = {
+ try {
Review Comment:
`runOnVirtualThread` is duplicated verbatim in multiple test specs in this PR. Pulling it into a shared test helper would reduce duplication and avoid future inconsistencies when the helper needs to change.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Improve virtual thread cleanup for ThreadLocals [grails-core]
Posted by "jamesfredley (via GitHub)" <gi...@apache.org>.
jamesfredley merged PR #15797:
URL: https://github.com/apache/grails-core/pull/15797
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org