From cf902af07580c5dace0fb8a63a49ce5320dffdf9 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 6 Oct 2022 12:02:21 +0200 Subject: [PATCH] accounts/keystore: add acessors to account cache, to improve tests --- accounts/keystore/account_cache.go | 8 ++++++++ accounts/keystore/account_cache_test.go | 5 +---- accounts/keystore/keystore_test.go | 16 ++++------------ 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/accounts/keystore/account_cache.go b/accounts/keystore/account_cache.go index a3ec6e9c5606..db49ec349942 100644 --- a/accounts/keystore/account_cache.go +++ b/accounts/keystore/account_cache.go @@ -146,6 +146,14 @@ func (ac *accountCache) deleteByFile(path string) { } } +// watcherStarted returns true if the watcher loop started running (even if it +// has since also ended). +func (ac *accountCache) watcherStarted() bool { + ac.mu.Lock() + defer ac.mu.Unlock() + return ac.watcher.running || ac.watcher.runEnded +} + func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.Account { for i := range slice { if slice[i] == elem { diff --git a/accounts/keystore/account_cache_test.go b/accounts/keystore/account_cache_test.go index 231f2acb30a1..01db587d1599 100644 --- a/accounts/keystore/account_cache_test.go +++ b/accounts/keystore/account_cache_test.go @@ -58,10 +58,7 @@ func waitWatcherStart(ks *KeyStore) bool { } // The watcher should start, and then exit. for t0 := time.Now(); time.Since(t0) < 1*time.Second; time.Sleep(100 * time.Millisecond) { - ks.cache.mu.Lock() - watchOk := ks.cache.watcher.runEnded || ks.cache.watcher.running - ks.cache.mu.Unlock() - if watchOk { + if ks.cache.watcherStarted() { return true } } diff --git a/accounts/keystore/keystore_test.go b/accounts/keystore/keystore_test.go index 582d0adfde7e..f90d809b55f4 100644 --- a/accounts/keystore/keystore_test.go +++ b/accounts/keystore/keystore_test.go @@ -222,10 +222,7 @@ func waitForKsUpdating(t *testing.T, ks *KeyStore, wantStatus bool, maxTime time t.Helper() // Wait max 250 ms, then return false for t0 := time.Now(); time.Since(t0) < maxTime; { - ks.mu.RLock() - updating := ks.updating - ks.mu.RUnlock() - if updating == wantStatus { + if ks.isUpdating() == wantStatus { return true } time.Sleep(25 * time.Millisecond) @@ -242,11 +239,8 @@ func TestWalletNotifierLifecycle(t *testing.T) { // Ensure that the notification updater is not running yet time.Sleep(250 * time.Millisecond) - ks.mu.RLock() - updating := ks.updating - ks.mu.RUnlock() - if updating { + if ks.isUpdating() { t.Errorf("wallet notifier running without subscribers") } // Subscribe to the wallet feed and ensure the updater boots up @@ -267,10 +261,8 @@ func TestWalletNotifierLifecycle(t *testing.T) { } // Check that it is still running time.Sleep(250 * time.Millisecond) - ks.mu.RLock() - updating = ks.updating - ks.mu.RUnlock() - if !updating { + + if !ks.isUpdating() { t.Fatal("event notifier stopped prematurely") } // Unsubscribe the last one and ensure the updater terminates eventually.