feat: add generic phased background shutdown to managed services
This commit is contained in:
85
scripts/testdata/phasedMySQL_test.go
vendored
Normal file
85
scripts/testdata/phasedMySQL_test.go
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"example.com/mysqlAcceptance/internal/config"
|
||||
"example.com/mysqlAcceptance/internal/svc"
|
||||
"git.yuntuo.hk/k2safe/go-omni/lifecycle"
|
||||
"git.yuntuo.hk/k2safe/go-omni/logx"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestGeneratedMySQLBackgroundDrain(t *testing.T) {
|
||||
cfg, err := config.Load("etc/config.yaml")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
logger, err := logx.New(logx.Config{Writer: io.Discard})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer logger.Close()
|
||||
parent, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
c, err := svc.New(parent, logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer c.Close()
|
||||
stop := make(chan struct{})
|
||||
finished := make(chan error, 1)
|
||||
c.OnStop("writer", func(context.Context) error { close(stop); return nil })
|
||||
c.OnDrain("writer", func(context.Context) error { return <-finished })
|
||||
go func() {
|
||||
<-stop
|
||||
result := func() (err error) {
|
||||
ctx, end := context.WithTimeout(c.WorkContext(), 3*time.Second)
|
||||
defer end()
|
||||
conn, err := c.DB.Conn(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { err = errors.Join(err, conn.Close()) }()
|
||||
for _, query := range []string{"CREATE TEMPORARY TABLE omni_phase_records (id INT)", "INSERT INTO omni_phase_records VALUES (42)"} {
|
||||
if _, err = conn.ExecContext(ctx, query); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var n int
|
||||
if err = conn.QueryRowContext(ctx, "SELECT id FROM omni_phase_records").Scan(&n); err != nil {
|
||||
return err
|
||||
}
|
||||
if n != 42 {
|
||||
return errors.New("final background write missing")
|
||||
}
|
||||
_, err = conn.ExecContext(ctx, "DROP TEMPORARY TABLE omni_phase_records")
|
||||
return err
|
||||
}()
|
||||
// Completion follows release of the borrowed connection, including failures.
|
||||
finished <- result
|
||||
}()
|
||||
cancel()
|
||||
if err = c.Finish(nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = c.DB.PingContext(context.Background()); err == nil {
|
||||
t.Fatal("pool not closed after worker finished")
|
||||
}
|
||||
|
||||
held, err := svc.New(context.Background(), logger, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer held.DB.Close() // This test owns recovery and has no outstanding work.
|
||||
cause := errors.New("worker completion unproven")
|
||||
held.OnDrain("unjoined", func(context.Context) error { return cause })
|
||||
if err = held.Finish(nil); !errors.Is(err, cause) || !errors.Is(err, lifecycle.ErrResourcesRetained) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = held.DB.Health(context.Background()); err != nil {
|
||||
t.Fatal("unjoined worker lost shared database", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user