Skip to content

Commit

Permalink
clean up for unclosed connection
Browse files Browse the repository at this point in the history
  • Loading branch information
jennifersp committed Jan 25, 2024
1 parent 19da977 commit 5cb4626
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions testing/logictest/harness/doltgres_server_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"database/sql"
"fmt"
"io"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -45,28 +46,33 @@ var _ logictest.Harness = &DoltgresHarness{}

// DoltgresHarness is sqllogictest harness for doltgres databases.
type DoltgresHarness struct {
db *sql.DB
doltgresExec string
server *DoltgresServer
serverDir string
timeout int64 // in seconds
db *sql.DB
doltgresExec string
server *DoltgresServer
serverDir string
timeout int64 // in seconds
harnessLog *os.File
stashedLogOutput io.Writer
}

// NewDoltgresHarness returns a new Doltgres test harness for the data source name given.
// It starts doltgres server and handles every connection to it.
func NewDoltgresHarness(doltgresExec string, t int64) *DoltgresHarness {
serverDir := prepareSqlLogicTestDBAndGetServerDir(context.Background(), doltgresExec)
logFile, err := os.OpenFile(harnessLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
hl, err := os.OpenFile(harnessLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal(err)
}
log.SetOutput(logFile)
stashLogOutput := log.Writer()
log.SetOutput(hl)
logMsg("creating a new DoltgresHarness")

return &DoltgresHarness{
doltgresExec: doltgresExec,
serverDir: serverDir,
timeout: t,
doltgresExec: doltgresExec,
serverDir: serverDir,
timeout: t,
harnessLog: hl,
stashedLogOutput: stashLogOutput,
}
}

Expand All @@ -91,8 +97,11 @@ func (h *DoltgresHarness) Init() error {
}

func (s *DoltgresHarness) Close() error {
s.db.Close()
s.server.Close()
s.server = nil
s.harnessLog.Close()
log.SetOutput(s.stashedLogOutput)
return os.RemoveAll(s.serverDir)
}

Expand Down Expand Up @@ -215,10 +224,7 @@ func (h *DoltgresHarness) dropAllViews() error {
// startNewDoltgresServer stops the existing server if exists.
// It starts a new server and update the |server| of the harness.
func (h *DoltgresHarness) startNewDoltgresServer(ctx context.Context, newTestFile string) {
if h.server != nil {
h.server.Stop()
h.server = nil
}
h.ClearServer()

withKeyCtx, cancel := context.WithCancel(ctx)
gServer, serverCtx := errgroup.WithContext(withKeyCtx)
Expand Down Expand Up @@ -247,18 +253,33 @@ func (h *DoltgresHarness) startNewDoltgresServer(ctx context.Context, newTestFil
}()

doltgresServer := &DoltgresServer{
dir: h.serverDir,
quit: quit,
wg: &wg,
gServer: gServer,
server: server,
testFile: newTestFile,
dir: h.serverDir,
quit: quit,
wg: &wg,
gServer: gServer,
server: server,
testFile: newTestFile,
serverLog: l,
}

h.server = doltgresServer
h.server.Start()
}

// ClearServer closes the connection to the server and the server if either exists.
func (h *DoltgresHarness) ClearServer() {
if h.db != nil {
err := h.db.Close()
logErr(err, "closing connection")
h.db = nil
}
// close
if h.server != nil {
h.server.Close()
h.server = nil
}
}

func prepareSqlLogicTestDBAndGetServerDir(ctx context.Context, doltgresExec string) string {
cwd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -290,12 +311,13 @@ func prepareSqlLogicTestDBAndGetServerDir(ctx context.Context, doltgresExec stri
}

type DoltgresServer struct {
dir string
quit chan os.Signal
wg *sync.WaitGroup
gServer *errgroup.Group
server *exec.Cmd
testFile string
dir string
quit chan os.Signal
wg *sync.WaitGroup
gServer *errgroup.Group
server *exec.Cmd
testFile string
serverLog *os.File
}

func (s *DoltgresServer) Start() {
Expand Down Expand Up @@ -343,6 +365,10 @@ func (s *DoltgresServer) Stop() {

func (s *DoltgresServer) Close() {
s.Stop()
err := s.serverLog.Close()
if err != nil {
logErr(err, fmt.Sprintf("closing server.log file for server for %s", s.testFile))
}
}

func logErr(err error, cause string) {
Expand Down

0 comments on commit 5cb4626

Please sign in to comment.