@@ -7,6 +7,7 @@ extern "C" {
77
88#include " zai_tests_common.hpp"
99
10+ #include < atomic>
1011#include < string>
1112
1213/* Use Catch2's expression decomposition: REQUIRE(a == b) prints both values on failure (like assert_eq! in Rust). */
@@ -565,21 +566,28 @@ TEST_INI("setting an env value after memoization for multiple ZAI config users",
565566#endif
566567
567568static char sapi_getenv_test_buf[64 ];
569+ static std::atomic<int > sapi_getenv_request_count{0 };
568570
569- // Returns "sapi env val" only for INI_FOO_STRING; otherwise NULL (so config falls back to cache).
571+ // For INI_FOO_STRING: first request (call) returns NULL (fall back to cache), second returns "sapi env val" .
570572TEA_SAPI_GETENV_FUNCTION (ini_sapi_getenv_from_sapi) {
571573 zai_str key = ZAI_STR_NEW (name, name_len);
572574 if (zai_str_eq (key, ZAI_STRL (" INI_FOO_STRING" ))) {
575+ int call = sapi_getenv_request_count.fetch_add (1 );
576+ if (call == 0 ) {
577+ return NULL ;
578+ }
573579 memset (sapi_getenv_test_buf, 0 , sizeof (sapi_getenv_test_buf));
574580 strcpy (sapi_getenv_test_buf, " sapi env val" );
575581 return sapi_getenv_test_buf;
576582 }
577583 return NULL ;
578584}
579585
580- TEST_INI (" SAPI env takes priority over cache" , {}, {
586+ TEST_INI (" SAPI env takes priority over cache" , {
587+ sapi_getenv_request_count.store (0 );
581588 REQUIRE_SETENV (" INI_FOO_STRING" , " system env val" );
582-
589+ tea_sapi_module.getenv = ini_sapi_getenv_from_sapi;
590+ }, {
583591 REQUEST_BEGIN ()
584592
585593 zval *value = zai_config_get_value (EXT_CFG_INI_FOO_STRING);
@@ -588,8 +596,6 @@ TEST_INI("SAPI env takes priority over cache", {}, {
588596
589597 REQUEST_END ()
590598
591- tea_sapi_module.getenv = ini_sapi_getenv_from_sapi;
592-
593599 REQUEST_BEGIN ()
594600
595601 zval *value = zai_config_get_value (EXT_CFG_INI_FOO_STRING);
0 commit comments