109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/http"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/ginframework/database"
|
|
"git.zhangdeman.cn/zhangdeman/ginframework/httpclient"
|
|
"git.zhangdeman.cn/zhangdeman/ginframework/logger"
|
|
cache "git.zhangdeman.cn/zhangdeman/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)
|
|
}
|
|
}
|