test: 补充 WithEnv 环境变量注入的测试

验证 WithEnv 设置的变量能真正传进子进程,避免上层配置解析漏掉该
字段时无法被发现(此前 framework-v2 里 env 字段一直未接入,线上因
此从未真正生效过)。
This commit is contained in:
2026-07-23 13:48:34 +08:00
parent 983d106166
commit eb6ebd8632
2 changed files with 32 additions and 0 deletions
+6
View File
@@ -31,6 +31,12 @@ def add(a: int, b: int) -> int:
return a + b return a + b
@expose
def get_env(name: str) -> str:
"""读取当前进程环境变量,取不到时返回空字符串;用于验证 WithEnv 注入是否生效"""
return os.environ.get(name, "")
@expose @expose
def range_gen(start: int, stop: int) -> Iterator[int]: def range_gen(start: int, stop: int) -> Iterator[int]:
"""流式输出:对应 Go 侧 Invoke[chan int]""" """流式输出:对应 Go 侧 Invoke[chan int]"""
+26
View File
@@ -88,6 +88,32 @@ func TestPoolExhaustionBlocks(t *testing.T) {
} }
} }
// TestWithEnv 验证 WithEnv 设置的环境变量真的被传进了子进程,
// 而不是被静默忽略(回归:曾经上层 framework 的配置解析漏了 env 字段,
// 导致 yaml 里配的 env 从没真正生效过)。
func TestWithEnv(t *testing.T) {
pool := newTestPool(t, WithWorkers(1), WithMaxConns(1),
WithEnv("GOBRIDGE_TEST_VAR=hello-from-go"))
got, err := Invoke[string](context.Background(), pool, "get_env", "GOBRIDGE_TEST_VAR")
if err != nil {
t.Fatalf("Invoke get_env: %v", err)
}
if got != "hello-from-go" {
t.Fatalf("want %q, got %q", "hello-from-go", got)
}
// 没有通过 WithEnv 设置的变量应该读不到,避免测试因为宿主机碰巧存在
// 同名环境变量而误判通过。
got, err = Invoke[string](context.Background(), pool, "get_env", "GOBRIDGE_TEST_VAR_UNSET")
if err != nil {
t.Fatalf("Invoke get_env (unset): %v", err)
}
if got != "" {
t.Fatalf("want empty string for unset var, got %q", got)
}
}
// TestDefaultTimeout 验证 WithDefaultTimeout:仅在 ctx 未设置 deadline 时生效, // TestDefaultTimeout 验证 WithDefaultTimeout:仅在 ctx 未设置 deadline 时生效,
// 调用方显式传入的 deadline 优先级更高,不会被覆盖。 // 调用方显式传入的 deadline 优先级更高,不会被覆盖。
func TestDefaultTimeout(t *testing.T) { func TestDefaultTimeout(t *testing.T) {