Exemplo n.º 1
0
                public async Task Will_Retry_Up_To_Retry_Limit_And_Pause_Between_Each_Attempt()
                {
                    const int MaxRetries       = 3;
                    const int ExpectedAttempts = MaxRetries + 1;

                    var blockProcessingException = new RpcClientTimeoutException("fake exception");
                    int timesThrown = 0;

                    MockProcessingStrategy
                    .SetupGet(s => s.MaxRetries).Returns(MaxRetries);

                    MockBlockProcessor
                    .Setup(p => p.ProcessBlockAsync(0))
                    .Callback <long>((blkNum) => timesThrown++)
                    .Throws(blockProcessingException);

                    for (var retryNum = 0; retryNum < MaxRetries; retryNum++)
                    {
                        MockProcessingStrategy
                        .Setup(s => s.PauseFollowingAnError(retryNum))
                        .Returns(Task.CompletedTask);
                    }

                    await Processor.ExecuteAsync(0, 0);

                    Assert.Equal(ExpectedAttempts, timesThrown);
                    MockProcessingStrategy.VerifyAll();
                }
Exemplo n.º 2
0
        public async Task <int> ReceiveBufferedResponseAsync(ClientWebSocket client, byte[] buffer)
        {
            try
            {
                var timeoutToken = new CancellationTokenSource(ForceCompleteReadTotalMilliseconds).Token;
                var tokenSource  = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token, timeoutToken);

                if (client == null)
                {
                    return(0);
                }
                var segmentBuffer = new ArraySegment <byte>(buffer);
                var result        = await client
                                    .ReceiveAsync(segmentBuffer, tokenSource.Token)
                                    .ConfigureAwait(false);

                return(result.Count);
            }
            catch (TaskCanceledException ex)
            {
                var exception = new RpcClientTimeoutException($"Rpc timeout after {ConnectionTimeout.TotalMilliseconds} milliseconds", ex);
                HandleError(exception);
                throw exception;
            }
        }
Exemplo n.º 3
0
        private async Task ConnectWebSocketAsync()
        {
            CancellationTokenSource tokenSource = null;

            try
            {
                if (_clientWebSocket == null || _clientWebSocket.State != WebSocketState.Open)
                {
                    tokenSource      = new CancellationTokenSource(ConnectionTimeout);
                    _clientWebSocket = new ClientWebSocket();
                    await _clientWebSocket.ConnectAsync(new Uri(_path), tokenSource.Token).ConfigureAwait(false);
                }
            }
            catch (TaskCanceledException ex)
            {
                var rpcException = new RpcClientTimeoutException($"Websocket connection timeout after {ConnectionTimeout.TotalMilliseconds} milliseconds", ex);
                HandleError(rpcException);
                throw rpcException;
            }
            catch (Exception ex)
            {
                HandleError(ex);
                throw;
            }
            finally
            {
                tokenSource?.Dispose();
            }
        }
        public async Task <Tuple <int, bool> > ReceiveBufferedResponseAsync(ClientWebSocket client, byte[] buffer)
        {
            CancellationTokenSource timeoutTokenSource = null;
            CancellationTokenSource tokenSource        = null;

            try
            {
                timeoutTokenSource = new CancellationTokenSource(ForceCompleteReadTotalMilliseconds);
                tokenSource        = CancellationTokenSource.CreateLinkedTokenSource(_cancellationTokenSource.Token, timeoutTokenSource.Token);

                if (client == null)
                {
                    return(new Tuple <int, bool>(0, true));
                }
                var segmentBuffer = new ArraySegment <byte>(buffer);
                var result        = await client
                                    .ReceiveAsync(segmentBuffer, tokenSource.Token)
                                    .ConfigureAwait(false);

                return(new Tuple <int, bool>(result.Count, result.EndOfMessage));
            }
            catch (TaskCanceledException ex)
            {
                var exception = new RpcClientTimeoutException($"Rpc timeout after {ConnectionTimeout.TotalMilliseconds} milliseconds", ex);
                HandleError(exception);
                throw exception;
            }
            finally
            {
                timeoutTokenSource?.Dispose();
                tokenSource?.Dispose();
            }
        }
Exemplo n.º 5
0
        protected override async Task <RpcResponseMessage> SendAsync(RpcRequestMessage request, string route = null)
        {
            RpcLogger          rpcLogger = new RpcLogger(_log);
            RpcResponseMessage rpcResponseMessage;

            try
            {
                var cancellationTokenSource = new CancellationTokenSource();
                cancellationTokenSource.CancelAfter(ConnectionTimeout);

                using (var pipeStream = new NamedPipeClientStream(IpcPath))
                {
                    await pipeStream.ConnectAsync(cancellationTokenSource.Token);

                    string str   = JsonConvert.SerializeObject(request, JsonSerializerSettings);
                    byte[] bytes = Encoding.UTF8.GetBytes(str);
                    rpcLogger.LogRequest(str);
                    await pipeStream.WriteAsync(bytes, 0, bytes.Length, cancellationTokenSource.Token);

                    using (MemoryStream fullResponse = await ReceiveFullResponseAsync(pipeStream, cancellationTokenSource.Token))
                    {
                        fullResponse.Position = 0L;
                        using (StreamReader streamReader = new StreamReader(fullResponse))
                        {
                            using (JsonTextReader jsonTextReader = new JsonTextReader(streamReader))
                            {
                                RpcResponseMessage responseMessage = JsonSerializer.Create(JsonSerializerSettings).Deserialize <RpcResponseMessage>(jsonTextReader);
                                rpcLogger.LogResponse(responseMessage);
                                rpcResponseMessage = responseMessage;
                            }
                        }
                    }
                }
            }
            catch (TaskCanceledException ex)
            {
                var exception = new RpcClientTimeoutException($"Rpc timeout after {ConnectionTimeout.TotalMilliseconds} milliseconds", ex);
                rpcLogger.LogException(exception);
                throw exception;
            }
            catch (Exception ex)
            {
                var unknownException = new RpcClientUnknownException("Error occurred when trying to send ipc requests(s)", ex);
                rpcLogger.LogException(unknownException);
                throw unknownException;
            }
            return(rpcResponseMessage);
        }
        private async Task ConnectWebSocketAsync()
        {
            CancellationTokenSource tokenSource = null;

            try
            {
                if (_clientWebSocket == null || _clientWebSocket.State != WebSocketState.Open)
                {
                    tokenSource = new CancellationTokenSource(ConnectionTimeout);

                    _clientWebSocket?.Dispose();
                    _clientWebSocket = new ClientWebSocket();
                    if (RequestHeaders != null)
                    {
                        foreach (var requestHeader in RequestHeaders)
                        {
                            _clientWebSocket.Options.SetRequestHeader(requestHeader.Key, requestHeader.Value);
                        }
                    }

                    await _clientWebSocket.ConnectAsync(new Uri(_path), tokenSource.Token).ConfigureAwait(false);

                    //Random random = new Random();
                    //var x = random.Next(0, 30);
                    //if (x == 5) throw new Exception("Error");
                }
            }
            catch (TaskCanceledException ex)
            {
                var rpcException = new RpcClientTimeoutException($"Websocket connection timeout after {ConnectionTimeout.TotalMilliseconds} milliseconds", ex);
                HandleError(rpcException);
                throw rpcException;
            }
            catch (Exception ex)
            {
                HandleError(ex);
                throw;
            }
            finally
            {
                tokenSource?.Dispose();
            }
        }