예제 #1
0
        async Task RunLoop()
        {
            int?runId = null;

            try
            {
                runId = await CreateTestRun();

                while (!shouldExit || !addQueue.IsEmpty || !updateQueue.IsEmpty)
                {
                    workEvent.WaitOne();                     // Wait for work

                    // Get local copies of the queues
                    var aq = Interlocked.Exchange(ref addQueue, new ConcurrentQueue <IDictionary <string, object?> >());
                    var uq = Interlocked.Exchange(ref updateQueue, new ConcurrentQueue <IDictionary <string, object?> >());

                    if (previousErrors)
                    {
                        break;
                    }

                    // We have to do adds before update because we need the test ID from the add to inject into the update
                    await SendTestResults(true, runId.Value, aq.ToArray()).ConfigureAwait(false);
                    await SendTestResults(false, runId.Value, uq.ToArray()).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                logger.LogError($"VstsClient.RunLoop: Could not create test run. Message: {e.Message}");
            }
            finally
            {
                try
                {
                    if (runId.HasValue)
                    {
                        await FinishTestRun(runId.Value);
                    }
                    else
                    {
                        logger.LogError("RunId is not set, cannot complete test run");
                    }
                }
                catch (Exception e)
                {
                    logger.LogError($"VstsClient.RunLoop: Could not finish test run. Message: {e.Message}");
                }

                finished.Set();
            }
        }
예제 #2
0
        /// <summary>
        /// Logs an error message.
        /// </summary>
        /// <param name="logger">The logger</param>
        /// <param name="message">The message to be logged</param>
        public static void LogError(
            this IRunnerLogger logger,
            string message)
        {
            Guard.ArgumentNotNull(logger);
            Guard.ArgumentNotNull(message);

            logger.LogError(StackFrameInfo.None, message);
        }
예제 #3
0
        /// <summary>
        /// Logs a formatted error message.
        /// </summary>
        /// <param name="logger">The logger</param>
        /// <param name="messageFormat">The format of the message to be logged</param>
        /// <param name="args">The format arguments</param>
        public static void LogError(
            this IRunnerLogger logger,
            string messageFormat,
            params object?[] args)
        {
            Guard.ArgumentNotNull(logger);
            Guard.ArgumentNotNull(messageFormat);

            logger.LogError(StackFrameInfo.None, string.Format(messageFormat, args));
        }
예제 #4
0
        public static void SendRequest(IRunnerLogger logger, string url, HttpMethod method, object body)
        {
            if (previousErrors)
            {
                return;
            }

            lock (jsonMediaType)
            {
                using (var finished = new ManualResetEvent(false))
                {
                    XunitWorkerThread.QueueUserWorkItem(async() =>
                    {
                        var bodyString = ToJson(body);

                        try
                        {
                            var bodyBytes = Encoding.UTF8.GetBytes(bodyString);

                            var request     = new HttpRequestMessage(method, url);
                            request.Content = new ByteArrayContent(bodyBytes);
                            request.Content.Headers.ContentType = jsonMediaType;
                            request.Headers.Accept.Add(jsonMediaType);

                            using (var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
                            {
                                var response = await client.SendAsync(request, tcs.Token);
                                if (!response.IsSuccessStatusCode)
                                {
                                    logger.LogWarning($"When sending '{method} {url}', received status code '{response.StatusCode}'; request body: {bodyString}");
                                    previousErrors = true;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.LogError($"When sending '{method} {url}' with body '{bodyString}', exception was thrown: {ex.Message}");
                            previousErrors = true;
                        }
                        finally
                        {
                            finished.Set();
                        }
                    });

                    finished.WaitOne();
                }
            }
        }
예제 #5
0
        public static void SendRequest(IRunnerLogger logger, string url, HttpMethod method, object body)
        {
            if (previousErrors)
                return;

            lock (jsonMediaType)
            {
                using (var finished = new ManualResetEvent(false))
                {
                    XunitWorkerThread.QueueUserWorkItem(async () =>
                    {
                        var bodyString = ToJson(body);

                        try
                        {
                            var bodyBytes = Encoding.UTF8.GetBytes(bodyString);

                            var request = new HttpRequestMessage(method, url);
                            request.Content = new ByteArrayContent(bodyBytes);
                            request.Content.Headers.ContentType = jsonMediaType;
                            request.Headers.Accept.Add(jsonMediaType);

                            using (var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(10)))
                            {
                                var response = await client.SendAsync(request, tcs.Token);
                                if (!response.IsSuccessStatusCode)
                                {
                                    logger.LogWarning($"When sending '{method} {url}', received status code '{response.StatusCode}'; request body: {bodyString}");
                                    previousErrors = true;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.LogError($"When sending '{method} {url}' with body '{bodyString}', exception was thrown: {ex.Message}");
                            previousErrors = true;
                        }
                        finally
                        {
                            finished.Set();
                        }
                    });

                    finished.WaitOne();
                }
            }
        }
예제 #6
0
        async Task SendRequest(
            HttpMethod method,
            ICollection <IDictionary <string, object?> > body)
        {
            if (body.Count == 0)
            {
                return;
            }

            var bodyString = ToJson(body);

            try
            {
                var bodyBytes = Encoding.UTF8.GetBytes(bodyString);

                var request = new HttpRequestMessage(method, baseUri)
                {
                    Content = new ByteArrayContent(bodyBytes)
                };
                request.Content.Headers.ContentType = jsonMediaType;
                request.Headers.Accept.Add(jsonMediaType);

                using (var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
                {
                    var response = await client.SendAsync(request, tcs.Token).ConfigureAwait(false);

                    if (!response.IsSuccessStatusCode)
                    {
                        logger.LogWarning($"When sending '{method} {baseUri}', received status code '{response.StatusCode}'; request body: {bodyString}");
                        previousErrors = true;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError($"When sending '{method} {baseUri}' with body '{bodyString}', exception was thrown: {ex.Message}");
                throw;
            }
        }
예제 #7
0
 /// <summary>
 /// Logs a formatted error message with stack frame.
 /// </summary>
 /// <param name="logger">The logger</param>
 /// <param name="stackFrame">The stack frame information</param>
 /// <param name="messageFormat">The format of the message to be logged</param>
 /// <param name="args">The format arguments</param>
 public static void LogError(this IRunnerLogger logger, StackFrameInfo stackFrame, string messageFormat, params object[] args)
 {
     logger.LogError(stackFrame, string.Format(messageFormat, args));
 }
예제 #8
0
 /// <summary>
 /// Logs an error message.
 /// </summary>
 /// <param name="logger">The logger</param>
 /// <param name="message">The message to be logged</param>
 public static void LogError(this IRunnerLogger logger, string message)
 {
     logger.LogError(StackFrameInfo.None, message);
 }