From 00343723ab15f9abfb792bcb1e2e029c597f1d3a Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Sun, 17 Sep 2023 00:41:01 +0530 Subject: [PATCH] Refactored connectionSandboxSpec tests, removed simulateLostConnection --- .../Realtime/ConnectionSandBoxSpecs.cs | 33 ++++++++++++++----- src/IO.Ably.Tests.Shared/TestExtensions.cs | 7 ---- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/IO.Ably.Tests.Shared/Realtime/ConnectionSandBoxSpecs.cs b/src/IO.Ably.Tests.Shared/Realtime/ConnectionSandBoxSpecs.cs index 0f311dac7..1655f9783 100644 --- a/src/IO.Ably.Tests.Shared/Realtime/ConnectionSandBoxSpecs.cs +++ b/src/IO.Ably.Tests.Shared/Realtime/ConnectionSandBoxSpecs.cs @@ -489,7 +489,7 @@ public async Task ResumeRequest_ConnectedProtocolMessageWithSameConnectionId_Wit public async Task ResumeRequest_ConnectedProtocolMessageWithSameConnectionId_WithError(Protocol protocol) { var client = await GetRealtimeClient(protocol); - var channel = (RealtimeChannel)client.Channels.Get("RTN15c1".AddRandomSuffix()); + var channel = (RealtimeChannel)client.Channels.Get("RTN15c2".AddRandomSuffix()); await client.WaitForState(ConnectionState.Connected); var connectionId = client.Connection.Id; @@ -584,7 +584,9 @@ public async Task ResumeRequest_ConnectedProtocolMessageWithResumeFailedShouldEm var oldConnectionId = client.Connection.Id; var oldKey = client.Connection.Key; - client.SimulateLostConnectionAndState(); + client.State.Connection.Id = string.Empty; + client.State.Connection.Key = "xxxxx!xxxxxxx-xxxxxxxx-xxxxxxxx"; // invalid connection key for next resume request + client.GetTestTransport().Close(false); ConnectionStateChange stateChange = null; await WaitFor(done => @@ -610,16 +612,18 @@ await WaitFor(done => client.Connection.Id.Should().NotBe(oldConnectionId); client.Connection.Key.Should().NotBe(oldKey); client.Connection.MessageSerial.Should().Be(0); + + client.Close(); } [Theory] [ProtocolData] [Trait("spec", "RTN15c6")] [Trait("spec", "RTN15c7")] - public async Task ResumeRequest_ConnectedProtocolMessageWithNewConnectionId_AttachedAllChannels(Protocol protocol) + public async Task ResumeRequest_ConnectedProtocolMessageWithSameOrNewConnectionId_AttachesAllChannels(Protocol protocol) { var client = await GetRealtimeClient(protocol); - var channelName = "RTN15c3".AddRandomSuffix(); + var channelName = "RTN15c6.RTN15c7.".AddRandomSuffix(); const int channelCount = 5; await client.WaitForState(ConnectionState.Connected); @@ -642,9 +646,11 @@ await WaitForMultiple(channelCount, partialDone => }); } }); + client.State.Connection.Id = string.Empty; + client.State.Connection.Key = "xxxxx!xxxxxxx-xxxxxxxx-xxxxxxxx"; // invalid connection key for next resume request + client.GetTestTransport().Close(false); - client.SimulateLostConnectionAndState(); - await client.WaitForState(ConnectionState.Connected); + // Should send message on the channel so it gets into pending state await WaitForMultiple(channelCount, partialDone => { @@ -661,7 +667,10 @@ await WaitForMultiple(channelCount, partialDone => } }); + // Check if channels have processed pending queued messages attachedChannels.Should().HaveCount(channelCount); + + client.Close(); } [Theory] @@ -758,12 +767,16 @@ public async Task ResumeRequest_WithTokenAuthError_TransportWillBeClosed(Protoco var prevTransport = client.GetTestTransport(); ConnectionStateChange stateChange = null; - client.Connection.Once(ConnectionEvent.Disconnected, change => + await WaitFor(done => { - stateChange = change; + client.Connection.Once(ConnectionEvent.Disconnected, change => + { + stateChange = change; + done(); + }); }); - await client.WaitForState(ConnectionState.Disconnected); // Disconnected due to token expired stateChange.Should().NotBeNull(); + stateChange.Current.Should().Be(ConnectionState.Disconnected); // Disconnected due to token expired stateChange.HasError.Should().BeTrue(); stateChange.Reason.Code.Should().Be(ErrorCodes.TokenExpired); @@ -773,6 +786,8 @@ public async Task ResumeRequest_WithTokenAuthError_TransportWillBeClosed(Protoco var newTransport = client.GetTestTransport(); newTransport.Should().NotBe(prevTransport); client.Connection.Id.Should().Be(prevConnectionId); // connection should be resumed, connectionId should be unchanged + + client.Close(); } [Theory] diff --git a/src/IO.Ably.Tests.Shared/TestExtensions.cs b/src/IO.Ably.Tests.Shared/TestExtensions.cs index 3b279ddee..d18fc8630 100644 --- a/src/IO.Ably.Tests.Shared/TestExtensions.cs +++ b/src/IO.Ably.Tests.Shared/TestExtensions.cs @@ -61,13 +61,6 @@ internal static void BlockActionFromReceiving(this IRealtimeClient client, Proto transport.BlockReceiveActions.Add(action); } - internal static void SimulateLostConnectionAndState(this AblyRealtime client) - { - client.State.Connection.Id = string.Empty; - client.State.Connection.Key = "xxxxx!xxxxxxx-xxxxxxxx-xxxxxxxx"; - client.GetTestTransport().Close(false); - } - internal static void BeforeProtocolMessageProcessed(this AblyRealtime client, Action action) { var t = client.GetTestTransport();