增加安全字段校验提示、请求预算和就绪/存活探针, 让绑定、数据库、Redis 和出站响应解码保留可信诊断上下文。 补充完整应用装配及逆序清理示例,同步接入和迁移文档。 验证:make verify、make race-check 全部通过。 BREAKING CHANGE: 普通 Controller 默认启用 30 秒协作式请求预算, 可用 request_timeout_ms: 0 或 max-exec-time:"0" 关闭; 字段校验失败的 400 响应增加安全字段明细。
This commit is contained in:
1 parent
b4c3ac622c
commit
3196e45a01
22 files changed
+1294
-79
No files matched your search
@@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/github/ginframework/database"
|
||||
"git.zhangdeman.cn/github/ginframework/httpclient"
|
||||
"git.zhangdeman.cn/github/ginframework/logger"
|
||||
cache "git.zhangdeman.cn/github/ginframework/redis"
|
||||
redisclient "github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/sqlite"
|
||||
)
|
||||
|
||||
func TestConfiguredDependencyFailureReleasesLogging(t *testing.T) {
|
||||
cfg := applicationConfig{Address: "127.0.0.1:0", LogDirectory: t.TempDir(), Redis: &cache.Config{}}
|
||||
if err := run(context.Background(), cfg); err == nil {
|
||||
t.Fatal("configured Redis failure was silently ignored")
|
||||
}
|
||||
if _, err := logger.ConfigureOutput(&logger.OutputConfig{Name: "default", Path: t.TempDir()}); err != nil {
|
||||
t.Fatal("startup failure leaked global logs", err)
|
||||
}
|
||||
if err := logger.CloseOutputs(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartialResourceCleanupClosesNativePools(t *testing.T) {
|
||||
connection, err := database.Open(context.Background(), database.Config{}, database.Options{Dialector: sqlite.Open(":memory:")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pool, err := connection.Session(context.Background()).DB()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redis := redisclient.NewClient(&redisclient.Options{Network: "unix", Addr: filepath.Join(t.TempDir(), "unused.sock")})
|
||||
outbound, err := httpclient.New(httpclient.Config{}, httpclient.Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pending := &resources{database: connection, redis: redis, outbound: outbound}
|
||||
if err = pending.close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pool.PingContext(context.Background()) == nil || redis.Ping(context.Background()).Err() == nil {
|
||||
t.Fatal("partial resources remained open")
|
||||
}
|
||||
if pending.database != nil || pending.redis != nil || pending.outbound != nil {
|
||||
t.Fatal("closed handles still published")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplicationReadinessAndSignalStyleShutdown(t *testing.T) {
|
||||
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address := listener.Addr().String()
|
||||
listener.Close()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
result := make(chan error, 1)
|
||||
go func() { result <- run(ctx, applicationConfig{Address: address, LogDirectory: t.TempDir()}) }()
|
||||
client := &http.Client{Timeout: 200 * time.Millisecond}
|
||||
ready := false
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
response, err := client.Get("http://" + address + "/_health/ready")
|
||||
if err == nil {
|
||||
response.Body.Close()
|
||||
if response.StatusCode == 200 {
|
||||
ready = true
|
||||
break
|
||||
}
|
||||
}
|
||||
select {
|
||||
case err := <-result:
|
||||
t.Fatalf("application exited before ready: %v", err)
|
||||
default:
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if !ready {
|
||||
cancel()
|
||||
<-result
|
||||
t.Fatal("application never became ready")
|
||||
}
|
||||
cancel()
|
||||
select {
|
||||
case err = <-result:
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("application failed to drain")
|
||||
}
|
||||
if _, err = logger.ConfigureOutput(&logger.OutputConfig{Name: "default", Path: t.TempDir()}); err != nil {
|
||||
t.Fatal("shutdown leaked logs", err)
|
||||
}
|
||||
if err = logger.CloseOutputs(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||