From 7d463aedd3ad1a88964a4286f443c712164543ec Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Mon, 13 Apr 2026 20:10:56 +0800 Subject: [PATCH] accounts/keystore: fix flaky TestUpdatedKeyfileContents (#34084) TestUpdatedKeyfileContents was intermittently failing with: - Emptying account file failed - wasn't notified of new accounts Root cause: waitForAccounts required the account list match and an immediately readable ks.changes notification in the same instant, creating a timing race between cache update visibility and channel delivery. This change keeps the same timeout window but waits until both conditions are observed, which preserves test intent while removing the flaky timing dependency. Validation: - go test ./accounts/keystore -run '^TestUpdatedKeyfileContents$' -count=100 --- accounts/keystore/account_cache_test.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/accounts/keystore/account_cache_test.go b/accounts/keystore/account_cache_test.go index c9a8cdfcef..e49b110e7e 100644 --- a/accounts/keystore/account_cache_test.go +++ b/accounts/keystore/account_cache_test.go @@ -68,18 +68,27 @@ func waitWatcherStart(ks *KeyStore) bool { func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error { var list []accounts.Account + haveAccounts := false + haveChange := false for t0 := time.Now(); time.Since(t0) < 5*time.Second; time.Sleep(100 * time.Millisecond) { - list = ks.Accounts() - if reflect.DeepEqual(list, wantAccounts) { - // ks should have also received change notifications + if !haveAccounts { + list = ks.Accounts() + haveAccounts = reflect.DeepEqual(list, wantAccounts) + } + if !haveChange { select { case <-ks.changes: + haveChange = true default: - return errors.New("wasn't notified of new accounts") } + } + if haveAccounts && haveChange { return nil } } + if haveAccounts { + return errors.New("wasn't notified of new accounts") + } return fmt.Errorf("\ngot %v\nwant %v", list, wantAccounts) }