lntemp+lntest: use t.Cleanup to register cleanup functions

This commit is contained in:
yyforyongyu 2022-08-30 04:35:13 +08:00
parent ab62109865
commit 42eab93ba0
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868
6 changed files with 14 additions and 24 deletions

View File

@ -31,8 +31,7 @@ func TestFoo(t *testing.T) {
// Create a separate harness test for the testcase to // Create a separate harness test for the testcase to
// avoid overwriting the external harness test that is // avoid overwriting the external harness test that is
// tied to the parent test. // tied to the parent test.
ht, cleanup := harnessTest.Subtest(st) ht := harnessTest.Subtest(st)
defer cleanup()
// Run the test cases. // Run the test cases.
ht.RunTestCase(tc) ht.RunTestCase(tc)

View File

@ -279,7 +279,7 @@ func (h *HarnessTest) resetStandbyNodes(t *testing.T) {
// stand by nodes created by the parent test. It will return a cleanup function // stand by nodes created by the parent test. It will return a cleanup function
// which resets all the standby nodes' configs back to its original state and // which resets all the standby nodes' configs back to its original state and
// create snapshots of each nodes' internal state. // create snapshots of each nodes' internal state.
func (h *HarnessTest) Subtest(t *testing.T) (*HarnessTest, func()) { func (h *HarnessTest) Subtest(t *testing.T) *HarnessTest {
st := &HarnessTest{ st := &HarnessTest{
T: t, T: t,
manager: h.manager, manager: h.manager,
@ -304,7 +304,7 @@ func (h *HarnessTest) Subtest(t *testing.T) (*HarnessTest, func()) {
// Record block height. // Record block height.
_, startHeight := h.Miner.GetBestBlock() _, startHeight := h.Miner.GetBestBlock()
cleanup := func() { st.Cleanup(func() {
_, endHeight := h.Miner.GetBestBlock() _, endHeight := h.Miner.GetBestBlock()
st.Logf("finished test: %s, start height=%d, end height=%d, "+ st.Logf("finished test: %s, start height=%d, end height=%d, "+
@ -350,9 +350,9 @@ func (h *HarnessTest) Subtest(t *testing.T) (*HarnessTest, func()) {
// running cleanup again since its internal state has been // running cleanup again since its internal state has been
// cleaned up by its child harness tests. // cleaned up by its child harness tests.
h.cleaned = true h.cleaned = true
} })
return st, cleanup return st
} }
// shutdownNonStandbyNodes will shutdown any non-standby nodes. // shutdownNonStandbyNodes will shutdown any non-standby nodes.

View File

@ -401,8 +401,7 @@ func testChannelBackupRestoreBasic(ht *lntemp.HarnessTest) {
for _, testCase := range testCases { for _, testCase := range testCases {
tc := testCase tc := testCase
success := ht.Run(tc.name, func(t *testing.T) { success := ht.Run(tc.name, func(t *testing.T) {
h, cleanup := ht.Subtest(t) h := ht.Subtest(t)
defer cleanup()
runChanRestoreScenarioBasic(h, tc.restoreMethod) runChanRestoreScenarioBasic(h, tc.restoreMethod)
}) })
@ -445,17 +444,15 @@ func testChannelBackupRestoreUnconfirmed(ht *lntemp.HarnessTest) {
// Use the channel backup file that contains an unconfirmed channel and // Use the channel backup file that contains an unconfirmed channel and
// make sure recovery works as well. // make sure recovery works as well.
ht.Run("restore unconfirmed channel file", func(t *testing.T) { ht.Run("restore unconfirmed channel file", func(t *testing.T) {
st, cleanup := ht.Subtest(t) st := ht.Subtest(t)
runChanRestoreScenarioUnConfirmed(st, true) runChanRestoreScenarioUnConfirmed(st, true)
cleanup()
}) })
// Create a backup using RPC that contains an unconfirmed channel and // Create a backup using RPC that contains an unconfirmed channel and
// make sure recovery works as well. // make sure recovery works as well.
ht.Run("restore unconfirmed channel RPC", func(t *testing.T) { ht.Run("restore unconfirmed channel RPC", func(t *testing.T) {
st, cleanup := ht.Subtest(t) st := ht.Subtest(t)
runChanRestoreScenarioUnConfirmed(st, false) runChanRestoreScenarioUnConfirmed(st, false)
cleanup()
}) })
} }
@ -568,8 +565,7 @@ func testChannelBackupRestoreCommitTypes(ht *lntemp.HarnessTest) {
for _, testCase := range testCases { for _, testCase := range testCases {
tc := testCase tc := testCase
success := ht.Run(tc.name, func(t *testing.T) { success := ht.Run(tc.name, func(t *testing.T) {
h, cleanup := ht.Subtest(t) h := ht.Subtest(t)
defer cleanup()
runChanRestoreScenarioCommitTypes( runChanRestoreScenarioCommitTypes(
h, tc.ct, tc.zeroConf, h, tc.ct, tc.zeroConf,
@ -671,17 +667,15 @@ func testChannelBackupRestoreForceClose(ht *lntemp.HarnessTest) {
// Restore a channel that was force closed by dave just before going // Restore a channel that was force closed by dave just before going
// offline. // offline.
ht.Run("from backup file anchors", func(t *testing.T) { ht.Run("from backup file anchors", func(t *testing.T) {
st, cleanup := ht.Subtest(t) st := ht.Subtest(t)
runChanRestoreScenarioForceClose(st, false) runChanRestoreScenarioForceClose(st, false)
cleanup()
}) })
// Restore a zero-conf anchors channel that was force closed by dave // Restore a zero-conf anchors channel that was force closed by dave
// just before going offline. // just before going offline.
ht.Run("from backup file anchors w/ zero-conf", func(t *testing.T) { ht.Run("from backup file anchors w/ zero-conf", func(t *testing.T) {
st, cleanup := ht.Subtest(t) st := ht.Subtest(t)
runChanRestoreScenarioForceClose(st, true) runChanRestoreScenarioForceClose(st, true)
cleanup()
}) })
} }

View File

@ -127,8 +127,7 @@ test:
"carol_commit=%v,dave_commit=%v", cc, dc, "carol_commit=%v,dave_commit=%v", cc, dc,
) )
success := ht.Run(testName, func(t *testing.T) { success := ht.Run(testName, func(t *testing.T) {
st, cleanup := ht.Subtest(t) st := ht.Subtest(t)
defer cleanup()
testFunding(st, cc, dc) testFunding(st, cc, dc)
}) })

View File

@ -171,8 +171,7 @@ func runMultiHopHtlcClaimTest(ht *lntemp.HarnessTest, tester caseRunner) {
} }
s := ht.Run(name, func(t1 *testing.T) { s := ht.Run(name, func(t1 *testing.T) {
st, cleanup := ht.Subtest(t1) st := ht.Subtest(t1)
defer cleanup()
alice := st.NewNode("Alice", args) alice := st.NewNode("Alice", args)
bob := st.NewNode("Bob", args) bob := st.NewNode("Bob", args)

View File

@ -94,8 +94,7 @@ func TestLightningNetworkDaemonTemp(t *testing.T) {
// Create a separate harness test for the testcase to // Create a separate harness test for the testcase to
// avoid overwriting the external harness test that is // avoid overwriting the external harness test that is
// tied to the parent test. // tied to the parent test.
ht, cleanup := harnessTest.Subtest(t1) ht := harnessTest.Subtest(t1)
defer cleanup()
// TODO(yy): split log files. // TODO(yy): split log files.
cleanTestCaseName := strings.ReplaceAll( cleanTestCaseName := strings.ReplaceAll(