Skip to content

Commit 3a6e656

Browse files
committed
walletunlocker: wait for goroutine to finish before exit
Make sure we wait for `doChangePassword` goroutine to finish before exiting the test. We also remove the `openOrCreateTestMacStore` call inside `doChangePassword` as it was never finished.
1 parent da27748 commit 3a6e656

File tree

1 file changed

+22
-34
lines changed

1 file changed

+22
-34
lines changed

walletunlocker/service_test.go

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ func TestChangeWalletPasswordNewRootKey(t *testing.T) {
491491
// password that meets the length requirement, the password change
492492
// should succeed.
493493
errChan := make(chan error, 1)
494-
go doChangePassword(service, testDir, req, errChan)
494+
go doChangePassword(service, req, errChan)
495495

496496
// The new password should be sent over the channel.
497497
select {
@@ -510,6 +510,15 @@ func TestChangeWalletPasswordNewRootKey(t *testing.T) {
510510
t.Fatalf("password not received")
511511
}
512512

513+
// Wait for the doChangePassword goroutine to finish.
514+
select {
515+
case err := <-errChan:
516+
require.NoError(t, err, "ChangePassword call failed")
517+
518+
case <-time.After(defaultTestTimeout):
519+
t.Fatalf("ChangePassword timed out")
520+
}
521+
513522
// The files should no longer exist.
514523
for _, tempFile := range tempFiles {
515524
f, err := os.Open(tempFile)
@@ -594,7 +603,7 @@ func TestChangeWalletPasswordStateless(t *testing.T) {
594603
// async and then wait for the unlock message to arrive so we can send
595604
// back a fake macaroon.
596605
errChan := make(chan error, 1)
597-
go doChangePassword(service, testDir, req, errChan)
606+
go doChangePassword(service, req, errChan)
598607

599608
// Password and recovery window should be sent over the channel.
600609
select {
@@ -612,9 +621,18 @@ func TestChangeWalletPasswordStateless(t *testing.T) {
612621
case <-time.After(defaultTestTimeout):
613622
t.Fatalf("password not received")
614623
}
624+
625+
// Wait for the doChangePassword goroutine to finish.
626+
select {
627+
case err := <-errChan:
628+
require.NoError(t, err, "ChangePassword call failed")
629+
630+
case <-time.After(defaultTestTimeout):
631+
t.Fatalf("ChangePassword timed out")
632+
}
615633
}
616634

617-
func doChangePassword(service *walletunlocker.UnlockerService, testDir string,
635+
func doChangePassword(service *walletunlocker.UnlockerService,
618636
req *lnrpc.ChangePasswordRequest, errChan chan error) {
619637

620638
// When providing the correct wallet's current password and a new
@@ -633,35 +651,5 @@ func doChangePassword(service *walletunlocker.UnlockerService, testDir string,
633651
return
634652
}
635653

636-
// Close the macaroon DB and try to open it and read the root key with
637-
// the new password.
638-
store, err := openOrCreateTestMacStore(
639-
testDir, &req.NewPassword, testNetParams,
640-
)
641-
if err != nil {
642-
errChan <- fmt.Errorf("could not create test store: %w", err)
643-
return
644-
}
645-
_, _, err = store.RootKey(defaultRootKeyIDContext)
646-
if err != nil {
647-
errChan <- fmt.Errorf("could not get root key: %w", err)
648-
return
649-
}
650-
651-
// Do cleanup now. Since we are in a go func, the defer at the top of
652-
// the outer would not work, because it would delete the directory
653-
// before we could check the content in here.
654-
err = store.Close()
655-
if err != nil {
656-
errChan <- fmt.Errorf("could not close store: %w", err)
657-
return
658-
}
659-
660-
// The backend database isn't closed automatically if the store is
661-
// closed, do that now manually.
662-
err = store.Backend.Close()
663-
if err != nil {
664-
errChan <- fmt.Errorf("could not close db: %w", err)
665-
return
666-
}
654+
close(errChan)
667655
}

0 commit comments

Comments
 (0)