diff --git a/example/worker.py b/example/worker.py index 61295ab..84e048c 100644 --- a/example/worker.py +++ b/example/worker.py @@ -31,6 +31,12 @@ def add(a: int, b: int) -> int: return a + b +@expose +def get_env(name: str) -> str: + """读取当前进程环境变量,取不到时返回空字符串;用于验证 WithEnv 注入是否生效""" + return os.environ.get(name, "") + + @expose def range_gen(start: int, stop: int) -> Iterator[int]: """流式输出:对应 Go 侧 Invoke[chan int]""" diff --git a/pool_test.go b/pool_test.go index 48e7246..fc1bfe2 100644 --- a/pool_test.go +++ b/pool_test.go @@ -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 时生效, // 调用方显式传入的 deadline 优先级更高,不会被覆盖。 func TestDefaultTimeout(t *testing.T) {