From 42eab93ba0f6a977578862ddaa5fde588d63355b Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Tue, 30 Aug 2022 04:35:13 +0800 Subject: [PATCH] lntemp+lntest: use `t.Cleanup` to register cleanup functions --- lntemp/README.md | 3 +-- lntemp/harness.go | 8 ++++---- lntest/itest/lnd_channel_backup_test.go | 18 ++++++------------ lntest/itest/lnd_funding_test.go | 3 +-- lntest/itest/lnd_multi-hop_test.go | 3 +-- lntest/itest/temp_lnd_test.go | 3 +-- 6 files changed, 14 insertions(+), 24 deletions(-) diff --git a/lntemp/README.md b/lntemp/README.md index 147589fdd..b4c7e65aa 100644 --- a/lntemp/README.md +++ b/lntemp/README.md @@ -31,8 +31,7 @@ func TestFoo(t *testing.T) { // Create a separate harness test for the testcase to // avoid overwriting the external harness test that is // tied to the parent test. - ht, cleanup := harnessTest.Subtest(st) - defer cleanup() + ht := harnessTest.Subtest(st) // Run the test cases. ht.RunTestCase(tc) diff --git a/lntemp/harness.go b/lntemp/harness.go index e64e2742f..5dff93f55 100644 --- a/lntemp/harness.go +++ b/lntemp/harness.go @@ -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 // which resets all the standby nodes' configs back to its original state and // 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{ T: t, manager: h.manager, @@ -304,7 +304,7 @@ func (h *HarnessTest) Subtest(t *testing.T) (*HarnessTest, func()) { // Record block height. _, startHeight := h.Miner.GetBestBlock() - cleanup := func() { + st.Cleanup(func() { _, endHeight := h.Miner.GetBestBlock() 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 // cleaned up by its child harness tests. h.cleaned = true - } + }) - return st, cleanup + return st } // shutdownNonStandbyNodes will shutdown any non-standby nodes. diff --git a/lntest/itest/lnd_channel_backup_test.go b/lntest/itest/lnd_channel_backup_test.go index a0ef6d5a5..dc539ae03 100644 --- a/lntest/itest/lnd_channel_backup_test.go +++ b/lntest/itest/lnd_channel_backup_test.go @@ -401,8 +401,7 @@ func testChannelBackupRestoreBasic(ht *lntemp.HarnessTest) { for _, testCase := range testCases { tc := testCase success := ht.Run(tc.name, func(t *testing.T) { - h, cleanup := ht.Subtest(t) - defer cleanup() + h := ht.Subtest(t) runChanRestoreScenarioBasic(h, tc.restoreMethod) }) @@ -445,17 +444,15 @@ func testChannelBackupRestoreUnconfirmed(ht *lntemp.HarnessTest) { // Use the channel backup file that contains an unconfirmed channel and // make sure recovery works as well. ht.Run("restore unconfirmed channel file", func(t *testing.T) { - st, cleanup := ht.Subtest(t) + st := ht.Subtest(t) runChanRestoreScenarioUnConfirmed(st, true) - cleanup() }) // Create a backup using RPC that contains an unconfirmed channel and // make sure recovery works as well. ht.Run("restore unconfirmed channel RPC", func(t *testing.T) { - st, cleanup := ht.Subtest(t) + st := ht.Subtest(t) runChanRestoreScenarioUnConfirmed(st, false) - cleanup() }) } @@ -568,8 +565,7 @@ func testChannelBackupRestoreCommitTypes(ht *lntemp.HarnessTest) { for _, testCase := range testCases { tc := testCase success := ht.Run(tc.name, func(t *testing.T) { - h, cleanup := ht.Subtest(t) - defer cleanup() + h := ht.Subtest(t) runChanRestoreScenarioCommitTypes( 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 // offline. ht.Run("from backup file anchors", func(t *testing.T) { - st, cleanup := ht.Subtest(t) + st := ht.Subtest(t) runChanRestoreScenarioForceClose(st, false) - cleanup() }) // Restore a zero-conf anchors channel that was force closed by dave // just before going offline. 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) - cleanup() }) } diff --git a/lntest/itest/lnd_funding_test.go b/lntest/itest/lnd_funding_test.go index 347219df4..140d93050 100644 --- a/lntest/itest/lnd_funding_test.go +++ b/lntest/itest/lnd_funding_test.go @@ -127,8 +127,7 @@ test: "carol_commit=%v,dave_commit=%v", cc, dc, ) success := ht.Run(testName, func(t *testing.T) { - st, cleanup := ht.Subtest(t) - defer cleanup() + st := ht.Subtest(t) testFunding(st, cc, dc) }) diff --git a/lntest/itest/lnd_multi-hop_test.go b/lntest/itest/lnd_multi-hop_test.go index 3e201e9b4..8f738dc7c 100644 --- a/lntest/itest/lnd_multi-hop_test.go +++ b/lntest/itest/lnd_multi-hop_test.go @@ -171,8 +171,7 @@ func runMultiHopHtlcClaimTest(ht *lntemp.HarnessTest, tester caseRunner) { } s := ht.Run(name, func(t1 *testing.T) { - st, cleanup := ht.Subtest(t1) - defer cleanup() + st := ht.Subtest(t1) alice := st.NewNode("Alice", args) bob := st.NewNode("Bob", args) diff --git a/lntest/itest/temp_lnd_test.go b/lntest/itest/temp_lnd_test.go index 5af1c2df3..40fa8e10a 100644 --- a/lntest/itest/temp_lnd_test.go +++ b/lntest/itest/temp_lnd_test.go @@ -94,8 +94,7 @@ func TestLightningNetworkDaemonTemp(t *testing.T) { // Create a separate harness test for the testcase to // avoid overwriting the external harness test that is // tied to the parent test. - ht, cleanup := harnessTest.Subtest(t1) - defer cleanup() + ht := harnessTest.Subtest(t1) // TODO(yy): split log files. cleanTestCaseName := strings.ReplaceAll(