public void MillisAreSerializedWithNoPeriodAttribute()
		{
			Timeout period = new Timeout(100);
			StringWriter stringWriter = new StringWriter();
			serializer.Write(new XmlTextWriter(stringWriter), period);
			Assert.AreEqual("<timeout>100</timeout>", stringWriter.ToString());
		}
Пример #2
0
 public override object Read(XmlNode node, NetReflectorTypeTable types)
 {
     Timeout timeout = Timeout.DefaultTimeout;
     if (node is XmlAttribute)
     {
         XmlAttribute a = (XmlAttribute) node;
         try
         {
             timeout = new Timeout(Int32.Parse(a.Value));
         }
         catch (Exception)
         {
             Log.Warning("Could not parse timeout string. Using default timeout.");
         }
     }
     else if (node is XmlElement)
     {
         XmlElement e = (XmlElement) node;
         try
         {
             TimeUnits units = TimeUnits.MILLIS;
             string unitsString = e.GetAttribute("units");
             if (unitsString != null && unitsString != "")
             {
                 units = TimeUnits.Parse(unitsString);
             }
             timeout = new Timeout(Int32.Parse(e.InnerText), units);
         }
         catch (Exception)
         {
             Log.Warning("Could not parse timeout string. Using default timeout.");
         }
     }
     return timeout;
 }
		public void MinutesAreSerializedWithAPeriodAttribute()
		{
			Timeout period = new Timeout(100, TimeUnits.MINUTES);
			StringWriter stringWriter = new StringWriter();
			serializer.Write(new XmlTextWriter(stringWriter), period);
			Assert.AreEqual("<timeout units=\"minutes\">100</timeout>", stringWriter.ToString());
		}
        /// <summary>
        /// Reads the specified node.	
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="types">The types.</param>
        /// <returns></returns>
        /// <remarks></remarks>
        public override object Read(XmlNode node, NetReflectorTypeTable types)
        {
            if (node == null)
            {
                // NetReflector should do this check, but doesn't
                if (this.Attribute.Required)
                {
                    throw new NetReflectorItemRequiredException(Attribute.Name + " is required");
                }
                else
                {
                    return null;
                }
            }

            Timeout timeout = Timeout.DefaultTimeout;
            XmlAttribute a = node as XmlAttribute;

            if (a != null)
            {
                try
                {
                    timeout = new Timeout(Int32.Parse(a.Value, CultureInfo.CurrentCulture));
                }
                catch (Exception)
                {
                    Log.Warning("Could not parse timeout string. Using default timeout.");
                }
            }
            else
            {
                var e = node as XmlElement;
                if (e != null)
                {
                    try
                    {
                        TimeUnits units = TimeUnits.MILLIS;
                        string unitsString = e.GetAttribute("units");
                        if (unitsString != null && !(unitsString != null && unitsString.Length == 0))
                        {
                            units = TimeUnits.Parse(unitsString);
                        }
                        timeout = new Timeout(Int32.Parse(e.InnerText, CultureInfo.CurrentCulture), units);
                    }
                    catch (Exception)
                    {
                        Log.Warning("Could not parse timeout string. Using default timeout.");
                    }
                }
            }
            return timeout;
        }
		private static void AssertTimeoutIs(Timeout expected, string xml)
		{
			StubSourceControl sc = new StubSourceControl(null, null);
			NetReflector.Read(xml, sc);
			Assert.AreEqual(expected, sc.Timeout);
		}
Пример #6
0
        /// <summary>
        /// Gets the request status.
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <returns>A <see cref="HttpRequestStatus"/>.</returns>
        protected HttpRequestStatus GetRequestStatus(HttpRequestSettings settings)
        {
            HttpRequestStatus ret = new HttpRequestStatus { Settings = settings, Success = false };

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(settings.Uri);
            HttpWebResponse response = null;

            if (settings.HasHeaders)
            {
                foreach (HttpRequestHeader header in settings.Headers)
                {
                    request.Headers.Set(header.Name, header.Value);
                }
            }

            if (settings.HasTimeout || settings.HasOverrideTimeout)
            {
                request.Timeout = (settings.OverrideTimeout ?? settings.Timeout).Millis;
            }

            if (settings.HasReadWriteTimeout)
            {
                request.ReadWriteTimeout = settings.ReadWriteTimeout.Millis;
            }

            request.Method = settings.Method;
            if (settings.HasCredentials)
            {
                request.Credentials = settings.Credentials;
            }
            else
            {
                request.UseDefaultCredentials = settings.UseDefaultCredentials;
            }

            ret.RequestTime = DateTime.Now;
            long start = Stopwatch.GetTimestamp();
            Timeout timeout = new Timeout(request.Timeout);
            timeout.Normalize();
            Timeout readWriteTimeout = new Timeout(request.ReadWriteTimeout);
            readWriteTimeout.Normalize();
            Log.Debug("Requesting uri (timeout: {0}, read/write-timeout: {1})...", timeout.ToString(), readWriteTimeout.ToString());
            try
            {
                // TODO this could be better, support chunked transfer etc?
                if (settings.HasBody || settings.HasSendFile)
                {
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        if (settings.HasBody)
                        {
                            using (TextWriter writer = new StreamWriter(requestStream, new UTF8Encoding(false, true)))
                            {
                                writer.Write(settings.Body);
                            }
                        }
                        else
                        {
                            using (FileStream inputFile = File.OpenRead(settings.SendFile))
                            {
                                int read;
                                byte[] buff = new byte[16384];
                                do
                                {
                                    read = inputFile.Read(buff, 0, buff.Length);
                                    requestStream.Write(buff, 0, read);
                                }
                                while (read > 0);
                            }
                        }

                        requestStream.Close();
                    }
                }

                response = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException wEx)
            {
                ret.Exception = wEx;
                if (wEx.Status == WebExceptionStatus.Timeout)
                {
                    ret.TimedOut = true;
                }

                if (wEx.Response is HttpWebResponse)
                {
                    response = (HttpWebResponse)wEx.Response;
                }
            }

            if (response != null)
            {
                ret.StatusCode = response.StatusCode;
                ret.StatusDescription = response.StatusDescription;
                ret.ContentEncoding = response.ContentEncoding;
                ret.CharacterSet = response.CharacterSet;
                ret.ContentType = response.ContentType;
                ret.Headers = response.Headers;
                if (this.IncludeContent)
                {
                    try
                    {
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            try
                            {
                                ret.ResponseEncoding = Encoding.GetEncoding(response.CharacterSet);
                            }
                            catch (ArgumentException)
                            {
                                ret.ResponseEncoding = null;
                            }

                            if (ret.ResponseEncoding != null)
                            {
                                using (TextReader reader = new StreamReader(responseStream, ret.ResponseEncoding))
                                {
                                    ret.Content = reader.ReadToEnd();
                                }
                            }
                            else
                            {
                                // BASE64 encode a blob
                                using (MemoryStream memBuffer = new MemoryStream())
                                {
                                    int read;
                                    byte[] buff = new byte[512];
                                    do
                                    {
                                        read = responseStream.Read(buff, 0, buff.Length);
                                        memBuffer.Write(buff, 0, read);
                                    }
                                    while (read > 0);

                                    if (memBuffer.Length > 0)
                                    {
                                        ret.ContentIsBase64Encoded = true;
                                        ret.Content = Convert.ToBase64String(
                                            memBuffer.ToArray(),
                                            Base64FormattingOptions.InsertLineBreaks);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        // keep the first exception
                        if (ret.Exception == null)
                        {
                            ret.Exception = ex;
                        }

                        ret.Content = null;
                    }
                }

                ret.Success = true;
            }

            ret.Duration = TimeSpan.FromSeconds((double)(Stopwatch.GetTimestamp() - start) / Stopwatch.Frequency);
            return ret;
        }
Пример #7
0
        /// <summary>
        /// Execute the actual task functionality.
        /// </summary>
        /// <param name="result">The current build result.</param>
        /// <returns>
        /// True if the task was successful, false otherwise.
        /// </returns>
        protected override bool Execute(IIntegrationResult result)
        {
            Log.Debug("HttpStatusTask is executing");

            bool taskTimedOut = false;
            string msg = string.Format("Checking http status of URI: '{0}'", this.RequestSettings.Uri.ToString());
            if (!String.IsNullOrEmpty(Description))
            {
                msg = Description + " (" + msg + ")";
            }

            Log.Debug(msg);
            result.BuildProgressInformation.SignalStartRunTask(msg);
            XmlTaskResult taskResult = new XmlTaskResult();
            List<HttpRequestStatus> resp = new List<HttpRequestStatus>();

            long startTimeStamp = -1;
            bool keepTrying;
            int attempt = 0;
            if (this.HasTimeout)
            {
                startTimeStamp = Stopwatch.GetTimestamp();
            }

            do
            {
                try
                {
                    if (this.HasTimeout)
                    {
                        long taskDurationTicks = Stopwatch.GetTimestamp() - startTimeStamp;
                        long taskDurationMilliSecs = (long)Math.Round(((double)taskDurationTicks / Stopwatch.Frequency) * 1000.0);
                        long msecsLeft = this.Timeout.Millis - taskDurationMilliSecs;

                        if (msecsLeft < 0)
                        {
                            taskTimedOut = true;
                            break;
                        }

                        if (msecsLeft < this.RequestSettings.Timeout.Millis)
                        {
                            this.RequestSettings.Timeout = new Timeout((int)msecsLeft, TimeUnits.MILLIS);
                        }

                        Log.Debug("Task timeout in T-{0:N1} seconds", msecsLeft / 1000.0);
                    }

                    HttpRequestStatus status = this.GetRequestStatus(this.RequestSettings);
                    if (status.Success)
                    {
                        Log.Debug("Checking returned status code ({0}) against success status codes: {1}", ((int)status.StatusCode).ToString(), this.SuccessStatusCodes);
                        for (int i = 0; i < this.successStatusCodes.Length; i++)
                        {
                            if ((int)status.StatusCode == this.successStatusCodes[i])
                            {
                                Log.Debug("Success, exiting...");
                                taskResult.Success = true;
                                break;
                            }
                        }
                    }

                    resp.Add(status);
                }
                catch (Exception ex)
                {
                    throw new BuilderException(this, String.Format("An exception occured in HttpStatusTask while checking status for: '{0}'", this.RequestSettings.Uri), ex);
                }

                // sleeping
                keepTrying = !taskResult.Success && (this.Retries == -1 || (this.Retries - attempt++) > 0);

                if (keepTrying)
                {
                    var delay = this.RetryDelay ?? new Timeout(5000);
                    if (delay.Millis > 0)
                    {
                        delay.Normalize();
                        Log.Debug("Retrying in {0}...", delay.ToString());
                        System.Threading.Thread.Sleep(delay.Millis);
                    }
                    else
                    {
                        Log.Debug("Retrying...");
                    }
                }
            }
            while (keepTrying);

            if (taskTimedOut)
            {
                throw new BuilderException(this, String.Format("HttpStatusTask timed out while checking status for: '{0}'", this.RequestSettings.Uri));
            }

            Log.Debug("Writing output...");
            XmlWriter writer = taskResult.GetWriter();
            writer.WriteStartElement("httpStatus");
            writer.WriteAttributeString("uri", this.RequestSettings.Uri.ToString());
            writer.WriteAttributeString("success", XmlConvert.ToString(taskResult.Success));
            if (!String.IsNullOrEmpty(this.Description))
            {
                writer.WriteElementString("description", this.Description);
            }

            foreach (HttpRequestStatus status in resp)
            {
                status.WriteTo(writer, this.IncludeContent);
            }

            writer.WriteEndElement();
            writer.Close();
            result.AddTaskResult(taskResult);
            Log.Debug("HttpStatusTask finished, return value: {0}", taskResult.Success.ToString());
            return taskResult.Success;
        }
Пример #8
0
		public void DefaultTimeoutIsInMillis()
		{
			Timeout timeout = new Timeout(100);
			Assert.AreEqual(100, timeout.Millis);
			Assert.AreEqual(new Timeout(100, TimeUnits.MILLIS), timeout);
		}
Пример #9
0
		public void CanSpecifyTimeoutInHours()
		{
			Timeout period = new Timeout(1, TimeUnits.HOURS);
			Assert.AreEqual(60*60*1000, period.Millis);
			Assert.AreEqual(new Timeout(60*60*1000, TimeUnits.MILLIS), period);
		}
Пример #10
0
		public void CanSpecifyTimeoutInMinutes()
		{
			Timeout period = new Timeout(1, TimeUnits.MINUTES);
			Assert.AreEqual(60*1000, period.Millis);
			Assert.AreEqual(new Timeout(60*1000, TimeUnits.MILLIS), period);
		}
Пример #11
0
		public void CanSpecifyTimeoutInSeconds()
		{
			Timeout period = new Timeout(1, TimeUnits.SECONDS);
			Assert.AreEqual(1000, period.Millis);
			Assert.AreEqual(new Timeout(1000, TimeUnits.MILLIS), period);
		}