Skip to content

Commit 05b87f5

Browse files
authored
Merge pull request #10075 from yyforyongyu/fix-TestChangeWalletPasswordStateless
Fix flake in `TestChangeWalletPasswordStateless`
2 parents f09c7ae + 9039cd1 commit 05b87f5

File tree

1 file changed

+56
-37
lines changed

1 file changed

+56
-37
lines changed

walletunlocker/service_test.go

Lines changed: 56 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func openOrCreateTestMacStore(tempDir string, pw *[]byte,
140140
return store, nil
141141
}
142142

143-
// TestGenSeedUserEntropy tests that the gen seed method generates a valid
143+
// TestGenSeed tests that the gen seed method generates a valid
144144
// cipher seed mnemonic phrase and user provided source of entropy.
145145
func TestGenSeed(t *testing.T) {
146146
t.Parallel()
@@ -173,8 +173,8 @@ func TestGenSeed(t *testing.T) {
173173
require.NoError(t, err)
174174
}
175175

176-
// TestGenSeedInvalidEntropy tests that the gen seed method generates a valid
177-
// cipher seed mnemonic pass phrase even when the user doesn't provide its own
176+
// TestGenSeedGenerateEntropy tests that the gen seed method generates a valid
177+
// cipher seed mnemonic passphrase even when the user doesn't provide its own
178178
// source of entropy.
179179
func TestGenSeedGenerateEntropy(t *testing.T) {
180180
t.Parallel()
@@ -318,7 +318,7 @@ func TestInitWallet(t *testing.T) {
318318
require.Error(t, err)
319319
}
320320

321-
// TestInitWalletInvalidCipherSeed tests that if we attempt to create a wallet
321+
// TestCreateWalletInvalidEntropy tests that if we attempt to create a wallet
322322
// with an invalid cipher seed, then we'll receive an error.
323323
func TestCreateWalletInvalidEntropy(t *testing.T) {
324324
t.Parallel()
@@ -344,7 +344,7 @@ func TestCreateWalletInvalidEntropy(t *testing.T) {
344344
require.Error(t, err)
345345
}
346346

347-
// TestUnlockWallet checks that trying to unlock non-existing wallet fail, that
347+
// TestUnlockWallet checks that trying to unlock non-existing wallet fails, that
348348
// unlocking existing wallet with wrong passphrase fails, and that unlocking
349349
// existing wallet with correct passphrase succeeds.
350350
func TestUnlockWallet(t *testing.T) {
@@ -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)
@@ -518,11 +527,17 @@ func TestChangeWalletPasswordNewRootKey(t *testing.T) {
518527
t.Fatal("file exists but it shouldn't")
519528
}
520529
}
530+
531+
// Close the old db first.
532+
require.NoError(t, store.Backend.Close())
533+
534+
// Check that the new password can be used to open the db.
535+
assertPasswordChanged(t, testDir, req.NewPassword)
521536
}
522537

523538
// TestChangeWalletPasswordStateless checks that trying to change the password
524-
// of an existing wallet that was initialized stateless works when when the
525-
// --stateless_init flat is set. Also checks that if no password is given,
539+
// of an existing wallet that was initialized stateless works when the
540+
// --stateless_init flag is set. Also checks that if no password is given,
526541
// the default password is used.
527542
func TestChangeWalletPasswordStateless(t *testing.T) {
528543
t.Parallel()
@@ -594,7 +609,7 @@ func TestChangeWalletPasswordStateless(t *testing.T) {
594609
// async and then wait for the unlock message to arrive so we can send
595610
// back a fake macaroon.
596611
errChan := make(chan error, 1)
597-
go doChangePassword(service, testDir, req, errChan)
612+
go doChangePassword(service, req, errChan)
598613

599614
// Password and recovery window should be sent over the channel.
600615
select {
@@ -612,9 +627,24 @@ func TestChangeWalletPasswordStateless(t *testing.T) {
612627
case <-time.After(defaultTestTimeout):
613628
t.Fatalf("password not received")
614629
}
630+
631+
// Wait for the doChangePassword goroutine to finish.
632+
select {
633+
case err := <-errChan:
634+
require.NoError(t, err, "ChangePassword call failed")
635+
636+
case <-time.After(defaultTestTimeout):
637+
t.Fatalf("ChangePassword timed out")
638+
}
639+
640+
// Close the old db first.
641+
require.NoError(t, store.Backend.Close())
642+
643+
// Check that the new password can be used to open the db.
644+
assertPasswordChanged(t, testDir, req.NewPassword)
615645
}
616646

617-
func doChangePassword(service *walletunlocker.UnlockerService, testDir string,
647+
func doChangePassword(service *walletunlocker.UnlockerService,
618648
req *lnrpc.ChangePasswordRequest, errChan chan error) {
619649

620650
// When providing the correct wallet's current password and a new
@@ -630,37 +660,26 @@ func doChangePassword(service *walletunlocker.UnlockerService, testDir string,
630660
if !bytes.Equal(response.AdminMacaroon, testMac) {
631661
errChan <- fmt.Errorf("mismatched macaroon: expected %x, got "+
632662
"%x", testMac, response.AdminMacaroon)
663+
return
633664
}
634665

635-
// Close the macaroon DB and try to open it and read the root key with
636-
// the new password.
666+
close(errChan)
667+
}
668+
669+
// assertPasswordChanged asserts that the new password can be used to open the
670+
// store.
671+
func assertPasswordChanged(t *testing.T, testDir string, newPassword []byte) {
672+
// Open it and read the root key with the new password.
637673
store, err := openOrCreateTestMacStore(
638-
testDir, &testPassword, testNetParams,
674+
testDir, &newPassword, testNetParams,
639675
)
640-
if err != nil {
641-
errChan <- fmt.Errorf("could not create test store: %w", err)
642-
return
643-
}
644-
_, _, err = store.RootKey(defaultRootKeyIDContext)
645-
if err != nil {
646-
errChan <- fmt.Errorf("could not get root key: %w", err)
647-
return
648-
}
676+
require.NoError(t, err)
649677

650-
// Do cleanup now. Since we are in a go func, the defer at the top of
651-
// the outer would not work, because it would delete the directory
652-
// before we could check the content in here.
653-
err = store.Close()
654-
if err != nil {
655-
errChan <- fmt.Errorf("could not close store: %w", err)
656-
return
657-
}
678+
// Assert that we can read the root key.
679+
_, _, err = store.RootKey(defaultRootKeyIDContext)
680+
require.NoError(t, err)
658681

659-
// The backend database isn't closed automatically if the store is
660-
// closed, do that now manually.
661-
err = store.Backend.Close()
662-
if err != nil {
663-
errChan <- fmt.Errorf("could not close db: %w", err)
664-
return
665-
}
682+
// Close the db once done.
683+
require.NoError(t, store.Close())
684+
require.NoError(t, store.Backend.Close())
666685
}

0 commit comments

Comments
 (0)