Skip to content

Commit

Permalink
accounts/keystore: add acessors to account cache, to improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Oct 6, 2022
1 parent d968f20 commit cf902af
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
8 changes: 8 additions & 0 deletions accounts/keystore/account_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions accounts/keystore/account_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
16 changes: 4 additions & 12 deletions accounts/keystore/keystore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit cf902af

Please sign in to comment.