Skip to content

Commit

Permalink
HandshakingState emit status
Browse files Browse the repository at this point in the history
  • Loading branch information
budgetpreneur committed Aug 1, 2023
1 parent 39c21e4 commit a3297c9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class EmitStatusInvocation : Core.IEffectInvocation {
public EmitStatusInvocation(PNStatus status)
{
this.Status = status;
if (status != null)
{
this.StatusCategory = status.Category;
}
}

public EmitStatusInvocation(PNStatusCategory category)
Expand Down
2 changes: 1 addition & 1 deletion src/Api/PubnubApi/Model/Consumer/PNStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public PNStatus(Exception e, PNOperationType operationType, PNStatusCategory cat
{
this.Error = true;
this.Operation = operationType;
this.ErrorData = new PNErrorData(e.Message, e);
this.ErrorData = new PNErrorData(e?.Message, e);
this.AffectedChannels = affectedChannels?.ToList();
this.AffectedChannelGroups = affectedChannelGroups?.ToList();
this.Category = category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PubnubApi.EventEngine.Subscribe.Common;
using PubnubApi.EventEngine.Subscribe.Context;
using PubnubApi.EventEngine.Subscribe.Events;
using PubnubApi.EventEngine.Subscribe.Invocations;
using PubnubApi.EventEngine.Subscribe.States;
using System.Linq;

Expand All @@ -14,8 +15,8 @@ internal class HandshakingStateTransition
public void TestHandshakingStateTransitionWithSubscriptionRestoredEvent()
{
//Arrange
State handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
State handshakingState2 = new HandshakingState();
var handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
var handshakingState2 = new HandshakingState();
//Act
TransitionResult result = handshakingState.Transition(new SubscriptionRestoredEvent()
{
Expand All @@ -39,8 +40,8 @@ public void TestHandshakingStateTransitionWithSubscriptionRestoredEvent()
public void TestHandshakingStateTransitionWithWithSubscriptionChangedEvent()
{
//Arrange
State handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
State handshakingState2 = new HandshakingState();
var handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
var handshakingState2 = new HandshakingState();
//Act
TransitionResult result = handshakingState.Transition(new SubscriptionChangedEvent()
{
Expand All @@ -63,8 +64,9 @@ public void TestHandshakingStateTransitionWithWithSubscriptionChangedEvent()
public void TestHandshakingStateTransitionWithHandshakeFailureEvent()
{
//Arrange
State handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
State handshakeReconnectingState = new HandshakeReconnectingState();
var handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
var handshakeReconnectingState = new HandshakeReconnectingState();
EmitStatusInvocation emitStatusInvocation = new EmitStatusInvocation(new PNStatus());
//Act
TransitionResult result = handshakingState.Transition(new HandshakeFailureEvent() { });
//Assert
Expand All @@ -75,14 +77,17 @@ public void TestHandshakingStateTransitionWithHandshakeFailureEvent()
Assert.AreEqual("cg2", ((HandshakeReconnectingState)(result.State)).ChannelGroups.ElementAt(1));
Assert.AreEqual(PNReconnectionPolicy.LINEAR, ((HandshakeReconnectingState)(result.State)).ReconnectionConfiguration.ReconnectionPolicy);
Assert.AreEqual(50, ((HandshakeReconnectingState)(result.State)).ReconnectionConfiguration.MaximumReconnectionRetries);
Assert.IsTrue(result.Invocations.ElementAt(0).GetType().Equals(emitStatusInvocation.GetType()));
Assert.AreEqual(PNStatusCategory.PNUnknownCategory, ((EmitStatusInvocation)result.Invocations.ElementAt(0)).StatusCategory);
}

[Test]
public void TestHandshakingStateTransitionWithDisconnectEvent()
{
//Arrange
State handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
State handshakeStoppedState = new HandshakeStoppedState();
var handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
var handshakeStoppedState = new HandshakeStoppedState();
EmitStatusInvocation emitStatusInvocation = new EmitStatusInvocation(new PNStatus());
//Act
TransitionResult result = handshakingState.Transition(new DisconnectEvent()
{
Expand All @@ -98,18 +103,22 @@ public void TestHandshakingStateTransitionWithDisconnectEvent()
Assert.AreEqual("cg2", ((HandshakeStoppedState)(result.State)).ChannelGroups.ElementAt(1));
Assert.AreEqual(PNReconnectionPolicy.LINEAR, ((HandshakeStoppedState)(result.State)).ReconnectionConfiguration.ReconnectionPolicy);
Assert.AreEqual(50, ((HandshakeStoppedState)(result.State)).ReconnectionConfiguration.MaximumReconnectionRetries);
Assert.IsTrue(result.Invocations.ElementAt(0).GetType().Equals(emitStatusInvocation.GetType()));
Assert.AreEqual(PNStatusCategory.PNDisconnectedCategory, ((EmitStatusInvocation)result.Invocations.ElementAt(0)).StatusCategory);
}

[Test]
public void TestHandshakingStateTransitionWithHandshakeSuccessEvent()
{
//Arrange
State handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
State receivingState = new ReceivingState();
var handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
var receivingState = new ReceivingState();
EmitStatusInvocation emitStatusInvocation = new EmitStatusInvocation(new PNStatus());
//Act
TransitionResult result = handshakingState.Transition(new HandshakeSuccessEvent()
{
Cursor = new SubscriptionCursor() { Region = 1, Timetoken = 1234567890 }
Cursor = new SubscriptionCursor() { Region = 1, Timetoken = 1234567890 },
Status = new PNStatus(null,PNOperationType.PNSubscribeOperation, PNStatusCategory.PNConnectedCategory, handshakingState.Channels, handshakingState.ChannelGroups)
});
//Assert
Assert.IsTrue(result.State.GetType().Equals(receivingState.GetType()));
Expand All @@ -119,14 +128,16 @@ public void TestHandshakingStateTransitionWithHandshakeSuccessEvent()
Assert.AreEqual("cg2", ((ReceivingState)(result.State)).ChannelGroups.ElementAt(1));
Assert.AreEqual(PNReconnectionPolicy.LINEAR, ((ReceivingState)(result.State)).ReconnectionConfiguration.ReconnectionPolicy);
Assert.AreEqual(50, ((ReceivingState)(result.State)).ReconnectionConfiguration.MaximumReconnectionRetries);
Assert.IsTrue(result.Invocations.ElementAt(0).GetType().Equals(emitStatusInvocation.GetType()));
Assert.AreEqual(PNStatusCategory.PNConnectedCategory, ((EmitStatusInvocation)result.Invocations.ElementAt(0)).StatusCategory);
}

[Test]
public void TestHandshakingStateTransitionWithUnsubscribeEvent()
{
//Arrange
State handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
State unsubscribedState = new UnsubscribedState();
var handshakingState = new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" }, ReconnectionConfiguration = new ReconnectionConfiguration(PNReconnectionPolicy.LINEAR, 50) };
var unsubscribedState = new UnsubscribedState();
//Act
TransitionResult result = handshakingState.Transition(new UnsubscribeAllEvent() { });
//Assert
Expand Down

0 comments on commit a3297c9

Please sign in to comment.