testing: add CT (Custom Testing) structure; create uniq point for 'lnd process errors' and 'test panic/failed errors' handling

This commit is contained in:
Andrey Samokhvalov
2016-10-15 16:47:09 +03:00
parent b0525cf478
commit e6f45a948e
4 changed files with 264 additions and 168 deletions

View File

@@ -19,6 +19,8 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"bytes"
"github.com/go-errors/errors"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/roasbeef/btcd/chaincfg"
"github.com/roasbeef/btcd/rpctest"
@@ -159,14 +161,27 @@ func (l *lightningNode) genArgs() []string {
// start launches a new process running lnd. Additionally, the PID of the
// launched process is saved in order to possibly kill the process forcibly
// later.
func (l *lightningNode) start() error {
func (l *lightningNode) start(lndError chan error) error {
args := l.genArgs()
l.cmd = exec.Command("lnd", args...)
// Redirect stderr output to buffer
var errb bytes.Buffer
l.cmd.Stderr = &errb
if err := l.cmd.Start(); err != nil {
return err
}
go func() {
// If lightning node process exited with error status
// then we should transmit stderr output in main process.
if err := l.cmd.Wait(); err != nil {
lndError <- errors.New(errb.String())
}
}()
pid, err := os.Create(filepath.Join(l.cfg.DataDir,
fmt.Sprintf("%s.pid", l.nodeId)))
if err != nil {
@@ -234,7 +249,14 @@ func (l *lightningNode) cleanup() error {
// stop attempts to stop the active lnd process.
func (l *lightningNode) stop() error {
if l.cmd == nil || l.cmd.Process == nil {
// We should skip node stop in case:
// - start of the node wasn't initiated
// - process wasn't spawned
// - process already finished
if l.cmd == nil ||
l.cmd.Process == nil ||
(l.cmd.ProcessState != nil && l.cmd.ProcessState.Exited()) {
return nil
}
@@ -276,6 +298,10 @@ type networkHarness struct {
seenTxns chan wire.ShaHash
watchRequests chan *watchRequest
// Channel for transmitting stderr output from failed lightning node
// to main process.
lndErrorChan chan error
sync.Mutex
}
@@ -288,6 +314,7 @@ func newNetworkHarness() (*networkHarness, error) {
activeNodes: make(map[int]*lightningNode),
seenTxns: make(chan wire.ShaHash),
watchRequests: make(chan *watchRequest),
lndErrorChan: make(chan error),
}, nil
}
@@ -345,7 +372,7 @@ func (n *networkHarness) SetUp() error {
go func() {
var err error
defer wg.Done()
if err = n.Alice.start(); err != nil {
if err = n.Alice.start(n.lndErrorChan); err != nil {
errChan <- err
return
}
@@ -353,7 +380,7 @@ func (n *networkHarness) SetUp() error {
go func() {
var err error
defer wg.Done()
if err = n.Bob.start(); err != nil {
if err = n.Bob.start(n.lndErrorChan); err != nil {
errChan <- err
return
}
@@ -462,7 +489,7 @@ func (n *networkHarness) NewNode(extraArgs []string) (*lightningNode, error) {
return nil, err
}
if err := node.start(); err != nil {
if err := node.start(n.lndErrorChan); err != nil {
return nil, err
}