itest: defer shutdown of nodes in main test method

This ensures that the nodes will properly be shutdown even if one fails
to start or any of them fail to connect. Previously the shutdown is
defered only in the event that the setup was successful.
This commit is contained in:
Conner Fromknecht 2020-12-03 10:56:45 -08:00
parent 374725842e
commit 123c3a2530
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7

@ -44,9 +44,20 @@ type interceptorTestCase struct {
// 4. When Interceptor disconnects it resumes all held htlcs, which result in // 4. When Interceptor disconnects it resumes all held htlcs, which result in
// valid payment (invoice is settled). // valid payment (invoice is settled).
func testForwardInterceptor(net *lntest.NetworkHarness, t *harnessTest) { func testForwardInterceptor(net *lntest.NetworkHarness, t *harnessTest) {
// initialize the test context with 3 connected nodes. // Initialize the test context with 3 connected nodes.
testContext := newInterceptorTestContext(t, net) alice, err := net.NewNode("alice", nil)
defer testContext.shutdownNodes() require.NoError(t.t, err, "unable to create alice")
defer shutdownAndAssert(net, t, alice)
bob, err := net.NewNode("bob", nil)
require.NoError(t.t, err, "unable to create bob")
defer shutdownAndAssert(net, t, alice)
carol, err := net.NewNode("carol", nil)
require.NoError(t.t, err, "unable to create carol")
defer shutdownAndAssert(net, t, alice)
testContext := newInterceptorTestContext(t, net, alice, bob, carol)
const ( const (
chanAmt = btcutil.Amount(300000) chanAmt = btcutil.Amount(300000)
@ -222,24 +233,17 @@ type interceptorTestContext struct {
} }
func newInterceptorTestContext(t *harnessTest, func newInterceptorTestContext(t *harnessTest,
net *lntest.NetworkHarness) *interceptorTestContext { net *lntest.NetworkHarness,
alice, bob, carol *lntest.HarnessNode) *interceptorTestContext {
ctxb := context.Background() ctxb := context.Background()
// Create a three-node context consisting of Alice, Bob and Carol
alice, err := net.NewNode("alice", nil)
require.NoError(t.t, err, "unable to create alice")
bob, err := net.NewNode("bob", nil)
require.NoError(t.t, err, "unable to create bob")
carol, err := net.NewNode("carol", nil)
require.NoError(t.t, err, "unable to create carol")
// Connect nodes // Connect nodes
nodes := []*lntest.HarnessNode{alice, bob, carol} nodes := []*lntest.HarnessNode{alice, bob, carol}
for i := 0; i < len(nodes); i++ { for i := 0; i < len(nodes); i++ {
for j := i + 1; j < len(nodes); j++ { for j := i + 1; j < len(nodes); j++ {
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout) ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
err = net.EnsureConnected(ctxt, nodes[i], nodes[j]) err := net.EnsureConnected(ctxt, nodes[i], nodes[j])
require.NoError(t.t, err, "unable to connect nodes") require.NoError(t.t, err, "unable to connect nodes")
} }
} }
@ -331,12 +335,6 @@ func (c *interceptorTestContext) closeChannels() {
} }
} }
func (c *interceptorTestContext) shutdownNodes() {
shutdownAndAssert(c.net, c.t, c.alice)
shutdownAndAssert(c.net, c.t, c.bob)
shutdownAndAssert(c.net, c.t, c.carol)
}
func (c *interceptorTestContext) waitForChannels() { func (c *interceptorTestContext) waitForChannels() {
ctxb := context.Background() ctxb := context.Background()