public async Task WhenConnectionFails_AttachingOrAttachedChannelsShouldTrasitionToFailedWithSameError(ChannelState state) { var error = new ErrorInfo(); (_channel as RealtimeChannel).SetChannelState(state); await _client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Error) { Error = error }); _client.Connection.State.Should().Be(ConnectionState.Failed); _channel.State.Should().Be(ChannelState.Failed); _channel.ErrorReason.Should().BeSameAs(error); }
public async Task ShouldSendDetachMessageAndOnceDetachedReceviedShouldMigrateToDetached() { SetState(ChannelState.Attached); _channel.Detach(); LastCreatedTransport.LastMessageSend.Action.Should().Be(ProtocolMessage.MessageAction.Detach); _channel.State.Should().Be(ChannelState.Detaching); await _client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Detached) { Channel = _channel.Name }); _channel.State.Should().Be(ChannelState.Detached); }
public async Task Release_ShouldRemoveChannelWhenDetached() { // Arrange var channel = Channels.Get("test"); channel.Attach(); Channels.Release("test"); // Act await _realtime.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Detached, "test")); await Task.Delay(50); // Assert Channels.Should().BeEmpty(); }
private async Task ReceiveAttachedMessage() { await _client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Attached) { Channel = _channel.Name }); }
public async Task WhenConnectedMessageContainsClientId_AuthClientIdShouldBeTheSame() { // Arrange var options = new ClientOptions(ValidKey) { TransportFactory = new FakeTransportFactory(), SkipInternetCheck = true }; var mobileDevice = new FakeMobileDevice(); var realtime = new AblyRealtime(options, mobileDevice: mobileDevice); const string newClientId = "testId"; var localDevice = realtime.Device; localDevice.ClientId.Should().BeNull(); // Act realtime.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Connected) { ConnectionDetails = new ConnectionDetails { ClientId = newClientId }, }); await realtime.WaitForState(ConnectionState.Connected); // Assert realtime.Auth.ClientId.Should().Be(newClientId); localDevice.ClientId.Should().Be(newClientId); mobileDevice.GetPreference(PersistKeys.Device.ClientId, PersistKeys.Device.SharedName).Should().Be(newClientId); }
private async Task CloseAndWaitToReconnect(AblyRealtime client, ProtocolMessage protocolMessage = null) { protocolMessage = protocolMessage ?? new ProtocolMessage(ProtocolMessage.MessageAction.Connected); LastCreatedTransport.Listener.OnTransportEvent(TransportState.Closed); await new ConnectionAwaiter(client.Connection, ConnectionState.Connecting).Wait(); await client.FakeProtocolMessageReceived(protocolMessage); }
public static void FakeMessageReceived(this AblyRealtime client, Message message, string channel = null) { client.FakeProtocolMessageReceived( new ProtocolMessage(ProtocolMessage.MessageAction.Message) { Messages = new[] { message }, Channel = channel }); }
private async Task CloseAndWaitToReconnect(AblyRealtime client, ProtocolMessage connectedMessage = null) { connectedMessage = connectedMessage ?? new ProtocolMessage(ProtocolMessage.MessageAction.Connected); LastCreatedTransport.Listener.OnTransportEvent(LastCreatedTransport.Id, TransportState.Closed); await client.WaitForState(ConnectionState.Connecting); client.FakeProtocolMessageReceived(connectedMessage); await client.WaitForState(ConnectionState.Connected); await client.ProcessCommands(); }
public static async Task DisconnectWithRetryableError(this AblyRealtime client) { client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Disconnected) { Error = new ErrorInfo { StatusCode = HttpStatusCode.GatewayTimeout } }); await client.WaitForState(ConnectionState.Disconnected); }
public async Task WithMultipleMessages_ShouldSetIdOnEachMessage() { var channel = _client.Channels.Get("test"); SetState(channel, ChannelState.Attached); List <Message> receivedMessages = new List <Message>(); channel.Subscribe(msg => { receivedMessages.Add(msg); }); var protocolMessage = SetupTestProtocolmessage(); await _client.FakeProtocolMessageReceived(protocolMessage); receivedMessages.Should().HaveCount(3); receivedMessages.Select(x => x.Id) .Should() .BeEquivalentTo(new[] { $"{protocolMessage.Id}:0", $"{protocolMessage.Id}:1", $"{protocolMessage.Id}:2" }); }
public async Task WithFallbackHost_ShouldMakeRestRequestsOnSameHost() { var response = new HttpResponseMessage(HttpStatusCode.Accepted) { Content = new StringContent("[12345678]") }; var handler = new FakeHttpMessageHandler(response); var client = new AblyRealtime(new ClientOptions(ValidKey) { UseBinaryProtocol = false, UseSyncForTesting = true, SkipInternetCheck = true, TransportFactory = _fakeTransportFactory }); client.RestClient.HttpClient.CreateInternalHttpClient(TimeSpan.FromSeconds(10), handler); await client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Connected) { ConnectionDetails = new ConnectionDetails() { ConnectionKey = "connectionKey" }, ConnectionId = "1", ConnectionSerial = 100 }); await client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Error) { Error = new ErrorInfo() { StatusCode = HttpStatusCode.GatewayTimeout } }); await client.TimeAsync(); handler.Requests.Last().RequestUri.ToString().Should().Contain(client.Connection.Host); }
internal async Task WhenClientIdChangesAfterInitialisation_StateMachineShouldReceive_GotPushDeviceDetailsEvent(Func <ActivationStateMachine, ActivationStateMachine.State> createCurrentState) { // Arrange const string initialClientId = "123"; var options = new ClientOptions(ValidKey) { TransportFactory = new FakeTransportFactory(), SkipInternetCheck = true, ClientId = initialClientId }; var mobileDevice = new FakeMobileDevice(); var realtime = new AblyRealtime(options, mobileDevice: mobileDevice); const string newClientId = "testId"; var localDevice = realtime.Device; // Make sure the LocalDevice is registered realtime.Device.DeviceIdentityToken = "token"; localDevice.ClientId.Should().Be(initialClientId); // Initialise the activation statemachine and set a fake state to record the next event. realtime.Push.InitialiseStateMachine(); var taskAwaiter = new TaskCompletionAwaiter(); realtime.Push.StateMachine.CurrentState = createCurrentState(realtime.Push.StateMachine); realtime.Push.StateMachine.ProcessingEventCallback = @event => { // Check we received the correct event @event.Should().BeOfType <ActivationStateMachine.GotPushDeviceDetails>(); taskAwaiter.Done(); }; // Pretend we are connected and change the ClientId realtime.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Connected) { ConnectionDetails = new ConnectionDetails { ClientId = newClientId }, }); await realtime.WaitForState(ConnectionState.Connected); // Check the clientId is set correctly realtime.Auth.ClientId.Should().Be(newClientId); localDevice.ClientId.Should().Be(newClientId); // It's necessary to pause the current thread and let the background action to complete which fires the event. await Task.Delay(100); (await taskAwaiter).Should().BeTrue(); mobileDevice.GetPreference(PersistKeys.Device.ClientId, PersistKeys.Device.SharedName).Should().Be(newClientId); }
public static async Task ConnectClient(this AblyRealtime client) { await client.WaitForState(ConnectionState.Connecting, TimeSpan.FromMilliseconds(10000)); client.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Connected) { ConnectionDetails = new ConnectionDetails { ConnectionKey = "connectionKey" }, ConnectionId = "1", ConnectionSerial = 100 }); await client.WaitForState(ConnectionState.Connected); }
internal async Task WhenClientIdChangesAfterInitialisationAndStateMachineIsNotActivated_ShouldNotFireEvent() { // Arrange const string initialClientId = "123"; var options = new ClientOptions(ValidKey) { TransportFactory = new FakeTransportFactory(), SkipInternetCheck = true, ClientId = initialClientId }; var mobileDevice = new FakeMobileDevice(); var realtime = new AblyRealtime(options, mobileDevice: mobileDevice); const string newClientId = "testId"; var localDevice = realtime.Device; // Make sure the LocalDevice is registered realtime.Device.DeviceIdentityToken = "token"; localDevice.ClientId.Should().Be(initialClientId); realtime.Push.InitialiseStateMachine(); var taskAwaiter = new TaskCompletionAwaiter(1000); realtime.Push.StateMachine.ProcessingEventCallback = @event => { taskAwaiter.Done(); }; // Pretend we are connected and change the ClientId realtime.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Connected) { ConnectionDetails = new ConnectionDetails { ClientId = newClientId }, }); await realtime.WaitForState(ConnectionState.Connected); // It's necessary to pause the current thread and let the background action to complete which fires the event. await Task.Delay(100); // No event should be sent to the statemachine (await taskAwaiter).Should().BeFalse(); }
public async Task WhenConnectedMessageContainsClientId_AuthClientIdShouldBeTheSame() { // Arrange var options = new ClientOptions(ValidKey) { TransportFactory = new FakeTransportFactory(), SkipInternetCheck = true }; var realtime = new AblyRealtime(options); const string clientId = "testId"; // Act realtime.FakeProtocolMessageReceived(new ProtocolMessage(ProtocolMessage.MessageAction.Connected) { ConnectionDetails = new ConnectionDetails { ClientId = clientId }, }); await realtime.WaitForState(ConnectionState.Connected); // Assert realtime.Auth.ClientId.Should().Be(clientId); }