Skip to content

Commit

Permalink
Fixed shallowcopy issue, added test for the same
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Nov 12, 2023
1 parent 7a9d7d4 commit b99a651
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
64 changes: 62 additions & 2 deletions ably/proto_presence_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func Test_PresenceMap_RTP2(t *testing.T) {
assert.Equal(t, "989:12:0", member.ID)
})

t.Run("RTP2b2: check for newness by serial if not synthesized", func(t *testing.T) {
t.Run("RTP2b2, RTP2d: check for newness by serial if not synthesized", func(t *testing.T) {
in, _, _, channel, _, _, presenceMsgCh := setup(t)

initialMembers := channel.Presence.GetMembers()
Expand Down Expand Up @@ -399,8 +399,68 @@ func Test_PresenceMap_RTP2(t *testing.T) {

})

t.Run("RTP2d: when presence msg with ENTER, UPDATE AND PRESENT arrives, add to presence map with action as present", func(t *testing.T) {
t.Run("RTP2d, RTP2g: when presence msg with ENTER, UPDATE AND PRESENT arrives, add to presence map with action as present", func(t *testing.T) {
in, _, _, channel, _, _, presenceMsgCh := setup(t)

initialMembers := channel.Presence.GetMembers()
assert.Empty(t, initialMembers)

presenceMsg1 := &ably.PresenceMessage{
Action: ably.PresenceActionEnter,
Message: ably.Message{
ID: "987:12:0",
Timestamp: 125,
ConnectionID: "987",
ClientID: "999",
},
}

presenceMsg2 := &ably.PresenceMessage{
Action: ably.PresenceActionUpdate,
Message: ably.Message{
ID: "987:12:1",
Timestamp: 128,
ConnectionID: "988",
ClientID: "999",
},
}

presenceMsg3 := &ably.PresenceMessage{
Action: ably.PresenceActionPresent,
Message: ably.Message{
ID: "987:12:2",
Timestamp: 128,
ConnectionID: "989",
ClientID: "999",
},
}

msg := &ably.ProtocolMessage{
Action: ably.ActionPresence,
Channel: channel.Name,
Presence: []*ably.PresenceMessage{presenceMsg1},
}

var presenceMsg *ably.PresenceMessage
in <- msg
ablytest.Instantly.Recv(t, &presenceMsg, presenceMsgCh, t.Fatalf) // RTP2g
assert.Equal(t, ably.PresenceActionEnter, presenceMsg.Action)

msg.Presence = []*ably.PresenceMessage{presenceMsg2}
in <- msg
ablytest.Instantly.Recv(t, &presenceMsg, presenceMsgCh, t.Fatalf)
assert.Equal(t, ably.PresenceActionUpdate, presenceMsg.Action)

msg.Presence = []*ably.PresenceMessage{presenceMsg3}
in <- msg
ablytest.Instantly.Recv(t, &presenceMsg, presenceMsgCh, t.Fatalf)
assert.Equal(t, ably.PresenceActionPresent, presenceMsg.Action)

members := channel.Presence.GetMembers()
assert.Equal(t, 3, len(members))
for _, pm := range members {
assert.Equal(t, ably.PresenceActionPresent, pm.Action)
}
})

t.Run("RTP2e: when presence msg with LEAVE action arrives, remove member from presence map", func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions ably/realtime_presence.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ func (pres *RealtimePresence) processProtoPresenceMessage(msg *protocolMessage)
}
switch presenceMember.Action {
case PresenceActionEnter, PresenceActionUpdate, PresenceActionPresent: // RTP2d, RTP17b
presenceMemberShallowCopy := presenceMember
presenceMemberShallowCopy := *presenceMember
presenceMemberShallowCopy.Action = PresenceActionPresent
pres.addPresenceMember(pres.internalMembers, memberKey, presenceMemberShallowCopy)
pres.addPresenceMember(pres.internalMembers, memberKey, &presenceMemberShallowCopy)
case PresenceActionLeave: // RTP17b, RTP2e
if !presenceMember.isServerSynthesized() {
pres.removePresenceMember(pres.internalMembers, memberKey, presenceMember)
Expand All @@ -310,9 +310,9 @@ func (pres *RealtimePresence) processProtoPresenceMessage(msg *protocolMessage)
switch presenceMember.Action {
case PresenceActionEnter, PresenceActionUpdate, PresenceActionPresent: // RTP2d
delete(pres.beforeSyncMembers, memberKey)
presenceMemberShallowCopy := presenceMember
presenceMemberShallowCopy := *presenceMember
presenceMemberShallowCopy.Action = PresenceActionPresent
memberUpdated = pres.addPresenceMember(pres.members, memberKey, presenceMemberShallowCopy)
memberUpdated = pres.addPresenceMember(pres.members, memberKey, &presenceMemberShallowCopy)
case PresenceActionLeave: // RTP2e
memberUpdated = pres.removePresenceMember(pres.members, memberKey, presenceMember)
}
Expand Down

0 comments on commit b99a651

Please sign in to comment.