itest: use ProcessState and runCtx to control process quit
This commit is contained in:
1 file changed
+36
-22
+36
-22
@@ -298,10 +298,8 @@ type HarnessNode struct {
|
|||||||
pidFile string
|
pidFile string
|
||||||
logFile *os.File
|
logFile *os.File
|
||||||
|
|
||||||
// processExit is a channel that's closed once it's detected that the
|
// chanWatchRequests receives a request for watching a particular event
|
||||||
// process this instance of HarnessNode is bound to has exited.
|
// for a given channel.
|
||||||
processExit chan struct{}
|
|
||||||
|
|
||||||
chanWatchRequests chan *chanWatchRequest
|
chanWatchRequests chan *chanWatchRequest
|
||||||
|
|
||||||
// For each outpoint, we'll track an integer which denotes the number of
|
// For each outpoint, we'll track an integer which denotes the number of
|
||||||
@@ -742,7 +740,6 @@ func (hn *HarnessNode) start(lndBinary string, lndError chan<- error,
|
|||||||
|
|
||||||
// Launch a new goroutine which that bubbles up any potential fatal
|
// Launch a new goroutine which that bubbles up any potential fatal
|
||||||
// process errors to the goroutine running the tests.
|
// process errors to the goroutine running the tests.
|
||||||
hn.processExit = make(chan struct{})
|
|
||||||
hn.wg.Add(1)
|
hn.wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer hn.wg.Done()
|
defer hn.wg.Done()
|
||||||
@@ -752,9 +749,6 @@ func (hn *HarnessNode) start(lndBinary string, lndError chan<- error,
|
|||||||
lndError <- fmt.Errorf("%v\n%v", err, errb.String())
|
lndError <- fmt.Errorf("%v\n%v", err, errb.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Signal any onlookers that this process has exited.
|
|
||||||
close(hn.processExit)
|
|
||||||
|
|
||||||
// Make sure log file is closed and renamed if necessary.
|
// Make sure log file is closed and renamed if necessary.
|
||||||
finalizeLogfile()
|
finalizeLogfile()
|
||||||
|
|
||||||
@@ -765,7 +759,12 @@ func (hn *HarnessNode) start(lndBinary string, lndError chan<- error,
|
|||||||
|
|
||||||
// Write process ID to a file.
|
// Write process ID to a file.
|
||||||
if err := hn.writePidFile(); err != nil {
|
if err := hn.writePidFile(); err != nil {
|
||||||
hn.cmd.Process.Kill()
|
err = fmt.Errorf("writePidFile err: %w", err)
|
||||||
|
cmdErr := hn.cmd.Process.Kill()
|
||||||
|
if cmdErr != nil {
|
||||||
|
err = fmt.Errorf("kill process got err: %w: %v",
|
||||||
|
cmdErr, err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -775,12 +774,17 @@ func (hn *HarnessNode) start(lndBinary string, lndError chan<- error,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Since Stop uses the LightningClient to stop the node, if we fail to get a
|
// Since Stop uses the LightningClient to stop the node, if we fail to
|
||||||
// connected client, we have to kill the process.
|
// get a connected client, we have to kill the process.
|
||||||
useMacaroons := !hn.Cfg.HasSeed
|
useMacaroons := !hn.Cfg.HasSeed
|
||||||
conn, err := hn.ConnectRPC(useMacaroons)
|
conn, err := hn.ConnectRPC(useMacaroons)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hn.cmd.Process.Kill()
|
err = fmt.Errorf("ConnectRPC err: %w", err)
|
||||||
|
cmdErr := hn.cmd.Process.Kill()
|
||||||
|
if cmdErr != nil {
|
||||||
|
err = fmt.Errorf("kill process got err: %w: %v",
|
||||||
|
cmdErr, err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1246,7 +1250,7 @@ func (hn *HarnessNode) cleanup() error {
|
|||||||
// Stop attempts to stop the active lnd process.
|
// Stop attempts to stop the active lnd process.
|
||||||
func (hn *HarnessNode) stop() error {
|
func (hn *HarnessNode) stop() error {
|
||||||
// Do nothing if the process is not running.
|
// Do nothing if the process is not running.
|
||||||
if hn.processExit == nil {
|
if hn.runCtx == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1276,18 +1280,28 @@ func (hn *HarnessNode) stop() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for lnd process and other goroutines to exit.
|
|
||||||
select {
|
|
||||||
case <-hn.processExit:
|
|
||||||
case <-time.After(DefaultTimeout * 2):
|
|
||||||
return fmt.Errorf("process did not exit")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stop the runCtx and wait for goroutines to finish.
|
// Stop the runCtx and wait for goroutines to finish.
|
||||||
hn.cancel()
|
hn.cancel()
|
||||||
hn.wg.Wait()
|
|
||||||
|
|
||||||
hn.processExit = nil
|
// Wait for lnd process to exit.
|
||||||
|
err := wait.NoError(func() error {
|
||||||
|
if hn.cmd.ProcessState == nil {
|
||||||
|
return fmt.Errorf("process did not exit")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !hn.cmd.ProcessState.Exited() {
|
||||||
|
return fmt.Errorf("process did not exit")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for goroutines to be finished.
|
||||||
|
hn.wg.Wait()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}, DefaultTimeout*2)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
hn.LightningClient = nil
|
hn.LightningClient = nil
|
||||||
hn.WalletUnlockerClient = nil
|
hn.WalletUnlockerClient = nil
|
||||||
hn.Watchtower = nil
|
hn.Watchtower = nil
|
||||||
|
|||||||
Reference in new issue
Block a user