Exemplo n.º 1
0
        public void Retry_SpecificException_Predicate_Test()
        {
            // Keep track of attempts
            var tryCount = 0;

            // Act and assert
            Assert.Catch(() =>
            {
                Retry.Create()
                .Catch <DummyException>(false, e => e.DummyProperty != null)
                .Execute(() =>
                {
                    tryCount++;

                    if (tryCount < 5)
                    {
                        throw new DummyException("Hello world");
                    }

                    throw new DummyException();
                });
            });

            // Assert
            Assert.That(tryCount, Is.EqualTo(5));
        }
Exemplo n.º 2
0
        public void Retry_squares_by_division()
        {
            var t = this.SourceProbe <int>()
                    .Select(i => Tuple.Create(i, i * i))
                    .Via(Retry.Create(RetryFlow <int>(), s =>
            {
                if (s % 4 == 0)
                {
                    return(Tuple.Create(s / 2, s / 4));
                }
                var sqrt = (int)Math.Sqrt(s);
                return(Tuple.Create(sqrt, s));
            }))
                    .ToMaterialized(this.SinkProbe <Tuple <Result <int>, int> >(), Keep.Both)
                    .Run(Sys.Materializer());

            var source = t.Item1;
            var sink   = t.Item2;

            sink.Request(99);
            source.SendNext(1);
            sink.ExpectNext().Item1.Value.Should().Be(2);
            source.SendNext(2);
            sink.ExpectNext().Item1.Value.Should().Be(2);
            source.SendNext(4);
            sink.ExpectNext().Item1.Value.Should().Be(2);
            sink.ExpectNoMsg(TimeSpan.FromSeconds(3));
            source.SendComplete();
            sink.ExpectComplete();
        }
Exemplo n.º 3
0
        public async Task Retry_AnyException_Async_Test()
        {
            // Keep track of attempts
            var tryCount = 0;

            // Act
            var result = await Retry.Create()
                         .CatchAnyException()
                         .ExecuteAsync(async() =>
            {
                await Task.Yield();

                tryCount++;

                if (tryCount < 5)
                {
                    throw new DummyException();
                }

                return(true);
            });

            // Assert
            Assert.That(result);
        }
Exemplo n.º 4
0
        public void Tolerate_killswitch_terminations_inside_the_flow_after_start()
        {
            var innerFlow = RetryFlow <int>().ViaMaterialized(KillSwitches.Single <Tuple <Result <int>, int> >(), Keep.Right);

            var t = this.SourceProbe <int>()
                    .Select(i => Tuple.Create(i, i * i))
                    .ViaMaterialized(Retry.Create(innerFlow, x =>
            {
                if (x % 4 == 0)
                {
                    return(Tuple.Create(x / 2, x / 4));
                }
                var sqrt = (int)Math.Sqrt(x);
                return(Tuple.Create(sqrt, x));
            }), Keep.Both)
                    .ToMaterialized(this.SinkProbe <Tuple <Result <int>, int> >(), Keep.Both)
                    .Run(Sys.Materializer());

            var killSwitch = t.Item1.Item2;
            var sink       = t.Item2;

            sink.Request(99);
            killSwitch.Abort(FailedElement.Exception);
            sink.ExpectError().InnerException.Should().Be(FailedElement.Exception);
        }
Exemplo n.º 5
0
        public void Retry_SpecificException_Test()
        {
            // Keep track of attempts
            var tryCount = 0;

            // Act
            var result = Retry.Create()
                         .Catch <DummyExceptionA>()
                         .Catch <DummyExceptionB>()
                         .Execute(() =>
            {
                tryCount++;

                if (tryCount < 3)
                {
                    throw new DummyExceptionA();
                }

                if (tryCount < 5)
                {
                    throw new DummyExceptionB();
                }

                return(true);
            });

            // Assert
            Assert.That(result);
        }
Exemplo n.º 6
0
        public Result <List <float> > forecast(int numSteps)
        {
            // Hold the requests in case we don't have any updated or simply any API to receive the requests
            // Retry a specified number of times, using a function to
            // calculate the duration to wait between retries based on
            // the current retry attempt (allows for exponential backoff)
            // In this case will wait for
            //  2 ^ 1 = 2 seconds then
            //  2 ^ 2 = 4 seconds then
            //  2 ^ 3 = 8 seconds then
            //  2 ^ 4 = 16 seconds then
            Policy <bool> .HandleResult(false).WaitAndRetry(4, retryAttempt =>
                                                            TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                                                            ).Execute(() => _coordinator.getApiHostPorts().Count > 0);

            var endpoint = string.Format("{0}/{1}", FORECAST, numSteps);

            try
            {
                var jsonBodyResp = Retry.Create()
                                   .CatchAnyException()
                                   .WithDelay(i => TimeSpan.FromSeconds(i * 0.5))
                                   .WithMaxTryCount(MAX_RETRY_FORECAST_CALL)
                                   .Execute <string>(() =>
                                                     _client.GetStringAsync(createUri(_coordinator.getApiHostPorts(), endpoint)).Result);

                return(deserialize <ApiModelForecastResponse>(jsonBodyResp)
                       .Map(value => value.Data));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(new Result <List <float> >(e));
            }
        }
Exemplo n.º 7
0
        public DataService(SettingsService settingsService, CacheService cacheService)
        {
            _settingsService = settingsService;
            _cacheService    = cacheService;

            // Connection limit
            ServicePointManager.DefaultConnectionLimit = 999;

            // Client
            var handler = new HttpClientHandler();

            if (handler.SupportsAutomaticDecompression)
            {
                handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }
            handler.UseCookies = false;
            _httpClient        = new HttpClient(handler, true);
            _httpClient.DefaultRequestHeaders.Add("User-Agent", "osu!helper (github.com/Tyrrrz/OsuHelper)");

            // Rate limiting
            _requestRateSemaphore = new SemaphoreSlim(1, 1);
            _lastRequestDateTime  = DateTime.MinValue;

            // Request retry policy
            // (osu web server is inconsistent)
            _requestRetryPolicy = Retry.Create()
                                  .Catch <HttpErrorStatusCodeException>(false, ex => (int)ex.StatusCode >= 500)
                                  .Catch <HttpRequestException>(false, ex => ex.InnerException is IOException)
                                  .WithMaxTryCount(20);
        }
Exemplo n.º 8
0
 public void 读取文件_最大重试10次()
 {
     string text =
         Retry.Create(100, 0)        // 指定重试10,使用默认的重试间隔时间,且不分辨异常类型(有异常就重试)
         .Run(() => {
         return(System.IO.File.ReadAllText(@"c:\abc.txt", Encoding.UTF8));
     });
 }
Exemplo n.º 9
0
        private async Task <JToken> GetApiResponseAsync(AuthToken token, string resource, string endpoint,
                                                        params string[] parameters)
        {
            // Create retry policy
            var retry = Retry.Create()
                        .Catch <HttpErrorStatusCodeException>(false, e => (int)e.StatusCode >= 500)
                        .Catch <HttpErrorStatusCodeException>(false, e => (int)e.StatusCode == 429)
                        .WithMaxTryCount(10)
                        .WithDelay(TimeSpan.FromSeconds(0.4));

            // Send request
            return(await retry.ExecuteAsync(async() =>
            {
                // Create request
                const string apiRoot = "https://discordapp.com/api/v6";
                using (var request = new HttpRequestMessage(HttpMethod.Get, $"{apiRoot}/{resource}/{endpoint}"))
                {
                    // Set authorization header
                    request.Headers.Authorization = token.Type == AuthTokenType.Bot
                        ? new AuthenticationHeaderValue("Bot", token.Value)
                        : new AuthenticationHeaderValue(token.Value);

                    // Add parameters
                    foreach (var parameter in parameters)
                    {
                        var key = parameter.SubstringUntil("=");
                        var value = parameter.SubstringAfter("=");

                        // Skip empty values
                        if (value.IsNullOrWhiteSpace())
                        {
                            continue;
                        }

                        request.RequestUri = request.RequestUri.SetQueryParameter(key, value);
                    }

                    // Get response
                    using (var response = await _httpClient.SendAsync(request))
                    {
                        // Check status code
                        // We throw our own exception here because default one doesn't have status code
                        if (!response.IsSuccessStatusCode)
                        {
                            throw new HttpErrorStatusCodeException(response.StatusCode, response.ReasonPhrase);
                        }

                        // Get content
                        var raw = await response.Content.ReadAsStringAsync();

                        // Parse
                        return JToken.Parse(raw);
                    }
                }
            }));
        }
Exemplo n.º 10
0
        public void Test_Retry_不指定过滤器()
        {
            TestRetryTask1 task = new TestRetryTask1();

            string text = Retry.Create(5, 10).Run(() => {
                return(task.Exec1());
            });

            Assert.AreEqual(TestRetryTask1.Result, text);
        }
Exemplo n.º 11
0
        public void 发送HTTP请求_支持重试()
        {
            // 如果发生异常,最多重试5次,间隔 500 毫秒
            Retry retry = Retry.Create(5, 500);

            string text = new HttpOption {
                Url  = "http://www.fish-web-demo.com/abc.aspx",
                Data = new { id = 2, name = "abc" }
            }.GetResult(retry);
        }
Exemplo n.º 12
0
        public void 发送HTTP请求_定制重试策略()
        {
            Retry retry =
                Retry.Create(100, 2000)     // 重试 100 次,间隔 2 秒
                .Filter <WebException>();   // 仅当出现 WebException 异常时才重试。

            string text = new HttpOption {
                Url  = "http://www.fish-web-demo.com/abc.aspx",
                Data = new { id = 2, name = "abc" }
            }.GetResult(retry);
        }
Exemplo n.º 13
0
        public void Test_Retry_指定部分过滤器_将会出现异常2()
        {
            TestRetryTask1 task = new TestRetryTask1();

            string text = Retry.Create(5, 10)
                          .Filter <NotSupportedException>()
                          .Run(() => {
                return(task.Exec2());
            });

            Assert.AreEqual(TestRetryTask1.Result, text);
        }
Exemplo n.º 14
0
        private HttpResponseMessage doPostCall <T>(ImmutableList <APIHostPort> apiHostPorts, string endpoint, T objBody)
        {
            var json     = serializeJson(objBody);
            var jsonBody = new StringContent(json, Encoding.UTF8, "application/json");

            return(Retry.Create()
                   .CatchAnyException()
                   .WithDelay(i => TimeSpan.FromSeconds(i * 0.5))
                   .WithMaxTryCount(MAX_RETRY)
                   .Execute <HttpResponseMessage>(() =>
                                                  _client.PostAsync(createUri(apiHostPorts, endpoint), jsonBody).Result));
        }
Exemplo n.º 15
0
        public void Test_Retry_指定部分过滤器_将会出现异常()
        {
            TestRetryTask1 task = new TestRetryTask1();

            string text = Retry.Create(5, 10)
                          .Filter <NotSupportedException>()
                          .Filter <ArgumentOutOfRangeException>(ex => ex.ParamName == "name")
                          .Run(() => {
                return(task.Exec2());
            });

            Assert.AreEqual(TestRetryTask1.Result, text);
        }
Exemplo n.º 16
0
        internal static void SendData(object data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            HttpOption httpOption = CreateHttpOption(data);

            Retry retry = Retry.Create(s_retryCount, s_retryWaitMillisecond);

            httpOption.GetResult(retry);
        }
Exemplo n.º 17
0
        public void Tolerate_killswitch_terminations_before_start()
        {
            var t = this.SourceProbe <int>()
                    .ViaMaterialized(KillSwitches.Single <int>(), Keep.Right)
                    .Select(i => Tuple.Create(i, i))
                    .Via(Retry.Create(RetryFlow <int>(), x => Tuple.Create(x, x + 1)))
                    .ToMaterialized(this.SinkProbe <Tuple <Result <int>, int> >(), Keep.Both)
                    .Run(Sys.Materializer());

            var killSwitch = t.Item1;
            var sink       = t.Item2;

            killSwitch.Abort(FailedElement.Exception);
            sink.Request(1);
            sink.ExpectError().InnerException.Should().Be(FailedElement.Exception);
        }
Exemplo n.º 18
0
        public void Test_Retry_指定过滤器_指定回调方法()
        {
            int            count  = 0;
            int            count2 = 0;
            TestRetryTask1 task   = new TestRetryTask1();

            string text = Retry.Create(5, 10)
                          .Filter <NotSupportedException>()
                          .OnException((ex, n) => { count++; })
                          .OnException((ex, n) => { count2 += n; })
                          .Run(() => {
                return(task.Exec3());
            });

            Assert.AreEqual(TestRetryTask1.Result, text);
            Assert.AreEqual(3, count);
            Assert.AreEqual(6, count2);
        }
Exemplo n.º 19
0
        /// <summary>
        /// 发送HTTP请求
        /// </summary>
        /// <param name="data"></param>
        public virtual void SendData(object data)
        {
            if (data == null)
            {
                return;
            }

            HttpOption httpOption = CreateHttpOption(data);

            if (httpOption == null)
            {
                return;
            }

            Retry retry = Retry.Create(_retryCount, _retryWaitMillisecond);

            httpOption.GetResult(retry);
        }
Exemplo n.º 20
0
        public void Retry_should_retry_ints_according_to_their_parity()
        {
            var t = this.SourceProbe <int>()
                    .Select(i => Tuple.Create(i, i))
                    .Via(Retry.Create(RetryFlow <int>(), s => s < 42 ? Tuple.Create(s + 1, s + 1) : null))
                    .ToMaterialized(this.SinkProbe <Tuple <Result <int>, int> >(), Keep.Both)
                    .Run(Sys.Materializer());

            var source = t.Item1;
            var sink   = t.Item2;

            sink.Request(99);
            source.SendNext(1);
            sink.ExpectNext().Item1.Value.Should().Be(2);
            source.SendNext(2);
            sink.ExpectNext().Item1.Value.Should().Be(4);
            source.SendNext(42);
            sink.ExpectNext().Item1.Should().Be(FailedElement);
            source.SendComplete();
            sink.ExpectComplete();
        }
Exemplo n.º 21
0
        public void Retry_SpecificException_DifferentException_DerivedException_Test()
        {
            // Keep track of attempts
            var tryCount = 0;

            // Act and assert
            Assert.Catch(() =>
            {
                Retry.Create()
                .Catch <DummyException>()
                .Execute(() =>
                {
                    tryCount++;

                    if (tryCount < 5)
                    {
                        throw new DummyExceptionA();
                    }
                });
            });
        }
Exemplo n.º 22
0
        public void Retry_AnyException_ExceedRetryLimit_Test()
        {
            // Keep track of attempts
            const int maxTryCount = 10;
            var       tryCount    = 0;

            // Act and assert
            Assert.Catch(() =>
            {
                Retry.Create()
                .WithMaxTryCount(maxTryCount)
                .CatchAnyException()
                .Execute(() =>
                {
                    tryCount++;

                    throw new DummyException();
                });
            });

            Assert.That(tryCount, Is.EqualTo(maxTryCount));
        }
Exemplo n.º 23
0
        public void Retry_descending_ints_until_success()
        {
            var t = this.SourceProbe <int>()
                    .Select(i => Tuple.Create(i, Enumerable.Range(0, i).Reverse().Select(x => x * 2).Concat(new[] { i + 1 }).ToImmutableList()))
                    .Via(Retry.Create(RetryFlow <ImmutableList <int> >(),
                                      s => s.IsEmpty
                        ? throw new IllegalStateException("should not happen")
                        : Tuple.Create(s[0], s.RemoveAt(0))))
                    .ToMaterialized(this.SinkProbe <Tuple <Result <int>, ImmutableList <int> > >(), Keep.Both)
                    .Run(Sys.Materializer());

            var source = t.Item1;
            var sink   = t.Item2;

            sink.Request(99);
            source.SendNext(1);
            sink.ExpectNext().Item1.Value.Should().Be(2);
            source.SendNext(2);
            sink.ExpectNext().Item1.Value.Should().Be(4);
            source.SendNext(40);
            sink.ExpectNext().Item1.Value.Should().Be(42);
            source.SendComplete();
            sink.ExpectComplete();
        }
Exemplo n.º 24
0
 private static Retry GetDefaultRetry()
 {
     // 默认的重试规则:
     // 重试5次,间隔 500 毫秒,且仅在WebException发生时重试
     return(Retry.Create(s_tryCount, s_WaitMillisecond).Filter <WebException>());
 }
Exemplo n.º 25
0
 private static Retry GetDefaultRetry()
 {
     // 默认的重试规则:
     // 重试5次,间隔 1 秒,且仅在WebException发生时重试
     return(Retry.Create(5, 1000).Filter <WebException>());
 }