fix: min idle connection envvar 9.1#306
Merged
Merged
Conversation
Comment on lines
+30
to
+56
| private static void setEnv(String key, String value) { | ||
| try { | ||
| Map<String, String> env = System.getenv(); | ||
| Class<?> cl = env.getClass(); | ||
| Field field = cl.getDeclaredField("m"); | ||
| field.setAccessible(true); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, String> writableEnv = (Map<String, String>) field.get(env); | ||
| writableEnv.put(key, value); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Failed to set environment variable", e); | ||
| } | ||
| } | ||
|
|
||
| private static void removeEnv(String key) { | ||
| try { | ||
| Map<String, String> env = System.getenv(); | ||
| Class<?> cl = env.getClass(); | ||
| Field field = cl.getDeclaredField("m"); | ||
| field.setAccessible(true); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, String> writableEnv = (Map<String, String>) field.get(env); | ||
| writableEnv.remove(key); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Failed to remove environment variable", e); | ||
| } | ||
| } |
There was a problem hiding this comment.
The setEnv() and removeEnv() methods rely on accessing an internal field named "m" from the System.getenv() map class. This field name is JVM implementation-specific and may not exist in all JVM implementations (e.g., different vendors, GraalVM native image, etc.), causing the tests to fail with NoSuchFieldException.
// Current code assumes field "m" exists
Field field = cl.getDeclaredField("m");This will break the test suite on non-standard JVMs. Consider using a more robust approach like JUnit's @SetEnvironmentVariable from system-lambda library, or document that these tests only work on specific JVM implementations.
Suggested change
| private static void setEnv(String key, String value) { | |
| try { | |
| Map<String, String> env = System.getenv(); | |
| Class<?> cl = env.getClass(); | |
| Field field = cl.getDeclaredField("m"); | |
| field.setAccessible(true); | |
| @SuppressWarnings("unchecked") | |
| Map<String, String> writableEnv = (Map<String, String>) field.get(env); | |
| writableEnv.put(key, value); | |
| } catch (Exception e) { | |
| throw new IllegalStateException("Failed to set environment variable", e); | |
| } | |
| } | |
| private static void removeEnv(String key) { | |
| try { | |
| Map<String, String> env = System.getenv(); | |
| Class<?> cl = env.getClass(); | |
| Field field = cl.getDeclaredField("m"); | |
| field.setAccessible(true); | |
| @SuppressWarnings("unchecked") | |
| Map<String, String> writableEnv = (Map<String, String>) field.get(env); | |
| writableEnv.remove(key); | |
| } catch (Exception e) { | |
| throw new IllegalStateException("Failed to remove environment variable", e); | |
| } | |
| } | |
| private static void setEnv(String key, String value) { | |
| try { | |
| Map<String, String> env = System.getenv(); | |
| Class<?> cl = env.getClass(); | |
| Field field = null; | |
| // Try common field names used by different JVM implementations | |
| String[] possibleFieldNames = {"m", "theUnmodifiableEnvironment", "theEnvironment"}; | |
| for (String fieldName : possibleFieldNames) { | |
| try { | |
| field = cl.getDeclaredField(fieldName); | |
| break; | |
| } catch (NoSuchFieldException e) { | |
| // Continue to next field name | |
| } | |
| } | |
| if (field == null) { | |
| throw new IllegalStateException("Unable to find environment map field in " + cl.getName() + | |
| ". This test may not work on this JVM implementation."); | |
| } | |
| field.setAccessible(true); | |
| @SuppressWarnings("unchecked") | |
| Map<String, String> writableEnv = (Map<String, String>) field.get(env); | |
| writableEnv.put(key, value); | |
| } catch (Exception e) { | |
| throw new IllegalStateException("Failed to set environment variable. Consider using system-lambda or similar library for better JVM compatibility.", e); | |
| } | |
| } | |
| private static void removeEnv(String key) { | |
| try { | |
| Map<String, String> env = System.getenv(); | |
| Class<?> cl = env.getClass(); | |
| Field field = null; | |
| // Try common field names used by different JVM implementations | |
| String[] possibleFieldNames = {"m", "theUnmodifiableEnvironment", "theEnvironment"}; | |
| for (String fieldName : possibleFieldNames) { | |
| try { | |
| field = cl.getDeclaredField(fieldName); | |
| break; | |
| } catch (NoSuchFieldException e) { | |
| // Continue to next field name | |
| } | |
| } | |
| if (field == null) { | |
| throw new IllegalStateException("Unable to find environment map field in " + cl.getName() + | |
| ". This test may not work on this JVM implementation."); | |
| } | |
| field.setAccessible(true); | |
| @SuppressWarnings("unchecked") | |
| Map<String, String> writableEnv = (Map<String, String>) field.get(env); | |
| writableEnv.remove(key); | |
| } catch (Exception e) { | |
| throw new IllegalStateException("Failed to remove environment variable. Consider using system-lambda or similar library for better JVM compatibility.", e); | |
| } | |
| } | |
Spotted by Graphite
Is this helpful? React 👍 or 👎 to let us know.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of change
(A few sentences about this PR)
Related issues
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your
changes work. Bonus points for screenshots and videos!)
Documentation changes
(If relevant, please create a PR in our docs repo, or create a checklist here
highlighting the necessary changes)
Checklist for important updates
pluginInterfaceSupported.jsonfile has been updated (if needed)build.gradlebuild.gradle, please make sure to add themin
implementationDependencies.json.git tag) in the formatvX.Y.Z, and then find thelatest branch (
git branch --all) whoseX.Yis greater than the latest released tag.OneMillionUsersTestRemaining TODOs for this PR