-
Notifications
You must be signed in to change notification settings - Fork 18
fix: test and fix how we handle the POSTGRESQL_MINIMUM_IDLE_CONNECTIONS envvar #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
95 changes: 95 additions & 0 deletions
95
src/test/java/io/supertokens/storage/postgresql/test/EnvConfigTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright (c) 2025, VRAI Labs and/or its affiliates. All rights reserved. | ||
| * | ||
| * This software is licensed under the Apache License, Version 2.0 (the | ||
| * "License") as published by the Apache Software Foundation. | ||
| * | ||
| * You may not use this file except in compliance with the License. You may | ||
| * obtain a copy of the License at http://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 io.supertokens.storage.postgresql.test; | ||
|
|
||
| import com.google.gson.JsonObject; | ||
| import io.supertokens.storage.postgresql.Start; | ||
| import org.junit.Test; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| public class EnvConfigTest { | ||
|
|
||
| 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); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testUpdateConfigJsonFromEnvHandlesBoxedTypes() { | ||
| // Regression test: Integer fields like postgresql_minimum_idle_connections | ||
| // were silently dropped because updateConfigJsonFromEnv only checked | ||
| // primitive types (int.class) not boxed types (Integer.class). | ||
| Start start = new Start(); | ||
| try { | ||
| setEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS", "5"); | ||
|
|
||
| JsonObject configJson = new JsonObject(); | ||
| start.updateConfigJsonFromEnv(configJson); | ||
|
|
||
| assertTrue("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS env var should be loaded into configJson", | ||
| configJson.has("postgresql_minimum_idle_connections")); | ||
| assertEquals(5, configJson.get("postgresql_minimum_idle_connections").getAsInt()); | ||
| } finally { | ||
| removeEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testUpdateConfigJsonFromEnvHandlesBoxedTypeWithZero() { | ||
| // Tests that Integer value of 0 is correctly loaded (not confused with null/unset) | ||
| Start start = new Start(); | ||
| try { | ||
| setEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS", "0"); | ||
|
|
||
| JsonObject configJson = new JsonObject(); | ||
| start.updateConfigJsonFromEnv(configJson); | ||
|
|
||
| assertTrue("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS env var with value 0 should be loaded", | ||
| configJson.has("postgresql_minimum_idle_connections")); | ||
| assertEquals(0, configJson.get("postgresql_minimum_idle_connections").getAsInt()); | ||
| } finally { | ||
| removeEnv("POSTGRESQL_MINIMUM_IDLE_CONNECTIONS"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test relies on accessing an internal field named
"m"in the System.getenv() implementation. This field name is specific to certain JVM implementations and is not guaranteed across all Java versions or implementations. The test will fail at runtime on JVMs where the environment map implementation uses a different field name.Consider using a more portable approach for testing environment variable handling, such as:
Spotted by Graphite

Is this helpful? React 👍 or 👎 to let us know.