Set chat active on being re-invited

If the user deletes/leaves a group chat, the chat is set as not active.
This means that if we are re-invited to the chat it won't be shown to
the user.

This commit changes this behavior so that if we are re-invited to the
chat it is set as active again.
This commit is contained in:
Andrea Maria Piana 2020-04-22 14:58:28 +02:00
parent 7d3655976c
commit 7bbe9561de
7 changed files with 206 additions and 278 deletions

View File

@ -1 +1 @@
0.52.3
0.52.4

View File

@ -150,7 +150,21 @@ func (c *Chat) MembersAsPublicKeys() ([]*ecdsa.PublicKey, error) {
return stringSliceToPublicKeys(publicKeys, true)
}
func (c *Chat) updateChatFromProtocolGroup(g *v1protocol.Group) {
func (c *Chat) HasMember(memberID string) bool {
for _, member := range c.Members {
if memberID == member.ID {
return true
}
}
return false
}
func (c *Chat) updateChatFromGroupMembershipChanges(myID string, g *v1protocol.Group) {
// Check if we were already in the chat
hadJoined := c.HasMember(myID)
// ID
c.ID = g.ChatID()
@ -163,6 +177,11 @@ func (c *Chat) updateChatFromProtocolGroup(g *v1protocol.Group) {
joined := g.Joined()
chatMembers := make([]ChatMember, 0, len(members))
for _, m := range members {
// Check if we joined thanks to these changes, if so, make chat active
if m == myID && !hadJoined {
c.Active = true
}
chatMember := ChatMember{
ID: m,
}

View File

@ -85,7 +85,8 @@ func (m *MessageHandler) HandleMembershipUpdate(messageState *ReceivedMessageSta
}
}
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
systemMessages := buildSystemMessages(message.Events, translations)
for _, message := range systemMessages {

View File

@ -644,7 +644,8 @@ func (m *Messenger) CreateGroupChatWithMembers(ctx context.Context, name string,
return nil, err
}
chat.LastClockValue = clock
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
clock, _ = chat.NextClockAndTimestamp(m.getTimesource())
// Add members
@ -682,7 +683,7 @@ func (m *Messenger) CreateGroupChatWithMembers(ctx context.Context, name string,
return nil, err
}
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
response.Chats = []*Chat{&chat}
response.Messages = buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations)
@ -747,7 +748,8 @@ func (m *Messenger) RemoveMemberFromGroupChat(ctx context.Context, chatID string
return nil, err
}
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
response.Chats = []*Chat{chat}
response.Messages = buildSystemMessages(chat.MembershipUpdates, m.systemMessagesTranslations)
err = m.persistence.SaveMessagesLegacy(response.Messages)
@ -810,7 +812,7 @@ func (m *Messenger) AddMembersToGroupChat(ctx context.Context, chatID string, me
return nil, err
}
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
response.Chats = []*Chat{chat}
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
@ -875,7 +877,7 @@ func (m *Messenger) ChangeGroupChatName(ctx context.Context, chatID string, name
return nil, err
}
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
var response MessengerResponse
response.Chats = []*Chat{chat}
@ -941,7 +943,7 @@ func (m *Messenger) AddAdminsToGroupChat(ctx context.Context, chatID string, mem
return nil, err
}
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
response.Chats = []*Chat{chat}
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
@ -1008,7 +1010,7 @@ func (m *Messenger) ConfirmJoiningGroup(ctx context.Context, chatID string) (*Me
return nil, err
}
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
response.Chats = []*Chat{chat}
response.Messages = buildSystemMessages([]v1protocol.MembershipUpdateEvent{event}, m.systemMessagesTranslations)
@ -1076,7 +1078,8 @@ func (m *Messenger) LeaveGroupChat(ctx context.Context, chatID string, remove bo
return nil, err
}
chat.updateChatFromProtocolGroup(group)
chat.updateChatFromGroupMembershipChanges(contactIDFromPublicKey(&m.identity.PublicKey), group)
if remove {
chat.Active = false
}

View File

@ -3,7 +3,6 @@ package protocol
import (
"context"
"crypto/ecdsa"
"errors"
"io/ioutil"
"os"
"testing"
@ -101,14 +100,11 @@ func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
s.Require().False(chat.Active, "It does not create an active chat")
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Contacts) == 0 {
err = errors.New("contact request not received")
}
return err
})
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
"contact request not received",
)
s.Require().NoError(err)
receivedContact := response.Contacts[0]
@ -118,20 +114,19 @@ func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
s.Require().True(receivedContact.HasBeenAdded())
s.Require().NotEmpty(receivedContact.LastUpdated)
newName := "new-name"
newPicture := "new-picture"
err = theirMessenger.SendContactUpdates(context.Background(), newName, newPicture)
s.Require().NoError(err)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Contacts) == 0 || (len(response.Contacts) == 1 && response.Contacts[0].ID != theirContactID) {
err = errors.New("contact request not received")
}
return err
})
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool {
return len(r.Contacts) > 0 && response.Contacts[0].ID == theirContactID
},
"contact request not received",
)
s.Require().NoError(err)
receivedContact = response.Contacts[0]

View File

@ -99,14 +99,12 @@ func (s *MessengerInstallationSuite) TestReceiveInstallation() {
s.Require().False(response.Chats[0].Active)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Installations) == 0 {
err = errors.New("installation not received")
}
return err
})
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Installations) > 0 },
"installation not received",
)
s.Require().NoError(err)
actualInstallation := response.Installations[0]
s.Require().Equal(theirMessenger.installationID, actualInstallation.ID)
@ -127,17 +125,11 @@ func (s *MessengerInstallationSuite) TestReceiveInstallation() {
s.Require().NoError(err)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Contacts) == 0 {
err = errors.New("contact not received")
}
if len(response.Contacts) != 0 && response.Contacts[0].ID != contact.ID {
err = errors.New("contact not received")
}
return err
})
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 && r.Contacts[0].ID == contact.ID },
"contact not received",
)
s.Require().NoError(err)
actualContact := response.Contacts[0]
@ -148,15 +140,12 @@ func (s *MessengerInstallationSuite) TestReceiveInstallation() {
err = s.m.SaveChat(&chat)
s.Require().NoError(err)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Chats) == 0 {
err = errors.New("sync chat not received")
}
return err
})
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"sync chat not received",
)
s.Require().NoError(err)
actualChat := response.Chats[0]
@ -196,14 +185,12 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() {
s.Require().False(response.Chats[0].Active)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Installations) == 0 {
err = errors.New("installation not received")
}
return err
})
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Installations) > 0 },
"installation not received",
)
s.Require().NoError(err)
actualInstallation := response.Installations[0]
s.Require().Equal(theirMessenger.installationID, actualInstallation.ID)

View File

@ -38,6 +38,7 @@ const (
testTransactionHash = "0x412a851ac2ae51cad34a56c8a9cfee55d577ac5e1ac71cf488a2f2093a373799"
testIdenticon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAnElEQVR4nOzXQaqDMBRG4bZkLR10e12H23PgZuJUjJAcE8kdnG/44IXDhZ9iyjm/4vnMDrhmFmEWYRZhFpH6n1jW7fSX/+/b+WbQa5lFmEVUljhqZfSdoNcyizCLeNMvn3JTLeh+g17LLMIsorLElt2VK7v3X0dBr2UWYRaBfxNLfifOZhYRNGvAEp8Q9FpmEWYRZhFmEXsAAAD//5K5JFhu0M0nAAAAAElFTkSuQmCC"
testAlias = "Concrete Lavender Xiphias"
newName = "new-name"
)
func TestMessengerSuite(t *testing.T) {
@ -521,15 +522,12 @@ func (s *MessengerSuite) TestRetrieveTheirPublic() {
sentMessage := sendResponse.Messages[0]
// Wait for the message to reach its destination
var response *MessengerResponse
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err := WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Messages, 1)
@ -634,15 +632,11 @@ func (s *MessengerSuite) TestResendPublicMessage() {
s.Require().NoError(err)
// Wait for the message to reach its destination
var response *MessengerResponse
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err := WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Messages, 1)
@ -688,15 +682,11 @@ func (s *MessengerSuite) TestRetrieveTheirPrivateChatExisting() {
sentMessage := sendResponse.Messages[0]
var response *MessengerResponse
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err := WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Equal(len(response.Chats), 1)
@ -726,15 +716,12 @@ func (s *MessengerSuite) TestRetrieveTheirPrivateChatNonExisting() {
sentMessage := sendResponse.Messages[0]
// Wait for the message to reach its destination
var response *MessengerResponse
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err := WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Chats, 1)
@ -771,40 +758,6 @@ func (s *MessengerSuite) TestRetrieveTheirPublicChatNonExisting() {
s.Require().Equal(len(response.Chats), 0)
}
// Test receiving a message on an non-existing private public chat
func (s *MessengerSuite) TestRetrieveTheirGroupChatNonExisting() {
theirMessenger := s.newMessenger(s.shh)
response, err := s.m.CreateGroupChatWithMembers(context.Background(), "test", []string{})
s.NoError(err)
s.Require().Len(response.Chats, 1)
chat := response.Chats[0]
err = theirMessenger.SaveChat(chat)
s.NoError(err)
inputMessage := buildTestMessage(*chat)
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
s.NoError(err)
s.Require().Len(sendResponse.Messages, 1)
// Retrieve their messages so that the chat is created
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Chats) == 1 {
err = errors.New("chat membership update not received")
}
return err
})
s.Require().NoError(err)
// The message is discarded
s.Require().Equal(0, len(response.Messages))
s.Require().Equal(0, len(response.Chats))
}
// Test receiving a message on an existing private group chat
func (s *MessengerSuite) TestRetrieveTheirPrivateGroupChat() {
var response *MessengerResponse
@ -823,27 +776,22 @@ func (s *MessengerSuite) TestRetrieveTheirPrivateGroupChat() {
s.NoError(err)
// Retrieve their messages so that the chat is created
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Chats) == 0 {
err = errors.New("chat invitation not received")
}
return err
})
_, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"chat invitation not received",
)
s.Require().NoError(err)
_, err = theirMessenger.ConfirmJoiningGroup(context.Background(), ourChat.ID)
s.NoError(err)
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Chats) == 0 {
err = errors.New("no joining group event received")
}
return err
})
// Wait for the message to reach its destination
_, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"no joining group event received",
)
s.Require().NoError(err)
inputMessage := buildTestMessage(*ourChat)
@ -854,14 +802,11 @@ func (s *MessengerSuite) TestRetrieveTheirPrivateGroupChat() {
sentMessage := sendResponse.Messages[0]
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Chats, 1)
@ -892,43 +837,33 @@ func (s *MessengerSuite) TestChangeNameGroupChat() {
s.NoError(err)
// Retrieve their messages so that the chat is created
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Chats) == 0 {
err = errors.New("chat invitation not received")
}
return err
})
_, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"chat invitation not received",
)
s.Require().NoError(err)
_, err = theirMessenger.ConfirmJoiningGroup(context.Background(), ourChat.ID)
s.NoError(err)
// Wait for join group event
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Chats) == 0 {
err = errors.New("no joining group event received")
}
return err
})
_, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"no joining group event received",
)
s.Require().NoError(err)
newName := "new-name"
_, err = s.m.ChangeGroupChatName(context.Background(), ourChat.ID, newName)
s.NoError(err)
// Retrieve their messages so that the chat is created
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Chats) == 0 {
err = errors.New("chat invitation not received")
}
return err
})
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"chat invitation not received",
)
s.Require().NoError(err)
s.Require().Len(response.Chats, 1)
@ -936,14 +871,11 @@ func (s *MessengerSuite) TestChangeNameGroupChat() {
s.Require().Equal(newName, actualChat.Name)
}
// Test receiving a message on an existing private group chat, if messages
// are not wrapped this will fail as they'll likely come out of order
// NOTE: Disabling this as too flaky
/*
func (s *MessengerSuite) testRetrieveTheirPrivateGroupWrappedMessageChat() {
// Test being re-invited to a group chat
func (s *MessengerSuite) TestReInvitedToGroupChat() {
var response *MessengerResponse
theirMessenger := s.newMessenger(s.shh)
response, err := s.m.CreateGroupChatWithMembers(context.Background(), "id", []string{})
response, err := s.m.CreateGroupChatWithMembers(context.Background(), "old-name", []string{})
s.NoError(err)
s.Require().Len(response.Chats, 1)
@ -957,46 +889,46 @@ func (s *MessengerSuite) testRetrieveTheirPrivateGroupWrappedMessageChat() {
s.NoError(err)
// Retrieve their messages so that the chat is created
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Chats) == 0 {
err = errors.New("chat invitation not received")
}
return err
})
_, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"chat invitation not received",
)
s.Require().NoError(err)
_, err = theirMessenger.ConfirmJoiningGroup(context.Background(), ourChat.ID)
s.NoError(err)
inputMessage := buildTestMessage(*ourChat)
sendResponse, err := theirMessenger.SendChatMessage(context.Background(), inputMessage)
s.NoError(err)
s.Require().Len(sendResponse.Messages, 1)
sentMessage := sendResponse.Messages[0]
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Chats) == 0 {
err = errors.New("no chats")
}
return err
})
// Wait for join group event
_, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"no joining group event received",
)
s.Require().NoError(err)
response, err = theirMessenger.LeaveGroupChat(context.Background(), ourChat.ID, true)
s.NoError(err)
s.Require().Len(response.Chats, 1)
actualChat := response.Chats[0]
// It updates the unviewed messages count
s.Require().Equal(uint(1), actualChat.UnviewedMessagesCount)
// It updates the last message clock value
s.Require().Equal(sentMessage.Clock, actualChat.LastClockValue)
// It sets the last message
s.Require().NotNil(actualChat.LastMessage)
s.Require().False(response.Chats[0].Active)
// And we get re-invited
_, err = s.m.AddMembersToGroupChat(context.Background(), ourChat.ID, members)
s.NoError(err)
// Retrieve their messages so that the chat is created
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
"chat invitation not received",
)
s.Require().NoError(err)
s.Require().Len(response.Chats, 1)
s.Require().True(response.Chats[0].Active)
}
*/
func (s *MessengerSuite) TestChatPersistencePublic() {
chat := Chat{
@ -1526,14 +1458,11 @@ func (s *MessengerSuite) TestDeclineRequestAddressForTransaction() {
s.Require().Equal(CommandStateRequestAddressForTransaction, senderMessage.CommandParameters.CommandState)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().NotNil(response)
@ -1566,14 +1495,11 @@ func (s *MessengerSuite) TestDeclineRequestAddressForTransaction() {
s.Require().Equal(receiverMessage.ID, senderMessage.Replace)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Chats, 1)
@ -1822,14 +1748,11 @@ func (s *MessengerSuite) TestAcceptRequestAddressForTransaction() {
s.Require().Equal(CommandStateRequestAddressForTransaction, senderMessage.CommandParameters.CommandState)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().NotNil(response)
@ -1863,14 +1786,11 @@ func (s *MessengerSuite) TestAcceptRequestAddressForTransaction() {
s.Require().Equal(receiverMessage.ID, senderMessage.Replace)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Chats, 1)
@ -1919,14 +1839,11 @@ func (s *MessengerSuite) TestDeclineRequestTransaction() {
s.Require().Equal(CommandStateRequestTransaction, senderMessage.CommandParameters.CommandState)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().NotNil(response)
@ -1958,14 +1875,11 @@ func (s *MessengerSuite) TestDeclineRequestTransaction() {
s.Require().Equal(CommandStateRequestTransactionDeclined, senderMessage.CommandParameters.CommandState)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = s.m.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().NotNil(response)
@ -2012,14 +1926,11 @@ func (s *MessengerSuite) TestRequestTransaction() {
s.Require().Equal(CommandStateRequestTransaction, senderMessage.CommandParameters.CommandState)
// Wait for the message to reach its destination
err = tt.RetryWithBackOff(func() error {
var err error
response, err = theirMessenger.RetrieveAll()
if err == nil && len(response.Messages) == 0 {
err = errors.New("no messages")
}
return err
})
response, err = WaitOnMessengerResponse(
theirMessenger,
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
"no messages",
)
s.Require().NoError(err)
s.Require().NotNil(response)
@ -2349,3 +2260,15 @@ func (s *MessageHandlerSuite) TestRun() {
})
}
}
func WaitOnMessengerResponse(m *Messenger, condition func(*MessengerResponse) bool, errorMessage string) (*MessengerResponse, error) {
var response *MessengerResponse
return response, tt.RetryWithBackOff(func() error {
var err error
response, err = m.RetrieveAll()
if err == nil && !condition(response) {
err = errors.New(errorMessage)
}
return err
})
}