From 5d5465f0bb3a2280b184888598f3762144760a9f Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Thu, 9 Nov 2023 21:21:06 +0530 Subject: [PATCH] Added basic test for RTP2 --- ably/export_test.go | 6 ++ ably/proto_presence_message_test.go | 85 +++++++++++++++++++++-------- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/ably/export_test.go b/ably/export_test.go index 8125e745..7cae2726 100644 --- a/ably/export_test.go +++ b/ably/export_test.go @@ -211,6 +211,12 @@ func (c *Connection) SetKey(key string) { c.key = key } +func (c *RealtimePresence) GetMembers() map[string]*PresenceMessage { + c.mtx.Lock() + defer c.mtx.Unlock() + return c.members +} + func (c *Connection) ConnectionStateTTL() time.Duration { return c.connectionStateTTL() } diff --git a/ably/proto_presence_message_test.go b/ably/proto_presence_message_test.go index 411e5889..9c6ba19d 100644 --- a/ably/proto_presence_message_test.go +++ b/ably/proto_presence_message_test.go @@ -4,6 +4,7 @@ package ably_test import ( + "context" "encoding/json" "fmt" "testing" @@ -172,9 +173,12 @@ func Test_PresenceMap_RTP2(t *testing.T) { channel *ably.RealtimeChannel, stateChanges ably.ChannelStateChanges, afterCalls chan ablytest.AfterCall, + presenceMsgCh chan *ably.PresenceMessage, ) { in = make(chan *ably.ProtocolMessage, 1) out = make(chan *ably.ProtocolMessage, 16) + presenceMsgCh = make(chan *ably.PresenceMessage, 16) + afterCalls = make(chan ablytest.AfterCall, 1) now, after := ablytest.TimeFuncs(afterCalls) @@ -199,44 +203,77 @@ func Test_PresenceMap_RTP2(t *testing.T) { channel = c.Channels.Get("test") stateChanges = make(ably.ChannelStateChanges, 10) + channel.OnAll(stateChanges.Receive) + + in <- &ably.ProtocolMessage{ + Action: ably.ActionAttached, + Channel: channel.Name, + } + + var change ably.ChannelStateChange + + ablytest.Instantly.Recv(t, &change, stateChanges, t.Fatalf) + assert.Equal(t, ably.ChannelStateAttached, change.Current, + "expected %v; got %v (event: %+v)", ably.ChannelStateAttached, change.Current) + + channel.Presence.SubscribeAll(context.Background(), func(message *ably.PresenceMessage) { + presenceMsgCh <- message + }) return } - t.Run("RTL14: when Error, should transition to failed state", func(t *testing.T) { - in, out, _, channel, stateChanges, afterCalls := setup(t) + t.Run("RTP2: should maintain a list of members present on the channel", func(t *testing.T) { + in, _, _, channel, _, _, presenceMsgCh := setup(t) + + initialMembers := channel.Presence.GetMembers() + assert.Empty(t, initialMembers) - errInfo := ably.ProtoErrorInfo{ - StatusCode: 500, - Code: 50500, - Message: "fake error", + presenceMsg1 := &ably.PresenceMessage{ + Action: ably.PresenceActionPresent, + Message: ably.Message{ + ID: "123:12:1", + Timestamp: 125, + ConnectionID: "987", + ClientID: "999", + }, } - in <- &ably.ProtocolMessage{ - Action: ably.ActionError, - Channel: channel.Name, - Error: &errInfo, + presenceMsg2 := &ably.PresenceMessage{ + Action: ably.PresenceActionPresent, + Message: ably.Message{ + ID: "123:12:2", + Timestamp: 123, + ConnectionID: "784", + ClientID: "999", + }, } - // Expect a state change with the error. + msg := &ably.ProtocolMessage{ + Action: ably.ActionPresence, + Channel: channel.Name, + Presence: []*ably.PresenceMessage{presenceMsg1}, + } - var change ably.ChannelStateChange - ablytest.Instantly.Recv(t, &change, stateChanges, t.Fatalf) - assert.Equal(t, ably.ChannelStateFailed, change.Current, - "expected %v; got %v (event: %+v)", change) + in <- msg + ablytest.Instantly.Recv(t, nil, presenceMsgCh, t.Fatalf) + presenceMembers := channel.Presence.GetMembers() - got := fmt.Sprint(change.Reason) - assert.Contains(t, got, "fake error", - "expected error info to contain \"fake error\"; got %v", got) + assert.Equal(t, 1, len(presenceMembers)) + member := presenceMembers[presenceMsg1.ConnectionID+presenceMsg1.ClientID] + assert.Equal(t, ably.PresenceActionPresent, member.Action) - got = fmt.Sprint(channel.ErrorReason()) - assert.Contains(t, got, "fake error", - "expected error info to contain \"fake error\"; got %v", got) + msg.Presence = []*ably.PresenceMessage{presenceMsg2} + in <- msg - ablytest.Instantly.NoRecv(t, nil, afterCalls, t.Fatalf) - ablytest.Instantly.NoRecv(t, nil, stateChanges, t.Fatalf) - ablytest.Instantly.NoRecv(t, nil, out, t.Fatalf) + ablytest.Instantly.Recv(t, nil, presenceMsgCh, t.Fatalf) + assert.Equal(t, 2, len(channel.Presence.GetMembers())) + member2 := presenceMembers[presenceMsg2.ConnectionID+presenceMsg2.ClientID] + assert.Equal(t, ably.PresenceActionPresent, member2.Action) }) + t.Run("RTP2", func(t *testing.T) { + + }) } func Test_Presence_SYNC_RTP18(t *testing.T) {