예제 #1
0
        public async Task <bool> TestAsync(IQueryable <bool> query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var test = query.Select(condition => condition ? 1 : Sleep(Options.InjectedWaitTime.TotalSeconds)).Take(1);

            using (var writer = WriterPool.Get())
            {
                var visitor = new MySqlQueryModelVisitor(writer);
                var sql     = visitor.Render(test);

                while (true)
                {
                    try
                    {
                        Stopwatch stopwatch = Stopwatch.StartNew();

                        Logger.LogDebug(sql);
                        await Injector.InjectAsync(sql, CancellationToken.None);

                        stopwatch.Stop();
                        WriterPool.Return(writer);

                        return(stopwatch.Elapsed < Options.SuccessfulTime);
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("timed out") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("unrecognized response") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (OperationCanceledException)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                }
            }
        }
        public async Task <bool> TestAsync(IQueryable <bool> query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var expr = ErrorExpressionProvider.GetErrorExpression();

            var p    = Expression.Parameter(typeof(bool), "condition");
            var next = query.Provider.CreateQuery <long>(
                Expression.Call(
                    typeof(Queryable).GetMethods().Where(m => m.Name == nameof(System.Linq.Queryable.Select)).Single(m => !m.GetParameters()[1].ParameterType.ToString().Contains("Int32"))
                    .MakeGenericMethod(typeof(bool), typeof(long)),
                    query.Expression,
                    Expression.Lambda(
                        Expression.Condition(
                            p,
                            expr,
                            Expression.Constant(1L)
                            ),
                        p
                        )
                    )
                ).Take(1);

            using (var writer = WriterPool.Get())
            {
                var visitor = new MySqlQueryModelVisitor(writer);
                var sql     = visitor.Render(next);

                Logger.LogDebug(sql);

                while (true)
                {
                    try
                    {
                        using (var response = await Injector.InjectAsync(sql, CancellationToken.None))
                        {
                            WriterPool.Return(writer);
                            return(await ErrorDetector.ContainsErrorAsync(response));
                        }
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("timed out") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("unrecognized response") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (OperationCanceledException)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                }
            }
        }