From b5f2219fe5ea5797b2c662620982d227b799e879 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Thu, 20 Jul 2023 22:29:05 +0530 Subject: [PATCH] Refactored connection recovery spec tests --- .../ConnectionRecoverySpecs.cs | 62 +++++++++++++------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/ConnectionRecoverySpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/ConnectionRecoverySpecs.cs index 515ad7c5b..d205cd5b6 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/ConnectionRecoverySpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/ConnectionSpecs/ConnectionRecoverySpecs.cs @@ -1,3 +1,4 @@ +using System; using System.Threading.Tasks; using FluentAssertions; using IO.Ably.Realtime; @@ -11,32 +12,57 @@ namespace IO.Ably.Tests.Realtime.ConnectionSpecs public class ConnectionRecoverySpecs : AblyRealtimeSpecs { [Fact] - [Trait("spec", "RTN16c")] - public async Task WhenConnectionIsClosed_ConnectionIdAndKeyShouldBeReset() + [Trait("spec", "RTN16g")] + [Trait("spec", "RTN16g1")] + public async Task CreateRecoveryKey_ShouldReturnSerializedConnectionKeyAndMsgSerialAndChannelSerials() { - var client = await GetConnectedClient(); + const string expectedRecoveryKey = "{\"connectionKey\":\"connectionKey\",\"msgSerial\":0,\"channelSerials\":{}}"; - client.Close(); + var client = GetClientWithFakeTransport(); + var connectedProtocolMessage = new ProtocolMessage(ProtocolMessage.MessageAction.Connected) + { + ConnectionDetails = new ConnectionDetails { ConnectionKey = "connectionKey" }, + ConnectionId = "1" + }; + client.FakeProtocolMessageReceived(connectedProtocolMessage); + await client.WaitForState(ConnectionState.Connected); + + client.Connection.CreateRecoveryKey().Should().Be(expectedRecoveryKey); + } + + [Fact] + [Trait("spec", "RTN16g2")] + public async Task CreateRecoveryKey_ShouldReturnNullRecoveryKeyForNullConnectionKeyOrWhenStateIsClosed() + { + var client = GetClientWithFakeTransport(); + client.Connection.CreateRecoveryKey().Should().BeNullOrEmpty(); // connectionKey is empty - client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Closed)); + client.FakeProtocolMessageReceived(ConnectedProtocolMessage); + await client.WaitForState(ConnectionState.Connected); + client.Connection.CreateRecoveryKey().Should().NotBeNullOrEmpty(); + client.Close(); await client.WaitForState(ConnectionState.Closed); - client.Connection.Id.Should().BeNullOrEmpty(); - client.Connection.Key.Should().BeNullOrEmpty(); + client.Connection.CreateRecoveryKey().Should().BeNullOrEmpty(); } [Fact] - [Trait("spec", "RTN16g")] - public async Task RecoveryKey_ShouldContainSerializedConnectionKeyAndMsgSerialAndChannelSerials() + [Trait("spec", "RTN16m")] + [System.Obsolete] + public async Task DeprecatedRecoveryKeyProperty_ShouldBehaveSameAsCreateRecoveryKey() { - var client = await GetConnectedClient(); - var expectedRecoveryKey = new RecoveryKeyContext() + const string expectedRecoveryKey = "{\"connectionKey\":\"connectionKey\",\"msgSerial\":0,\"channelSerials\":{}}"; + + var client = GetClientWithFakeTransport(); + var connectedProtocolMessage = new ProtocolMessage(ProtocolMessage.MessageAction.Connected) { - ConnectionKey = client.Connection.Key, - MsgSerial = client.Connection.MessageSerial, - ChannelSerials = client.Channels.GetChannelSerials(), - }.Encode(); - client.Connection.CreateRecoveryKey().Should().Be(expectedRecoveryKey); + ConnectionDetails = new ConnectionDetails { ConnectionKey = "connectionKey" }, + ConnectionId = "1" + }; + client.FakeProtocolMessageReceived(connectedProtocolMessage); + await client.WaitForState(ConnectionState.Connected); + + client.Connection.RecoveryKey.Should().Be(expectedRecoveryKey); } [Fact] @@ -47,14 +73,14 @@ public async Task RecoveryKey_MsgSerialShouldNotBeSentToAblyButShouldBeSetOnConn { var recoveryKey = "{\"connectionKey\":\"uniqueKey\",\"msgSerial\":45,\"channelSerials\":{\"channel1\":\"1\",\"channel2\":\"2\",\"channel3\":\"3\"}}"; - var client = GetRealtimeClient(options => { options.Recover = recoveryKey; }); + var client = GetClientWithFakeTransport(options => { options.Recover = recoveryKey; }); var transportParams = await client.ConnectionManager.CreateTransportParameters("https://realtime.ably.io"); var paramsDict = transportParams.GetParams(); paramsDict.ContainsKey("recover").Should().BeTrue(); paramsDict["recover"].Should().Be("uniqueKey"); - paramsDict.ContainsKey("msg_serial").Should().BeFalse(); + client.Connection.MessageSerial.Should().Be(45); }