Skip to content

Commit cf902af

Browse files
committed
accounts/keystore: add acessors to account cache, to improve tests
1 parent d968f20 commit cf902af

File tree

3 files changed

+13
-16
lines changed

3 files changed

+13
-16
lines changed

accounts/keystore/account_cache.go

+8
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ func (ac *accountCache) deleteByFile(path string) {
146146
}
147147
}
148148

149+
// watcherStarted returns true if the watcher loop started running (even if it
150+
// has since also ended).
151+
func (ac *accountCache) watcherStarted() bool {
152+
ac.mu.Lock()
153+
defer ac.mu.Unlock()
154+
return ac.watcher.running || ac.watcher.runEnded
155+
}
156+
149157
func removeAccount(slice []accounts.Account, elem accounts.Account) []accounts.Account {
150158
for i := range slice {
151159
if slice[i] == elem {

accounts/keystore/account_cache_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@ func waitWatcherStart(ks *KeyStore) bool {
5858
}
5959
// The watcher should start, and then exit.
6060
for t0 := time.Now(); time.Since(t0) < 1*time.Second; time.Sleep(100 * time.Millisecond) {
61-
ks.cache.mu.Lock()
62-
watchOk := ks.cache.watcher.runEnded || ks.cache.watcher.running
63-
ks.cache.mu.Unlock()
64-
if watchOk {
61+
if ks.cache.watcherStarted() {
6562
return true
6663
}
6764
}

accounts/keystore/keystore_test.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,7 @@ func waitForKsUpdating(t *testing.T, ks *KeyStore, wantStatus bool, maxTime time
222222
t.Helper()
223223
// Wait max 250 ms, then return false
224224
for t0 := time.Now(); time.Since(t0) < maxTime; {
225-
ks.mu.RLock()
226-
updating := ks.updating
227-
ks.mu.RUnlock()
228-
if updating == wantStatus {
225+
if ks.isUpdating() == wantStatus {
229226
return true
230227
}
231228
time.Sleep(25 * time.Millisecond)
@@ -242,11 +239,8 @@ func TestWalletNotifierLifecycle(t *testing.T) {
242239

243240
// Ensure that the notification updater is not running yet
244241
time.Sleep(250 * time.Millisecond)
245-
ks.mu.RLock()
246-
updating := ks.updating
247-
ks.mu.RUnlock()
248242

249-
if updating {
243+
if ks.isUpdating() {
250244
t.Errorf("wallet notifier running without subscribers")
251245
}
252246
// Subscribe to the wallet feed and ensure the updater boots up
@@ -267,10 +261,8 @@ func TestWalletNotifierLifecycle(t *testing.T) {
267261
}
268262
// Check that it is still running
269263
time.Sleep(250 * time.Millisecond)
270-
ks.mu.RLock()
271-
updating = ks.updating
272-
ks.mu.RUnlock()
273-
if !updating {
264+
265+
if !ks.isUpdating() {
274266
t.Fatal("event notifier stopped prematurely")
275267
}
276268
// Unsubscribe the last one and ensure the updater terminates eventually.

0 commit comments

Comments
 (0)