diff --git a/src/Api/PubnubApi/EndPoint/PubSub/SubscribeEndpoint.cs b/src/Api/PubnubApi/EndPoint/PubSub/SubscribeEndpoint.cs index 5e13ca538..bbd37f297 100644 --- a/src/Api/PubnubApi/EndPoint/PubSub/SubscribeEndpoint.cs +++ b/src/Api/PubnubApi/EndPoint/PubSub/SubscribeEndpoint.cs @@ -87,6 +87,7 @@ public ISubscribeOperation QueryParam(Dictionary customQueryP public void Execute() { + if(string.IsNullOrEmpty(config.SubscribeKey)) throw new MissingMemberException("Invalid Subscribe key"); subscribeChannelNames ??= new List(); subscribeChannelGroupNames ??= new List(); diff --git a/src/Api/PubnubApi/EndPoint/PubSub/UnsubscribeEndpoint.cs b/src/Api/PubnubApi/EndPoint/PubSub/UnsubscribeEndpoint.cs index 22904353f..1d7a3353c 100644 --- a/src/Api/PubnubApi/EndPoint/PubSub/UnsubscribeEndpoint.cs +++ b/src/Api/PubnubApi/EndPoint/PubSub/UnsubscribeEndpoint.cs @@ -110,7 +110,7 @@ private void Unsubscribe(string[] channels, string[] channelGroups) var isSubscriptionChanged = isUniqueChannelCountChanged || isUniqueChannelGroupCountChanged; if (isSubscriptionChanged) { - subscribeEventEngine.Unsubscribe(subscribeEventEngine.Channels.ToArray(), subscribeEventEngine.Channels.ToArray()); + subscribeEventEngine.Unsubscribe(subscribeEventEngine.Channels.ToArray(), subscribeEventEngine.ChannelGroups.ToArray()); if (config.PresenceInterval > 0 && presenceEventEngineFactory.HasEventEngine(instanceId) && (uniqueChannelsToRemove.Count > 0 || uniqueChannelGroupsToRemove.Count > 0)) { PresenceEventEngine presenceEventEngine = presenceEventEngineFactory.GetEventEngine(instanceId); presenceEventEngine.EventQueue.Enqueue(new EventEngine.Presence.Events.LeftEvent() diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs index 2d6d2b60f..384b5a885 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/States/ReceivingState.cs @@ -17,7 +17,7 @@ public override TransitionResult Transition(IEvent e) { return e switch { - Events.UnsubscribeAllEvent unsubscribeAll => new UnsubscribedState() { }, + Events.UnsubscribeAllEvent unsubscribeAll => new UnsubscribedState() { }.With(new EmitStatusInvocation(PNStatusCategory.PNDisconnectedCategory)), Events.ReceiveSuccessEvent receiveSuccess => new ReceivingState() { @@ -58,7 +58,7 @@ public override TransitionResult Transition(IEvent e) Cursor = this.Cursor, AttemptedRetries = 0, Reason = receiveFailure.Status - }, + }.With(new EmitStatusInvocation(PNStatusCategory.PNUnexpectedDisconnectCategory)), _ => null }; diff --git a/src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs b/src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs index 71d132548..1a137342e 100644 --- a/src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs +++ b/src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs @@ -89,11 +89,18 @@ public void UnsubscribeAll() public void Unsubscribe(string[] channels, string[] channelGroups) { - if(channels.Length>0 || channelGroups.Length>0) - EventQueue.Enqueue(new SubscriptionChangedEvent() { + if (channels.Length > 0 || channelGroups.Length > 0) + { + EventQueue.Enqueue(new SubscriptionChangedEvent() + { Channels = Channels.Distinct(), ChannelGroups = ChannelGroups.Distinct() }); + } + else + { + EventQueue.Enqueue(new UnsubscribeAllEvent()); + } } } } \ No newline at end of file diff --git a/src/UnitTests/PubnubApi.Tests/TestHarness.cs b/src/UnitTests/PubnubApi.Tests/TestHarness.cs index ddfba53cf..5550c391c 100644 --- a/src/UnitTests/PubnubApi.Tests/TestHarness.cs +++ b/src/UnitTests/PubnubApi.Tests/TestHarness.cs @@ -29,7 +29,6 @@ protected static Pubnub createPubNubInstance(PNConfiguration pnConfiguration, st else { pnConfiguration.Origin = "ps.pndsn.com"; - pnConfiguration.EnableEventEngine = true; pubnub = new Pubnub(pnConfiguration); } if (!string.IsNullOrEmpty(authToken)) diff --git a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs index 4687bb96d..8d20e89b7 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs @@ -514,7 +514,6 @@ public static void ThenMultiSubscribeShouldReturnConnectStatus() PublishKey = PubnubCommon.PublishKey, SubscribeKey = PubnubCommon.SubscribeKey, Secure = false, - EnableEventEngine = false }; if (PubnubCommon.PAMServerSideRun) { @@ -573,9 +572,10 @@ public static void ThenMultiSubscribeShouldReturnConnectStatus() string channel2 = "hello_my_channel2"; if (receivedMessage) { - receivedMessage = false; + // Reason: As per eventEngine specification, Connected Status won't be emitted on Subscription entity changes + // receivedMessage = false; - subscribeManualEvent = new ManualResetEvent(false); + // subscribeManualEvent = new ManualResetEvent(false); expected = "{\"t\":{\"t\":\"14836303477713304\",\"r\":7},\"m\":[]}"; @@ -600,7 +600,7 @@ public static void ThenMultiSubscribeShouldReturnConnectStatus() Thread.Sleep(1000); pubnub.Subscribe().Channels(new [] { channel2 }).Execute(); - subscribeManualEvent.WaitOne(manualResetEventWaitTimeout); //Wait for Connect Status + // subscribeManualEvent.WaitOne(manualResetEventWaitTimeout); //Wait for Connect Status } Thread.Sleep(1000); diff --git a/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs b/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs index 9e04290f3..fb360ef32 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs @@ -247,35 +247,36 @@ public override void MessageAction(Pubnub pubnub, PNMessageActionEventResult mes public override void Status(Pubnub pubnub, PNStatus status) { Debug.WriteLine("SubscribeCallback: PNStatus: " + status.StatusCode.ToString()); - if (status.StatusCode != 200 || status.Error) + // Reason: No status code info in status of Dicsonnected as per Event Engine specification + if (status.Category == PNStatusCategory.PNDisconnectedCategory) { switch (currentTestCase) { case "ThenShouldReturnUnsubscribedMessage": + receivedMessage = true; subscribeManualEvent.Set(); break; default: break; } - - if (status.ErrorData != null) - { - Debug.WriteLine(status.ErrorData.Information); - } } - else if (status.StatusCode == 200 && status.Category == PNStatusCategory.PNConnectedCategory) + else if (status.StatusCode != 200 || status.Error) { switch (currentTestCase) { case "ThenShouldReturnUnsubscribedMessage": - receivedMessage = true; subscribeManualEvent.Set(); break; default: break; } + + if (status.ErrorData != null) + { + Debug.WriteLine(status.ErrorData.Information); + } } - else if (status.StatusCode == 200 && status.Category == PNStatusCategory.PNDisconnectedCategory) + else if (status.StatusCode == 200 && status.Category == PNStatusCategory.PNConnectedCategory) { switch (currentTestCase) { @@ -288,7 +289,6 @@ public override void Status(Pubnub pubnub, PNStatus status) } } - } public override void File(Pubnub pubnub, PNFileEventResult fileEvent) diff --git a/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs b/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs index ed13eeae6..8719aad5b 100644 --- a/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs +++ b/src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs @@ -150,7 +150,12 @@ public static void ThenShouldReturnUnsubscribedMessage() (o, p) => { /* Catch the presence events */ }, (o, s) => { Debug.WriteLine("SubscribeCallback: PNStatus: " + s.StatusCode.ToString()); - if (s.StatusCode != 200 || s.Error) + // Reason: No Status code information emitted on Disconnected Event + if (s.Category == PNStatusCategory.PNConnectedCategory || s.Category == PNStatusCategory.PNDisconnectedCategory) + { + receivedMessage = true; + subscribeManualEvent.Set(); + } else if (s.StatusCode != 200 || s.Error) { subscribeManualEvent.Set(); if (s.ErrorData != null) @@ -158,11 +163,6 @@ public static void ThenShouldReturnUnsubscribedMessage() Debug.WriteLine(s.ErrorData.Information); } } - else if (s.StatusCode == 200 && (s.Category == PNStatusCategory.PNConnectedCategory || s.Category == PNStatusCategory.PNDisconnectedCategory)) - { - receivedMessage = true; - subscribeManualEvent.Set(); - } }); pubnub = createPubNubInstance(config); pubnub.AddListener(listenerSubCallack);