Skip to content

Commit

Permalink
fix: tests and emitStatus related issue, Dicsonnected category status…
Browse files Browse the repository at this point in the history
… does not need statusCode as per event engine specification
  • Loading branch information
mohitpubnub committed Nov 28, 2024
1 parent b8fc661 commit 534178a
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/Api/PubnubApi/EndPoint/PubSub/SubscribeEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public ISubscribeOperation<T> QueryParam(Dictionary<string, object> customQueryP

public void Execute()
{
if(string.IsNullOrEmpty(config.SubscribeKey)) throw new MissingMemberException("Invalid Subscribe key");
subscribeChannelNames ??= new List<string>();
subscribeChannelGroupNames ??= new List<string>();

Expand Down
2 changes: 1 addition & 1 deletion src/Api/PubnubApi/EndPoint/PubSub/UnsubscribeEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -58,7 +58,7 @@ public override TransitionResult Transition(IEvent e)
Cursor = this.Cursor,
AttemptedRetries = 0,
Reason = receiveFailure.Status
},
}.With(new EmitStatusInvocation(PNStatusCategory.PNUnexpectedDisconnectCategory)),

_ => null
};
Expand Down
11 changes: 9 additions & 2 deletions src/Api/PubnubApi/EventEngine/Subscribe/SubscribeEventEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
}
1 change: 0 additions & 1 deletion src/UnitTests/PubnubApi.Tests/TestHarness.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions src/UnitTests/PubnubApi.Tests/WhenSubscribedToAChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,6 @@ public static void ThenMultiSubscribeShouldReturnConnectStatus()
PublishKey = PubnubCommon.PublishKey,
SubscribeKey = PubnubCommon.SubscribeKey,
Secure = false,
EnableEventEngine = false
};
if (PubnubCommon.PAMServerSideRun)
{
Expand Down Expand Up @@ -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\":[]}";

Expand All @@ -600,7 +600,7 @@ public static void ThenMultiSubscribeShouldReturnConnectStatus()

Thread.Sleep(1000);
pubnub.Subscribe<string>().Channels(new [] { channel2 }).Execute();
subscribeManualEvent.WaitOne(manualResetEventWaitTimeout); //Wait for Connect Status
// subscribeManualEvent.WaitOne(manualResetEventWaitTimeout); //Wait for Connect Status
}

Thread.Sleep(1000);
Expand Down
20 changes: 10 additions & 10 deletions src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -288,7 +289,6 @@ public override void Status(Pubnub pubnub, PNStatus status)
}
}


}

public override void File(Pubnub pubnub, PNFileEventResult fileEvent)
Expand Down
12 changes: 6 additions & 6 deletions src/UnitTests/PubnubApi.Tests/WhenUnsubscribedToAChannelGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,19 @@ 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)
{
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);
Expand Down

0 comments on commit 534178a

Please sign in to comment.