Skip to content

Commit

Permalink
Updated connectionSandbox tests for ack/nack
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed Sep 18, 2023
1 parent 201feb1 commit 4857065
Showing 1 changed file with 29 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,104 +15,55 @@ namespace IO.Ably.Tests.Realtime
[Trait("type", "integration")]
public class ConnectionSandboxTransportSideEffectsSpecs : SandboxSpecs
{
/*
* (RTN19b) If there are any pending channels i.e. in the ATTACHING or DETACHING state,
* the respective ATTACH or DETACH message should be resent to Ably
*/
[Theory(Skip = "Intermittently fails")]
[Theory]
[ProtocolData]
[Trait("spec", "RTN19b")]
public async Task WithChannelInAttachingState_WhenTransportIsDisconnected_ShouldResendAttachMessageOnConnectionResumed(Protocol protocol)
{
var channelName = "test-channel".AddRandomSuffix();
var sentMessages = new List<ProtocolMessage>();

int attachMessageCount = 0;

AblyRealtime client = null;
client = await GetRealtimeClient(protocol, (options, settings) =>
{
options.TransportFactory = new TestTransportFactory
{
OnMessageSent = OnMessageSent,
};
});
var client = await GetRealtimeClient(protocol);
await client.WaitForState(ConnectionState.Connected);

void OnMessageSent(ProtocolMessage message)
{
sentMessages.Add(message);
if (message.Action == ProtocolMessage.MessageAction.Attach)
{
if (attachMessageCount == 0)
{
attachMessageCount++;
client.GetTestTransport().Close(suppressClosedEvent: false);
}
}
}
// Will be unblocked on new transport/connection
client.BlockActionFromSending(ProtocolMessage.MessageAction.Attach);

bool didDisconnect = false;
client.Connection.Once(ConnectionEvent.Disconnected, change =>
var channel = client.Channels.Get("RTN19b".AddRandomSuffix());
channel.Once(ChannelEvent.Attaching, change =>
{
didDisconnect = true;
client.GetTestTransport().Close(suppressClosedEvent: false);
});

await client.WaitForState(ConnectionState.Connected);

var channel = client.Channels.Get(channelName);
channel.Attach();

await channel.WaitForState(ChannelState.Attaching);
await channel.AttachAsync();

await client.WaitForState(ConnectionState.Disconnected);
await client.WaitForState(ConnectionState.Connecting);
channel.State.Should().Be(ChannelState.Attaching);
await client.WaitForState(ConnectionState.Connected);

client.Connection.State.Should().Be(ConnectionState.Connected);
didDisconnect.Should().BeTrue();

await channel.WaitForAttachedState();

var attachCount = sentMessages.Count(x => x.Channel == channelName && x.Action == ProtocolMessage.MessageAction.Attach);
attachCount.Should().Be(2);

client.Close();
}

[Theory]
[ProtocolData]
[Trait("spec", "RTN19b")]
[Trait("intermittent", "true")] // I think the logic behind resending the detach message has an issue
[Trait("description", "detached only works if detach message is received on old transport")]
public async Task WithChannelInDetachingState_WhenTransportIsDisconnected_ShouldResendDetachMessageOnConnectionResumed(Protocol protocol)
{
int detachMessageCount = 0;
AblyRealtime client = null;
var channelName = "test-channel".AddRandomSuffix();
var client = await GetRealtimeClient(protocol);
await client.WaitForState(ConnectionState.Connected);

client = await GetRealtimeClient(protocol, (options, settings) =>
{
options.TransportFactory = new TestTransportFactory
{
OnMessageSent = OnMessageSent,
};
});
var channel = client.Channels.Get("RTN19b".AddRandomSuffix());
await channel.AttachAsync();

void OnMessageSent(ProtocolMessage message)
// Will be reset on new transport/connection
client.GetTestTransport().AfterMessageSent += message =>
{
if (message.Action == ProtocolMessage.MessageAction.Detach)
{
if (detachMessageCount == 0)
{
detachMessageCount++;
client.GetTestTransport().Close(suppressClosedEvent: false);
}
client.BlockActionFromReceiving(ProtocolMessage.MessageAction.Detached);
client.GetTestTransport().Close(suppressClosedEvent: false);
}
}
};

await client.WaitForState(ConnectionState.Connected);

var channel = client.Channels.Get(channelName);
await channel.AttachAsync();
channel.Detach();
await channel.WaitForState(ChannelState.Detaching);

Expand All @@ -126,19 +77,19 @@ void OnMessageSent(ProtocolMessage message)
[Theory]
[ProtocolData]
[Trait("spec", "RTN19")]
[Trait("spec", "RTN19a")]
[Trait("spec", "RTN19a1")]
public async Task OnConnected_ShouldResendAckWithConnectionMessageSerialIfResumeFailed(Protocol protocol)
{
var client = await GetRealtimeClient(protocol);
await client.WaitForState(ConnectionState.Connected);
var initialConnectionId = client.Connection.Id;
var channel = client.Channels.Get("RTN19a".AddRandomSuffix());

// var client2 = await GetRealtimeClient(protocol);
// var channel2 = client2.Channels.Get(channel.Name);
// await channel2.AttachAsync();
// var channel2Messages = new List<Message>();
// channel2.Subscribe(message => channel2Messages.Add(message));
var client2 = await GetRealtimeClient(protocol);
var channel2 = client2.Channels.Get(channel.Name);
await channel2.AttachAsync();
var channel2Messages = new List<Message>();
channel2.Subscribe(message => channel2Messages.Add(message));

// Sending dummy messages to increment messageSerial
await channel.PublishAsync("dummy1", "data1");
Expand Down Expand Up @@ -186,16 +137,16 @@ public async Task OnConnected_ShouldResendAckWithConnectionMessageSerialIfResume
initialMessagesIdToSerialMap[keyValuePair.Key].Should().NotBe(keyValuePair.Value);
}

// TODO - Message duplicates can't be detected since messages id's not available
// channel2Messages.Count.Should().Be(noOfMessagesSent + 2); // first 2 dummy messages
// Duplicate messages received on second channel
channel2Messages.Count.Should().Be((noOfMessagesSent * 2) + 2); // add first 2 dummy messages

client.Close();
}

[Theory]
[ProtocolData]
[Trait("spec", "RTN19")]
[Trait("spec", "RTN19b")]
[Trait("spec", "RTN19a2")]
public async Task OnConnected_ShouldResendAckWithSameMessageSerialIfResumeSuccessful(Protocol protocol)
{
var client = await GetRealtimeClient(protocol);
Expand Down

0 comments on commit 4857065

Please sign in to comment.