Ejemplo n.º 1
0
        protected override void Dispose(bool disposing)
        {
            // Disposal has to perform two actions, unloading the runner and
            // stopping the agent. Both must be tried even if one fails so
            // there can be up to two independent errors to be reported
            // through an NUnitEngineException. We do that by combining messages.
            if (!_disposed && disposing)
            {
                _disposed = true;

                string unloadError = null;

                try
                {
                    Unload();
                }
                catch (Exception ex)
                {
                    // Save and log the unload error
                    unloadError = ex.Message;
                    log.Error(unloadError);
                }

                if (_agent != null && _agency.IsAgentRunning(_agent.Id))
                {
                    try
                    {
                        log.Debug("Stopping remote agent");
                        _agent.Stop();
                        _agent = null;
                    }
                    catch (Exception e)
                    {
                        string stopError = string.Format("Failed to stop the remote agent. {0}", e.Message);
                        log.Error(stopError);
                        _agent = null;

                        // Stop error with no unload error, just rethrow
                        if (unloadError == null)
                        {
                            throw;
                        }

                        // Both kinds of errors, throw exception with combined message
                        throw new NUnitEngineException(unloadError + Environment.NewLine + stopError);
                    }
                }

                if (unloadError != null) // Add message line indicating we managed to stop agent anyway
                {
                    throw new NUnitEngineException(unloadError + Environment.NewLine + "Agent Process was terminated successfully after error.");
                }
            }
        }
Ejemplo n.º 2
0
        protected override void Dispose(bool disposing)
        {
            // Disposal has to perform two actions, unloading the runner and
            // stopping the agent. Both must be tried even if one fails so
            // there can be up to two independent errors to be reported
            // through an NUnitEngineException. We do that by combining messages.
            if (!_disposed && disposing)
            {
                _disposed = true;

                Exception unloadException = null;

                try
                {
                    Unload();
                }
                catch (Exception ex)
                {
                    // Save and log the unload error
                    unloadException = ex;
                    log.Error(ExceptionHelper.BuildMessage(ex));
                    log.Error(ExceptionHelper.BuildMessageAndStackTrace(ex));
                }

                if (_agent != null && _agency.IsAgentRunning(_agent.Id))
                {
                    try
                    {
                        log.Debug("Stopping remote agent");
                        _agent.Stop();
                    }
                    catch (SocketException se)
                    {
                        var exitCode = _agency.GetAgentExitCode(_agent.Id);

                        if (exitCode.HasValue && exitCode == 0)
                        {
                            log.Warning("Agent connection was forcibly closed. Exit code was 0, so agent shutdown OK");
                        }
                        else
                        {
                            var stopError = $"Agent connection was forcibly closed. Exit code was {exitCode?.ToString() ?? "unknown"}. {Environment.NewLine}{ExceptionHelper.BuildMessageAndStackTrace(se)}";
                            log.Error(stopError);

                            // Stop error with no unload error, just rethrow
                            if (unloadException == null)
                            {
                                throw;
                            }

                            // Both kinds of errors, throw exception with combined message
                            throw new NUnitEngineUnloadException(ExceptionHelper.BuildMessage(unloadException) + Environment.NewLine + stopError);
                        }
                    }
                    catch (Exception e)
                    {
                        var stopError = "Failed to stop the remote agent." + Environment.NewLine + ExceptionHelper.BuildMessageAndStackTrace(e);
                        log.Error(stopError);

                        // Stop error with no unload error, just rethrow
                        if (unloadException == null)
                        {
                            throw;
                        }

                        // Both kinds of errors, throw exception with combined message
                        throw new NUnitEngineUnloadException(ExceptionHelper.BuildMessage(unloadException) + Environment.NewLine + stopError);
                    }
                    finally
                    {
                        _agent = null;
                    }
                }

                if (unloadException != null) // Add message line indicating we managed to stop agent anyway
                {
                    throw new NUnitEngineUnloadException("Agent Process was terminated successfully after error.", unloadException);
                }
            }
        }