watchtower: make use of t.Cleanup funcs in tests

Make use of the t.Cleanup helper function to clean up watchtower client
tests instead of relying on defer calls.
This commit is contained in:
Elle Mouton
2022-10-12 08:59:20 +02:00
parent 7274cf40d0
commit d29a55bbb5

View File

@@ -449,13 +449,16 @@ func newHarness(t *testing.T, cfg harnessCfg) *testHarness {
if err := server.Start(); err != nil { if err := server.Start(); err != nil {
t.Fatalf("Unable to start wtserver: %v", err) t.Fatalf("Unable to start wtserver: %v", err)
} }
t.Cleanup(func() {
_ = server.Stop()
})
if err = client.Start(); err != nil { if err = client.Start(); err != nil {
server.Stop()
t.Fatalf("Unable to start wtclient: %v", err) t.Fatalf("Unable to start wtclient: %v", err)
} }
t.Cleanup(client.ForceQuit)
if err := client.AddTower(towerAddr); err != nil { if err := client.AddTower(towerAddr); err != nil {
server.Stop()
t.Fatalf("Unable to add tower to wtclient: %v", err) t.Fatalf("Unable to add tower to wtclient: %v", err)
} }
@@ -979,7 +982,6 @@ var clientTests = []clientTest{
h.server.Stop() h.server.Stop()
h.serverCfg.NoAckUpdates = true h.serverCfg.NoAckUpdates = true
h.startServer() h.startServer()
defer h.server.Stop()
// Send the next state update to the tower. Since the // Send the next state update to the tower. Since the
// tower isn't acking state updates, we expect this // tower isn't acking state updates, we expect this
@@ -1000,12 +1002,10 @@ var clientTests = []clientTest{
h.server.Stop() h.server.Stop()
h.serverCfg.NoAckUpdates = false h.serverCfg.NoAckUpdates = false
h.startServer() h.startServer()
defer h.server.Stop()
// Restart the client and allow it to process the // Restart the client and allow it to process the
// committed update. // committed update.
h.startClient() h.startClient()
defer h.client.ForceQuit()
// Wait for the committed update to be accepted by the // Wait for the committed update to be accepted by the
// tower. // tower.
@@ -1052,7 +1052,6 @@ var clientTests = []clientTest{
h.server.Stop() h.server.Stop()
h.serverCfg.NoAckUpdates = true h.serverCfg.NoAckUpdates = true
h.startServer() h.startServer()
defer h.server.Stop()
// Now, queue the retributions for backup. // Now, queue the retributions for backup.
h.backupStates(chanID, 0, numUpdates, nil) h.backupStates(chanID, 0, numUpdates, nil)
@@ -1071,7 +1070,6 @@ var clientTests = []clientTest{
h.server.Stop() h.server.Stop()
h.serverCfg.NoAckUpdates = false h.serverCfg.NoAckUpdates = false
h.startServer() h.startServer()
defer h.server.Stop()
// Wait for all of the updates to be populated in the // Wait for all of the updates to be populated in the
// server's database. // server's database.
@@ -1215,13 +1213,11 @@ var clientTests = []clientTest{
h.server.Stop() h.server.Stop()
h.serverCfg.NoAckCreateSession = false h.serverCfg.NoAckCreateSession = false
h.startServer() h.startServer()
defer h.server.Stop()
// Restart the client with the same policy, which will // Restart the client with the same policy, which will
// immediately try to overwrite the old session with an // immediately try to overwrite the old session with an
// identical one. // identical one.
h.startClient() h.startClient()
defer h.client.ForceQuit()
// Now, queue the retributions for backup. // Now, queue the retributions for backup.
h.backupStates(chanID, 0, numUpdates, nil) h.backupStates(chanID, 0, numUpdates, nil)
@@ -1273,14 +1269,12 @@ var clientTests = []clientTest{
h.server.Stop() h.server.Stop()
h.serverCfg.NoAckCreateSession = false h.serverCfg.NoAckCreateSession = false
h.startServer() h.startServer()
defer h.server.Stop()
// Restart the client with a new policy, which will // Restart the client with a new policy, which will
// immediately try to overwrite the prior session with // immediately try to overwrite the prior session with
// the old policy. // the old policy.
h.clientCfg.Policy.SweepFeeRate *= 2 h.clientCfg.Policy.SweepFeeRate *= 2
h.startClient() h.startClient()
defer h.client.ForceQuit()
// Now, queue the retributions for backup. // Now, queue the retributions for backup.
h.backupStates(chanID, 0, numUpdates, nil) h.backupStates(chanID, 0, numUpdates, nil)
@@ -1341,7 +1335,6 @@ var clientTests = []clientTest{
// Restart the client with a new policy. // Restart the client with a new policy.
h.clientCfg.Policy.MaxUpdates = 20 h.clientCfg.Policy.MaxUpdates = 20
h.startClient() h.startClient()
defer h.client.ForceQuit()
// Now, queue the second half of the retributions. // Now, queue the second half of the retributions.
h.backupStates(chanID, numUpdates/2, numUpdates, nil) h.backupStates(chanID, numUpdates/2, numUpdates, nil)
@@ -1395,7 +1388,6 @@ var clientTests = []clientTest{
// maintained across restarts. // maintained across restarts.
h.client.Stop() h.client.Stop()
h.startClient() h.startClient()
defer h.client.ForceQuit()
// Try to back up the full range of retributions. Only // Try to back up the full range of retributions. Only
// the second half should actually be sent. // the second half should actually be sent.
@@ -1528,10 +1520,6 @@ func TestClient(t *testing.T) {
t.Parallel() t.Parallel()
h := newHarness(t, tc.cfg) h := newHarness(t, tc.cfg)
t.Cleanup(func() {
require.NoError(t, h.server.Stop())
h.client.ForceQuit()
})
tc.fn(h) tc.fn(h)
}) })