Exemplo n.º 1
0
        private void RunTest(
            string environment,
            string application,
            string testName,
            RouteContext context)
        {
            using (Log.OnEnterAndExit())
            {
                TestTarget target;

                try
                {
                    target = testTargets.Get(environment, application);
                }
                catch (TestNotDefinedException)
                {
                    context.Handler = async httpContext => { httpContext.Response.StatusCode = (int)HttpStatusCode.NotFound; };
                    return;
                }

                var testDefinition = testDefinitions.Get(testName);

                if (!testDefinition.AppliesTo(target))
                {
                    return;
                }

                context.Handler = async httpContext =>
                {
                    TestResult result;

                    try
                    {
                        TraceBuffer.Initialize();

                        var returnValue = await testDefinition.Run(
                            httpContext,
                            target.ResolveDependency);

                        if (returnValue is Task task)
                        {
                            await task;

                            if (task.GetType().IsGenericType)
                            {
                                var genericTypeParameter = task.GetType().GenericTypeArguments.First();

                                if (genericTypeParameter.IsPublic)
                                {
                                    returnValue = ((dynamic)task).Result;
                                }
                                else
                                {
                                    returnValue = null;
                                }
                            }
                        }

                        result = TestResult.Pass(returnValue);
                    }
                    catch (ParameterFormatException exception)
                    {
                        result = TestResult.Fail(exception);
                        httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    }
                    catch (Exception exception)
                    {
                        result = TestResult.Fail(exception);
                        httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    }

                    var json = JsonConvert.SerializeObject(result);

                    await httpContext.Response.WriteAsync(json);
                };
            }
        }
Exemplo n.º 2
0
        private async Task RunTest(
            string environment,
            string application,
            string testName,
            RouteContext context)
        {
            using (Log.OnEnterAndExit())
            {
                TestTarget target;

                try
                {
                    target = testTargets.Get(environment, application);
                }
                catch (TestNotDefinedException)
                {
                    context.Handler = async httpContext =>
                    {
                        await Task.Yield();

                        httpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                    };
                    return;
                }

                var testDefinition = testDefinitions.Get(testName);

                if (!testDefinition.AppliesTo(target))
                {
                    return;
                }

                var url      = context.HttpContext.Request.GetLink(target, testDefinition);
                var testInfo = new TestInfo(target.Application, target.Environment, testDefinition.TestName, new Uri(url), testDefinition.Tags);

                context.Handler = async httpContext =>
                {
                    TestResult result;

                    var stopwatch = Stopwatch.StartNew();

                    try
                    {
                        TraceBuffer.Initialize();

                        var container = target.DependencyRegistry.Container;

                        if (target.RequiresServiceWarmup)
                        {
                            var warmup = container.Resolve <ServiceWarmupTracker>();
                            await warmup.WarmUp();
                        }

                        var returnValue = await testDefinition.Run(
                            httpContext,
                            container.Resolve,
                            target);

                        if (returnValue is Task task)
                        {
                            await task;

                            if (task.GetType().IsGenericType)
                            {
                                var genericTypeParameter = task.GetType().GenericTypeArguments.First();

                                if (genericTypeParameter.IsPublic)
                                {
                                    // task is Task<T> so await to get its return value
                                    returnValue = await(dynamic) task;
                                }
                                else
                                {
                                    returnValue = null;
                                }
                            }
                        }

                        result = TestResult.Pass(returnValue, stopwatch.Elapsed, testInfo);
                    }
                    catch (ParameterFormatException exception)
                    {
                        result = TestResult.Fail(exception, stopwatch.Elapsed, testInfo);
                        httpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    }
                    catch (TestInconclusiveException tie)
                    {
                        result = TestResult.Inconclusive(tie, stopwatch.Elapsed, testInfo);
                        httpContext.Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;
                    }
                    catch (TestTimeoutException tte)
                    {
                        result = TestResult.Timeout(tte, stopwatch.Elapsed, testInfo);
                        httpContext.Response.StatusCode = (int)HttpStatusCode.GatewayTimeout;
                    }
                    catch (TestFailedException tfe)
                    {
                        result = TestResult.Fail(tfe, stopwatch.Elapsed, testInfo);
                        httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    }
                    catch (Exception exception)
                    {
                        result = TestResult.Fail(exception, stopwatch.Elapsed, testInfo);
                        httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                    }

                    var json = JsonConvert.SerializeObject(result, SerializerSettings);

                    await httpContext.Response.WriteAsync(json);
                };
            }
        }