public async Task Expected_server_disconnect()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateTransportClient();
            _connection = await _client.Connect();

            var             handshakeTcs     = new TaskCompletionSource <string>();
            MessageReceived handshakeHandler = (sender,
                                                args) => handshakeTcs.SetResult(args.Message);

            _connection.MessageReceived += handshakeHandler;
            _connection.Send(TestMessageStr);
            var response = await handshakeTcs.Task.WithTimeout(s => s,
                                                               5.Seconds());

            dynamic responseObj = JsonConvert.DeserializeObject <JArray>(response)[0];
            string  clientId    = responseObj.clientId;
            var     triggerServerDisconnectMessage =
                new { channel = "/meta/disconnect", clientId };
            var             disconnectJson    = JsonConvert.SerializeObject(triggerServerDisconnectMessage);
            var             disconnectTcs     = new TaskCompletionSource <string>();
            MessageReceived disconnectHandler = (sender,
                                                 args) => disconnectTcs.SetResult(args.Message);

            _connection.MessageReceived -= handshakeHandler;
            _connection.MessageReceived += disconnectHandler;
            var closedTcs = new TaskCompletionSource <bool>();

            _connection.ConnectionClosed += (sender,
                                             args) => closedTcs.SetResult(true);

            // act
            _connection.NotifyOfPendingServerDisconnection();
            _connection.Send(disconnectJson);
            await disconnectTcs.Task.WithTimeout(s => s,
                                                 10.Seconds());

            await closedTcs.Task.WithTimeout(s => s,
                                             10.Seconds());

            // assert
            _connection
            .ConnectionState
            .Should()
            .Be(ConnectionState.Disconnected);
        }
        public async Task Connect_send_receive_and_disconnect()
        {
            // arrange
            _fayeServerProcess.StartThinServer();
            var socket = new WebSocketClient(uri: TEST_SERVER_URL);

            SetupWebSocket(socket);
            InstantiateTransportClient();
            _connection = await _client.Connect();

            var tcs = new TaskCompletionSource <string>();

            _connection.MessageReceived += (sender,
                                            args) => tcs.SetResult(args.Message);
            // act
            _connection.Send(TestMessageStr);
            var response = await tcs.Task.WithTimeout(s => s,
                                                      5.Seconds());

            // assert
            dynamic responseObj = JsonConvert.DeserializeObject <JArray>(response)[0];
            bool    successful  = responseObj.successful;

            successful
            .Should()
            .BeTrue();
        }
        internal static Task Close(this ITransportConnection connection, string connectionId)
        {
            var command = new Command
            {
                Type = CommandType.Disconnect
            };

            return(connection.Send(new ConnectionMessage(connectionId, command)));
        }
Пример #4
0
        public static Task Abort(this ITransportConnection connection, string connectionId)
        {
            var command = new Command
            {
                Type = CommandType.Abort
            };

            return(connection.Send(connectionId, command));
        }
Пример #5
0
        public static Task Close(this ITransportConnection connection, string connectionId)
        {
            var command = new SignalCommand
            {
                Type = CommandType.Disconnect
            };

            return(connection.Send(SignalCommand.AddCommandSuffix(connectionId), command));
        }
Пример #6
0
        protected void SendConnect(string clientId,
                                   ITransportConnection connection)
        {
            var message = new ConnectRequestMessage(clientId: clientId,
                                                    connectionType: ONLY_SUPPORTED_CONNECTION_TYPE,
                                                    id: MessageCounter++);
            var json = Converter.Serialize(message);

            connection.Send(json);
        }
        public async Task Connection_dies_reestablishes_after_message_send()
        {
            // arrange
            const int inputPort = THIN_SERVER_PORT + 1;

            _fayeServerProcess.StartThinServer();
            _socatInterceptor = StartWritableSocket(hostname: "localhost",
                                                    inputPort: inputPort);
            const string urlThroughSocat = "ws://localhost:8133/bayeux";
            var          socket          = new WebSocketClient(uri: urlThroughSocat);

            SetupWebSocket(socket);
            InstantiateTransportClient();
            _connection = await _client.Connect();

            var tcs = new TaskCompletionSource <string>();

            _connection.MessageReceived += (sender,
                                            args) => tcs.SetResult(args.Message);
            var lostTcs = new TaskCompletionSource <bool>();

            _connection.ConnectionLost += (sender,
                                           args) => lostTcs.SetResult(true);

            // act
            // ReSharper disable once CSharpWarnings::CS4014
            Task.Factory.StartNew(() =>
            {
                Logger.Info("Killing socat");
                _socatInterceptor.Kill();
                Logger.Info("Sleeping");
                Thread.Sleep(5.Seconds());
                Logger.Info("Restarting socat");
                _socatInterceptor = StartWritableSocket(hostname: "localhost",
                                                        inputPort: inputPort);
            });
            Logger.Info("Waiting for websocket to acknowledge disconnect");
            await lostTcs.Task.WithTimeout(t => t,
                                           20.Seconds());

            Logger.Info("Disconnect acknowledged, sending message");
            _connection.Send(TestMessageStr);

            // assert
            var response = await tcs.Task.WithTimeout(s => s,
                                                      15.Seconds());

            dynamic responseObj = JsonConvert.DeserializeObject <JArray>(response)[0];
            bool    successful  = responseObj.successful;

            successful
            .Should()
            .BeTrue();
        }
        private static Task SendCommand(ITransportConnection connection, string connectionId, CommandType commandType)
        {
            var command = new Command
            {
                CommandType = commandType
            };

            var message = new ConnectionMessage(PrefixHelper.GetConnectionId(connectionId),
                                                command);

            return connection.Send(message);
        }
Пример #9
0
        private static Task SendCommand(ITransportConnection connection, string connectionId, CommandType commandType)
        {
            var command = new Command
            {
                CommandType = commandType
            };

            var message = new ConnectionMessage(PrefixHelper.GetConnectionId(connectionId),
                                                command);

            return(connection.Send(message));
        }
Пример #10
0
        private async Task <T> ExecuteSynchronousMessage <T>(BaseFayeMessage message,
                                                             TimeSpan timeoutValue) where T : BaseFayeMessage
        {
            var json = Converter.Serialize(message);
            var tcs  = new TaskCompletionSource <MessageReceivedArgs>();

            _synchronousMessageEvents[message.Id] = tcs;
            _connection.Send(json);
            var task   = tcs.Task;
            var result = await task.Timeout(timeoutValue);

            if (result == Result.Timeout)
            {
                var timeoutException = new TimeoutException(timeoutValue,
                                                            json);
                _logger.ErrorException("Timeout problem, rethrowing",
                                       timeoutException);
                throw timeoutException;
            }
            _synchronousMessageEvents.Remove(message.Id);
            return(Converter.Deserialize <T>(task.Result.Message));
        }
Пример #11
0
        private async Task <T> ExecuteSynchronousMessage <T>(BaseFayeMessage message,
                                                             TimeSpan timeoutValue) where T : BaseFayeMessage
        {
            var             json     = Converter.Serialize(message);
            var             tcs      = new TaskCompletionSource <MessageReceivedArgs>();
            MessageReceived received = (sender,
                                        args) => tcs.SetResult(args);

            _transportConnection.MessageReceived += received;
            _transportConnection.Send(json);
            var task   = tcs.Task;
            var result = await task.Timeout(timeoutValue);

            if (result == Result.Timeout)
            {
                var timeoutException = new TimeoutException(timeoutValue,
                                                            json);
                _logger.ErrorException("Timeout problem, rethrowing",
                                       timeoutException);
                throw timeoutException;
            }
            var receivedString = task.Result.Message;

            _logger.Debug("Received message '{0}'",
                          receivedString);
            _transportConnection.MessageReceived -= received;
            var     array      = JsonConvert.DeserializeObject <JArray>(receivedString);
            dynamic messageObj = array[0];
            var     newAdvice  = ParseAdvice(messageObj);

            if (newAdvice != null)
            {
                _advice = newAdvice;
                SetRetry(_advice,
                         _transportConnection);
            }
            return(Converter.Deserialize <T>(receivedString));
        }