Skip to content

Commit

Permalink
itest+lntest: make sure states are cleaned when tests end
Browse files Browse the repository at this point in the history
This commit changes how the node's state is updated to make sure the
test cleans up the node's state.

Also `testLookupHtlcResolution` is fixed with a cleanup.
  • Loading branch information
yyforyongyu authored and ellemouton committed Aug 17, 2023
1 parent 00c1c22 commit 9dfbdfd
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
1 change: 1 addition & 0 deletions itest/lnd_htlc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func testLookupHtlcResolution(ht *lntest.HarnessTest) {
cp := ht.OpenChannel(
alice, carol, lntest.OpenChannelParams{Amt: chanAmt},
)
defer ht.CloseChannel(alice, cp)

// Channel should be ready for payments.
const payAmt = 100
Expand Down
61 changes: 40 additions & 21 deletions lntest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,6 @@ func (h *HarnessTest) resetStandbyNodes(t *testing.T) {
// config for the coming test. This will also inherit the
// test's running context.
h.RestartNodeWithExtraArgs(hn, hn.Cfg.OriginalExtraArgs)

// Update the node's internal state.
hn.UpdateState()
}
}

Expand Down Expand Up @@ -433,8 +430,19 @@ func (h *HarnessTest) cleanupStandbyNode(hn *node.HarnessNode) {
// Delete all payments made from this test.
hn.RPC.DeleteAllPayments()

// Finally, check the node is in a clean state for the following tests.
h.validateNodeState(hn)
// Check the node's current state with timeout.
//
// NOTE: we need to do this in a `wait` because it takes some time for
// the node to update its internal state. Once the RPCs are synced we
// can then remove this wait.
err := wait.NoError(func() error {
// Update the node's internal state.
hn.UpdateState()

// Check the node is in a clean state for the following tests.
return h.validateNodeState(hn)
}, wait.DefaultTimeout)
require.NoError(h, err, "timeout checking node's state")
}

// removeConnectionns will remove all connections made on the standby nodes
Expand Down Expand Up @@ -752,32 +760,43 @@ func (h *HarnessTest) SetFeeEstimateWithConf(

// validateNodeState checks that the node doesn't have any uncleaned states
// which will affect its following tests.
func (h *HarnessTest) validateNodeState(hn *node.HarnessNode) {
errStr := func(subject string) string {
return fmt.Sprintf("%s: found %s channels, please close "+
func (h *HarnessTest) validateNodeState(hn *node.HarnessNode) error {
errStr := func(subject string) error {
return fmt.Errorf("%s: found %s channels, please close "+
"them properly", hn.Name(), subject)
}
// If the node still has open channels, it's most likely that the
// current test didn't close it properly.
require.Zerof(h, hn.State.OpenChannel.Active, errStr("active"))
require.Zerof(h, hn.State.OpenChannel.Public, errStr("public"))
require.Zerof(h, hn.State.OpenChannel.Private, errStr("private"))
require.Zerof(h, hn.State.OpenChannel.Pending, errStr("pending open"))
if hn.State.OpenChannel.Active != 0 {
return errStr("active")
}
if hn.State.OpenChannel.Public != 0 {
return errStr("public")
}
if hn.State.OpenChannel.Private != 0 {
return errStr("private")
}
if hn.State.OpenChannel.Pending != 0 {
return errStr("pending open")
}

// The number of pending force close channels should be zero.
require.Zerof(h, hn.State.CloseChannel.PendingForceClose,
errStr("pending force"))
if hn.State.CloseChannel.PendingForceClose != 0 {
return errStr("pending force")
}

// The number of waiting close channels should be zero.
require.Zerof(h, hn.State.CloseChannel.WaitingClose,
errStr("waiting close"))
if hn.State.CloseChannel.WaitingClose != 0 {
return errStr("waiting close")
}

// Ths number of payments should be zero.
// TODO(yy): no need to check since it's deleted in the cleanup? Or
// check it in a wait?
require.Zerof(h, hn.State.Payment.Total, "%s: found "+
"uncleaned payments, please delete all of them properly",
hn.Name())
if hn.State.Payment.Total != 0 {
return fmt.Errorf("%s: found uncleaned payments, please "+
"delete all of them properly", hn.Name())
}

return nil
}

// GetChanPointFundingTxid takes a channel point and converts it into a chain
Expand Down

0 comments on commit 9dfbdfd

Please sign in to comment.