Skip to content

Commit

Permalink
Refactored connection recovery spec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Jul 20, 2023
1 parent 0f478f9 commit b5f2219
Showing 1 changed file with 44 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using IO.Ably.Realtime;
Expand All @@ -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]
Expand All @@ -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);
}

Expand Down

0 comments on commit b5f2219

Please sign in to comment.