test: add a callback to networkHarness.RestartNode

This commit adds a callback to the RestartNode method on the network
harness in order to allow test authors to execute arbitrary logic
in-between the restart process for the node.
This commit is contained in:
Olaoluwa Osuntokun 2016-11-21 21:08:44 -06:00
parent 31e65e3333
commit 5a678d5beb
No known key found for this signature in database
GPG Key ID: 9CC5B105D03521A2
1 changed files with 21 additions and 9 deletions

View File

@ -125,7 +125,8 @@ func newLightningNode(rpcConfig *btcrpcclient.ConnConfig, lndArgs []string) (*li
numActiveNodes++ numActiveNodes++
return &lightningNode{cfg: cfg, return &lightningNode{
cfg: cfg,
p2pAddr: net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.PeerPort)), p2pAddr: net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.PeerPort)),
rpcAddr: net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.RPCPort)), rpcAddr: net.JoinHostPort("127.0.0.1", strconv.Itoa(cfg.RPCPort)),
rpcCert: rpcConfig.Certificates, rpcCert: rpcConfig.Certificates,
@ -186,12 +187,12 @@ func (l *lightningNode) start(lndError chan error) error {
}() }()
pid, err := os.Create(filepath.Join(l.cfg.DataDir, pid, err := os.Create(filepath.Join(l.cfg.DataDir,
fmt.Sprintf("%s.pid", l.nodeId))) fmt.Sprintf("%v.pid", l.nodeId)))
if err != nil { if err != nil {
return err return err
} }
l.pidFile = pid.Name() l.pidFile = pid.Name()
if _, err = fmt.Fprintf(pid, "%s\n", l.cmd.Process.Pid); err != nil { if _, err = fmt.Fprintf(pid, "%v\n", l.cmd.Process.Pid); err != nil {
return err return err
} }
if err := pid.Close(); err != nil { if err := pid.Close(); err != nil {
@ -265,8 +266,10 @@ func (l *lightningNode) stop() error {
// restart attempts to restart a lightning node by shutting it down cleanly, // restart attempts to restart a lightning node by shutting it down cleanly,
// then restarting the process. This function is fully blocking. Upon restart, // then restarting the process. This function is fully blocking. Upon restart,
// the RPC connection to the node will be re-attempted, continuing iff the // the RPC connection to the node will be re-attempted, continuing iff the
// connection attempt is successful. // connection attempt is successful. Additionally, if a callback is passed, the
func (l *lightningNode) restart(errChan chan error) error { // closure will be executed after the node has been shutdown, but before the
// process has been started up again.
func (l *lightningNode) restart(errChan chan error, callback func() error) error {
if err := l.stop(); err != nil { if err := l.stop(); err != nil {
return nil return nil
} }
@ -275,6 +278,12 @@ func (l *lightningNode) restart(errChan chan error) error {
l.processExit = make(chan struct{}) l.processExit = make(chan struct{})
if callback != nil {
if err := callback(); err != nil {
return err
}
}
return l.start(errChan) return l.start(errChan)
} }
@ -539,13 +548,15 @@ func (n *networkHarness) ConnectNodes(ctx context.Context, a, b *lightningNode)
// RestartNode attempts to restart a lightning node by shutting it down // RestartNode attempts to restart a lightning node by shutting it down
// cleanly, then restarting the process. This function is fully blocking. Upon // cleanly, then restarting the process. This function is fully blocking. Upon
// restart, the RPC connection to the node will be re-attempted, continuing iff // restart, the RPC connection to the node will be re-attempted, continuing iff
// the connection attempt is successful. // the connection attempt is successful. If the callback parameter is non-nil,
// then the function will be executed after the node shuts down, but *before*
// the process has been started up again.
// //
// This method can be useful when testing edge cases such as a node broadcast // This method can be useful when testing edge cases such as a node broadcast
// and invalidated prior state, or persistent state recovery, simulating node // and invalidated prior state, or persistent state recovery, simulating node
// crashes, etc. // crashes, etc.
func (n *networkHarness) RestartNode(node *lightningNode) error { func (n *networkHarness) RestartNode(node *lightningNode, callback func() error) error {
return node.restart(n.lndErrorChan) return node.restart(n.lndErrorChan, callback)
} }
// TODO(roasbeef): add a WithChannel higher-order function? // TODO(roasbeef): add a WithChannel higher-order function?
@ -615,7 +626,8 @@ func (n *networkHarness) OnTxAccepted(hash *wire.ShaHash, amt btcutil.Amount) {
// WaitForTxBroadcast blocks until the target txid is seen on the network. If // WaitForTxBroadcast blocks until the target txid is seen on the network. If
// the transaction isn't seen within the network before the passed timeout, // the transaction isn't seen within the network before the passed timeout,
// then an error is returend. // then an error is returned.
// TODO(roasbeef): add another method which creates queue of all seen transactions
func (n *networkHarness) WaitForTxBroadcast(ctx context.Context, txid wire.ShaHash) error { func (n *networkHarness) WaitForTxBroadcast(ctx context.Context, txid wire.ShaHash) error {
eventChan := make(chan struct{}) eventChan := make(chan struct{})