feat: complete contract-driven business development and MySQL integration
This commit is contained in:
76
scripts/testdata/mysqlGenerated_test.go
vendored
Normal file
76
scripts/testdata/mysqlGenerated_test.go
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"example.com/mysqlAcceptance/internal/config"
|
||||
"example.com/mysqlAcceptance/internal/svc"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
)
|
||||
|
||||
func TestGeneratedMySQLConfigurationAndLifecycle(t *testing.T) {
|
||||
cfg, err := config.Load("etc/config.yaml")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cfg.Database.User != "omni_test" || cfg.Database.TLS.ServerName != "localhost" || cfg.Database.Pool.MaxOpenConns != 2 {
|
||||
t.Fatal("structured environment overlay was not applied")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
var logs bytes.Buffer
|
||||
logger, err := logx.New(logx.Config{Writer: &logs})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
first, err := svc.New(ctx, logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer first.Close()
|
||||
second, err := svc.New(ctx, logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer second.Close()
|
||||
if first.DB == second.DB {
|
||||
t.Fatal("contexts share a pool")
|
||||
}
|
||||
if err = first.DB.Health(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var key, cipher string
|
||||
if err = first.DB.QueryRowContext(ctx, "SHOW SESSION STATUS LIKE 'Ssl_cipher'").Scan(&key, &cipher); err != nil || cipher == "" {
|
||||
t.Fatal("generated pool has no TLS", err)
|
||||
}
|
||||
if err = first.Finish(nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = first.Finish(nil); err != nil {
|
||||
t.Fatal("cleanup not idempotent", err)
|
||||
}
|
||||
if err = first.DB.PingContext(ctx); err == nil {
|
||||
t.Fatal("dependency pool remains open")
|
||||
}
|
||||
if err = second.DB.Health(ctx); err != nil {
|
||||
t.Fatal("another context was closed", err)
|
||||
}
|
||||
bad := cfg
|
||||
bad.Database.TLS.ServerName = "wrong.invalid"
|
||||
failed, err := svc.New(ctx, logger, bad)
|
||||
var verify *tls.CertificateVerificationError
|
||||
if failed != nil || !errors.As(err, &verify) {
|
||||
t.Fatal("TLS failure not propagated", err)
|
||||
}
|
||||
logger.Info("logger remains usable")
|
||||
if !strings.Contains(logs.String(), "logger remains usable") {
|
||||
t.Fatal("borrowed logger was closed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user