fix: flaky test TestController_watchAccountChanges by making sure

to check the database transfer after removal event is processed.
This commit is contained in:
Ivan Belyakov 2024-03-03 08:59:21 +01:00 committed by IvanBelyakoff
parent bdb2b261a6
commit 440779fc8c
2 changed files with 33 additions and 32 deletions

View File

@ -150,8 +150,8 @@ func (c *Controller) startAccountWatcher(chainIDs []uint64) {
c.accWatcher = accountsevent.NewWatcher(c.accountsDB, c.accountFeed, func(changedAddresses []common.Address, eventType accountsevent.EventType, currentAddresses []common.Address) { c.accWatcher = accountsevent.NewWatcher(c.accountsDB, c.accountFeed, func(changedAddresses []common.Address, eventType accountsevent.EventType, currentAddresses []common.Address) {
c.onAccountsChanged(changedAddresses, eventType, currentAddresses, chainIDs) c.onAccountsChanged(changedAddresses, eventType, currentAddresses, chainIDs)
}) })
c.accWatcher.Start()
} }
c.accWatcher.Start()
} }
func (c *Controller) onAccountsChanged(changedAddresses []common.Address, eventType accountsevent.EventType, currentAddresses []common.Address, chainIDs []uint64) { func (c *Controller) onAccountsChanged(changedAddresses []common.Address, eventType accountsevent.EventType, currentAddresses []common.Address, chainIDs []uint64) {

View File

@ -80,26 +80,12 @@ func TestController_watchAccountsChanges(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, ranges) require.NotNil(t, ranges)
ch := make(chan accountsevent.Event) c.accWatcher = accountsevent.NewWatcher(c.accountsDB, c.accountFeed, func(changedAddresses []common.Address, eventType accountsevent.EventType, currentAddresses []common.Address) {
// Subscribe for account changes c.onAccountsChanged(changedAddresses, eventType, currentAddresses, []uint64{chainID})
accountFeed.Subscribe(ch)
// Watching accounts must start before sending event. // Quit channel event handler before destroying the channel
// To avoid running goroutine immediately, use any delay.
go func() { go func() {
time.Sleep(1 * time.Millisecond) time.Sleep(1 * time.Millisecond)
accountFeed.Send(accountsevent.Event{
Type: accountsevent.EventTypeRemoved,
Accounts: []common.Address{address},
})
}()
c.startAccountWatcher([]uint64{chainID})
// Wait for event
<-ch
// Wait for DB to be cleaned up // Wait for DB to be cleaned up
c.accWatcher.Stop() c.accWatcher.Stop()
@ -121,6 +107,21 @@ func TestController_watchAccountsChanges(t *testing.T) {
require.Nil(t, ranges.tokens.FirstKnown) require.Nil(t, ranges.tokens.FirstKnown)
require.Nil(t, ranges.tokens.LastKnown) require.Nil(t, ranges.tokens.LastKnown)
require.Nil(t, ranges.tokens.Start) require.Nil(t, ranges.tokens.Start)
}()
})
c.startAccountWatcher([]uint64{chainID})
// Watching accounts must start before sending event.
// To avoid running goroutine immediately and let the controller subscribe first,
// use any delay.
go func() {
time.Sleep(1 * time.Millisecond)
accountFeed.Send(accountsevent.Event{
Type: accountsevent.EventTypeRemoved,
Accounts: []common.Address{address},
})
}()
} }
func TestController_cleanupAccountLeftovers(t *testing.T) { func TestController_cleanupAccountLeftovers(t *testing.T) {