mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
WIP fix flakey wallet notifier test
This commit is contained in:
parent
37590b2c55
commit
b69da295d3
2 changed files with 25 additions and 10 deletions
|
|
@ -72,6 +72,8 @@ type KeyStore struct {
|
||||||
|
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
importMu sync.Mutex // Import Mutex locks the import to prevent two insertions from racing
|
importMu sync.Mutex // Import Mutex locks the import to prevent two insertions from racing
|
||||||
|
|
||||||
|
addedWallets map[string]struct{} // Track addedWallets wallet URLs to prevent duplicates
|
||||||
}
|
}
|
||||||
|
|
||||||
type unlocked struct {
|
type unlocked struct {
|
||||||
|
|
@ -95,6 +97,7 @@ func (ks *KeyStore) init(keydir string) {
|
||||||
// Initialize the set of unlocked keys and the account cache
|
// Initialize the set of unlocked keys and the account cache
|
||||||
ks.unlocked = make(map[common.Address]*unlocked)
|
ks.unlocked = make(map[common.Address]*unlocked)
|
||||||
ks.cache, ks.changes = newAccountCache(keydir)
|
ks.cache, ks.changes = newAccountCache(keydir)
|
||||||
|
ks.addedWallets = make(map[string]struct{})
|
||||||
|
|
||||||
// TODO: In order for this finalizer to work, there must be no references
|
// TODO: In order for this finalizer to work, there must be no references
|
||||||
// to ks. addressCache doesn't keep a reference but unlocked keys do,
|
// to ks. addressCache doesn't keep a reference but unlocked keys do,
|
||||||
|
|
@ -147,6 +150,13 @@ func (ks *KeyStore) refreshWallets() {
|
||||||
if len(ks.wallets) == 0 || ks.wallets[0].URL().Cmp(account.URL) > 0 {
|
if len(ks.wallets) == 0 || ks.wallets[0].URL().Cmp(account.URL) > 0 {
|
||||||
wallet := &keystoreWallet{account: account, keystore: ks}
|
wallet := &keystoreWallet{account: account, keystore: ks}
|
||||||
|
|
||||||
|
// Only add a wallet once
|
||||||
|
if _, ok := ks.addedWallets[wallet.URL().String()]; !ok {
|
||||||
|
ks.addedWallets[wallet.URL().String()] = struct{}{}
|
||||||
|
} else {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived})
|
events = append(events, accounts.WalletEvent{Wallet: wallet, Kind: accounts.WalletArrived})
|
||||||
wallets = append(wallets, wallet)
|
wallets = append(wallets, wallet)
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package keystore
|
package keystore
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
@ -283,6 +284,8 @@ type walletEvent struct {
|
||||||
// Tests that wallet notifications and correctly fired when accounts are added
|
// Tests that wallet notifications and correctly fired when accounts are added
|
||||||
// or deleted from the keystore.
|
// or deleted from the keystore.
|
||||||
func TestWalletNotifications(t *testing.T) {
|
func TestWalletNotifications(t *testing.T) {
|
||||||
|
log.SetDefault(log.NewLogger(log.NewGlogHandler(log.NewTerminalHandler(os.Stderr, false))))
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
_, ks := tmpKeyStore(t)
|
_, ks := tmpKeyStore(t)
|
||||||
|
|
||||||
|
|
@ -292,16 +295,14 @@ func TestWalletNotifications(t *testing.T) {
|
||||||
updates = make(chan accounts.WalletEvent)
|
updates = make(chan accounts.WalletEvent)
|
||||||
sub = ks.Subscribe(updates)
|
sub = ks.Subscribe(updates)
|
||||||
)
|
)
|
||||||
|
|
||||||
defer sub.Unsubscribe()
|
defer sub.Unsubscribe()
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
defer wg.Done()
|
||||||
select {
|
for ev := range updates {
|
||||||
case ev := <-updates:
|
events = append(events, walletEvent{ev, ev.Wallet.Accounts()[0]})
|
||||||
events = append(events, walletEvent{ev, ev.Wallet.Accounts()[0]})
|
|
||||||
case <-sub.Err():
|
|
||||||
close(updates)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
@ -336,9 +337,13 @@ func TestWalletNotifications(t *testing.T) {
|
||||||
|
|
||||||
// Shut down the event collector and check events.
|
// Shut down the event collector and check events.
|
||||||
sub.Unsubscribe()
|
sub.Unsubscribe()
|
||||||
for ev := range updates {
|
if !waitForKsUpdating(t, ks, false, 4*time.Second) {
|
||||||
events = append(events, walletEvent{ev, ev.Wallet.Accounts()[0]})
|
t.Errorf("wallet notifier didn't terminate after unsubscribe")
|
||||||
}
|
}
|
||||||
|
// Wait for the event collection goroutine to finish
|
||||||
|
close(updates)
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
checkAccounts(t, live, ks.Wallets())
|
checkAccounts(t, live, ks.Wallets())
|
||||||
checkEvents(t, wantEvents, events)
|
checkEvents(t, wantEvents, events)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue