Skip to content

Commit

Permalink
Refactored HandshakeFailedStateTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
budgetpreneur committed Aug 2, 2023
1 parent af2633b commit bbcca51
Showing 1 changed file with 56 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,91 +1,81 @@
using NUnit.Framework;
using PubnubApi.EventEngine.Core;
using PubnubApi.EventEngine.Subscribe.Common;
using PubnubApi.EventEngine.Subscribe.Context;
using PubnubApi.EventEngine.Subscribe.Events;
using PubnubApi.EventEngine.Subscribe.States;
using System.Linq;

namespace PubnubApi.Tests.EventEngine
{
internal class HandshakeFailedStateTransition
{
[Test]
public void TestHandshakeFailedStateTransitionWithSubscriptionChangedEvent()
{
//Arrange
var handshakeFailedState = new HandshakeFailedState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } };
var handshakingState = new HandshakingState();
//Act
var result = handshakeFailedState.Transition(new SubscriptionChangedEvent()
private static object[] handshakeFailedEventCases = {
new object[] {
new HandshakeFailedState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } },
new SubscriptionChangedEvent()
{
Channels = new string[] { "ch1", "ch2", "ch3" },
ChannelGroups = new string[] { "cg1", "cg2", "cg3" }
},
new HandshakingState(){ Channels = new string[] { "ch1", "ch2", "ch3" }, ChannelGroups = new string[] { "cg1", "cg2", "cg3" } }
},
new object[]
{
Channels = new string[] { "ch1", "ch2", "ch3" },
ChannelGroups = new string[] { "cg1", "cg2", "cg3" }
} );
//Assert
Assert.IsTrue(result.State.GetType().Equals(handshakingState.GetType()));
Assert.AreEqual("ch1", ((HandshakingState)(result.State)).Channels.ElementAt(0));
Assert.AreEqual("ch2", ((HandshakingState)(result.State)).Channels.ElementAt(1));
Assert.AreEqual("ch3", ((HandshakingState)(result.State)).Channels.ElementAt(2));
Assert.AreEqual("cg1", ((HandshakingState)(result.State)).ChannelGroups.ElementAt(0));
Assert.AreEqual("cg2", ((HandshakingState)(result.State)).ChannelGroups.ElementAt(1));
Assert.AreEqual("cg3", ((HandshakingState)(result.State)).ChannelGroups.ElementAt(2));
}
new HandshakeFailedState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } },
new SubscriptionRestoredEvent()
{
Channels = new string[] { "ch1", "ch2" },
ChannelGroups = new string[] { "cg1", "cg2" }
},
new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } }
},
new object[]
{
new HandshakeFailedState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } },
new ReconnectEvent()
{
Channels = new string[] { "ch1", "ch2" },
ChannelGroups = new string[] { "cg1", "cg2" },
Cursor = new SubscriptionCursor() { Region = 1, Timetoken = 1234567890 }
},
new HandshakingState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } }
}
};

[Test]
public void TestHandshakeFailedStateTransitionWithSubscriptionRestoredEvent()
[TestCaseSource(nameof(handshakeFailedEventCases))]
public void When_EventOccurs_HandshakeFailedStateTransitionsToHandshakingState(
HandshakeFailedState handshakeFailedState, IEvent @event, HandshakingState expectedState)
{
//Arrange
var handshakeFailedState = new HandshakeFailedState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } };
var handshakingState = new HandshakingState();
//Act
var result = handshakeFailedState.Transition(new SubscriptionRestoredEvent()
{
Channels = new string[] { "ch1", "ch2" },
ChannelGroups = new string[] { "cg1", "cg2" }
} );
var result = handshakeFailedState.Transition(@event);

//Assert
Assert.IsTrue(result.State.GetType().Equals(handshakingState.GetType()));
Assert.AreEqual("ch1", ((HandshakingState)(result.State)).Channels.ElementAt(0));
Assert.AreEqual("ch2", ((HandshakingState)(result.State)).Channels.ElementAt(1));
Assert.AreEqual("cg1", ((HandshakingState)(result.State)).ChannelGroups.ElementAt(0));
Assert.AreEqual("cg2", ((HandshakingState)(result.State)).ChannelGroups.ElementAt(1));
Assert.IsInstanceOf<HandshakingState>(result.State);
Assert.AreEqual(expectedState.Channels, ((HandshakingState)result.State).Channels);
Assert.AreEqual(expectedState.ChannelGroups, ((HandshakingState)result.State).ChannelGroups);
if (@event is ReconnectEvent reconnectEvent)
{
Assert.AreEqual(reconnectEvent.Cursor, ((HandshakingState)result.State).Cursor);
}
}

[Test]
public void TestHandshakeFailedStateTransitionWithReconnectEvent()
[TestCase]
public void When_UnsubscribeAllEventOccurs_HandshakeFailedStateTransitionsToUnsubscribedState()
{
//Arrange
var handshakeFailedState = new HandshakeFailedState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } };
var handshakingState = new HandshakingState();
//Act
var result = handshakeFailedState.Transition(new ReconnectEvent()
// Arrange
var handshakeFailedState = new HandshakeFailedState()
{
Channels = new string[] { "ch1", "ch2" },
ChannelGroups = new string[] { "cg1", "cg2" },
Cursor = new SubscriptionCursor() { Region = 1, Timetoken = 1234567890 }
} );
//Assert
Assert.IsTrue(result.State.GetType().Equals(handshakingState.GetType()));
Assert.AreEqual("ch1", ((HandshakingState)(result.State)).Channels.ElementAt(0));
Assert.AreEqual("ch2", ((HandshakingState)(result.State)).Channels.ElementAt(1));
Assert.AreEqual("cg1", ((HandshakingState)(result.State)).ChannelGroups.ElementAt(0));
Assert.AreEqual("cg2", ((HandshakingState)(result.State)).ChannelGroups.ElementAt(1));
Assert.AreEqual(1, ((HandshakingState)(result.State)).Cursor.Region);
Assert.AreEqual(1234567890, ((HandshakingState)(result.State)).Cursor.Timetoken);
}
Channels = new string[] { "ch1", "ch2" },
ChannelGroups = new string[] { "cg1", "cg2" }
};

var @event = new UnsubscribeAllEvent() { };

[Test]
public void TestHandshakeFailedStateTransitionWithUnsubscribeEvent()
{
//Arrange
var handshakeFailedState = new HandshakeFailedState() { Channels = new string[] { "ch1", "ch2" }, ChannelGroups = new string[] { "cg1", "cg2" } };
var unsubscribedState = new UnsubscribedState();
//Act
var result = handshakeFailedState.Transition(new UnsubscribeAllEvent() { });
var result = handshakeFailedState.Transition(@event);

//Assert
Assert.IsTrue(result.State.GetType().Equals(unsubscribedState.GetType()));
Assert.IsInstanceOf<UnsubscribedState>(result.State);
}

}
}

0 comments on commit bbcca51

Please sign in to comment.