fix flaky test TestBackupSettings (#4772)

This commit is contained in:
frank 2024-02-21 10:51:12 +08:00 committed by GitHub
parent e2cab1a8ae
commit 569a401643
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import (
"testing"
"github.com/status-im/status-go/protocol/identity"
"github.com/status-im/status-go/protocol/wakusync"
"github.com/stretchr/testify/suite"
@ -388,13 +389,13 @@ func (s *MessengerBackupSuite) TestBackupSettings() {
s.Require().NoError(err)
// Wait for the message to reach its destination
_, err = WaitOnMessengerResponse(
bob2,
func(r *MessengerResponse) bool {
return r.BackupHandled
},
"no messages",
)
_, err = WaitOnSignaledSendWakuFetchingBackupProgress(bob2, func(r *wakusync.WakuBackedUpDataResponse) bool {
detailsMap := r.FetchingBackedUpDataDetails()
if settingDetails, ok := detailsMap[SyncWakuSectionKeySettings]; ok {
return settingDetails.DataNumber == settingDetails.TotalNumber
}
return false
}, "no messages")
s.Require().NoError(err)
// Check bob2

View File

@ -9,6 +9,8 @@ import (
"sync"
"time"
"github.com/status-im/status-go/protocol/wakusync"
"github.com/status-im/status-go/protocol/identity"
"github.com/status-im/status-go/eth-node/types"
@ -52,8 +54,18 @@ func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bo
type MessengerSignalsHandlerMock struct {
MessengerSignalsHandler
responseChan chan *MessengerResponse
communityFoundChan chan *communities.Community
responseChan chan *MessengerResponse
communityFoundChan chan *communities.Community
wakuBackedUpDataResponseChan chan *wakusync.WakuBackedUpDataResponse
}
func (m *MessengerSignalsHandlerMock) SendWakuFetchingBackupProgress(response *wakusync.WakuBackedUpDataResponse) {
m.wakuBackedUpDataResponseChan <- response
}
func (m *MessengerSignalsHandlerMock) SendWakuBackedUpProfile(*wakusync.WakuBackedUpDataResponse) {}
func (m *MessengerSignalsHandlerMock) SendWakuBackedUpSettings(*wakusync.WakuBackedUpDataResponse) {}
func (m *MessengerSignalsHandlerMock) SendWakuBackedUpKeypair(*wakusync.WakuBackedUpDataResponse) {}
func (m *MessengerSignalsHandlerMock) SendWakuBackedUpWatchOnlyAccount(*wakusync.WakuBackedUpDataResponse) {
}
func (m *MessengerSignalsHandlerMock) MessengerResponse(response *MessengerResponse) {
@ -73,6 +85,42 @@ func (m *MessengerSignalsHandlerMock) CommunityInfoFound(community *communities.
}
}
func WaitOnSignaledSendWakuFetchingBackupProgress(m *Messenger, condition func(*wakusync.WakuBackedUpDataResponse) bool, errorMessage string) (*wakusync.WakuBackedUpDataResponse, error) {
interval := 500 * time.Millisecond
timeoutChan := time.After(10 * time.Second)
if m.config.messengerSignalsHandler != nil {
return nil, errors.New("messengerSignalsHandler already provided/mocked")
}
responseChan := make(chan *wakusync.WakuBackedUpDataResponse, 1000)
m.config.messengerSignalsHandler = &MessengerSignalsHandlerMock{
wakuBackedUpDataResponseChan: responseChan,
}
defer func() {
m.config.messengerSignalsHandler = nil
}()
for {
_, err := m.RetrieveAll()
if err != nil {
return nil, err
}
select {
case r := <-responseChan:
if condition(r) {
return r, nil
}
case <-timeoutChan:
return nil, errors.New("timed out: " + errorMessage)
default: // No immediate response, rest & loop back to retrieve again
time.Sleep(interval)
}
}
}
func WaitOnSignaledMessengerResponse(m *Messenger, condition func(*MessengerResponse) bool, errorMessage string) (*MessengerResponse, error) {
interval := 500 * time.Millisecond
timeoutChan := time.After(10 * time.Second)