Example #1
0
        public void CanThrottle1Per3Seconds()
        {
            var f = new TestRequestFactory();
            var t = new ThrottedRequestQueue(TimeSpan.FromSeconds(3), 1, 10);

            var sw = new Stopwatch();
            sw.Start();
            var handles = new List<WaitHandle>();

            for (int i = 0; i < 3; i++)
            {
                var gate = new AutoResetEvent(false);
                handles.Add(gate);

                f.CreateTestRequest("foo" + i);
                const string url = "http://tmpuri.org";
                var r = f.Create(url);
                int i1 = i;
                t.Enqueue(url, r, (ar, ignored) =>
                    {
                        string expected = "foo" + i1;
                        var response = r.EndGetResponse(ar);
                        var actual = new StreamReader(response.GetResponseStream()).ReadToEnd();
                        Trace.WriteLine(actual);
                        Assert.AreEqual(expected, actual);
                        gate.Set();
                    });

            }
            WaitHandle.WaitAll(handles.ToArray());
            sw.Stop();

            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 5800, "3 requests, 2 throttled - expect almost 6 seconds delay for 3 requests");
        }
        public void CanInduceLatencyInAsyncGetResponse()
        {
            const int desiredLatencyMs = 200;

            var f = new TestRequestFactory();
            const string expected = "foo";
            f.CreateTestRequest(expected, TimeSpan.FromMilliseconds(desiredLatencyMs), null, null, null);
            WebRequest r = f.Create("http://testuri.org");
            var sw = new Stopwatch();
            sw.Start();
            string actual = "";
            using (var gate = new ManualResetEvent(false))
            {
                r.BeginGetResponse(ar =>
                                       {
                                           WebResponse response = r.EndGetResponse(ar);
                                           actual = new StreamReader(response.GetResponseStream()).ReadToEnd();
                                           gate.Set();
                                       }, null);
                gate.WaitOne(TimeSpan.FromSeconds(2));
            }
           
            sw.Stop();
            Assert.AreEqual(expected, actual);
            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, desiredLatencyMs - 50, "incorrect latency");
            // TODO: Sometimes this seems to take *much* longer on the build server
            // Assert.LessOrEqual(sw.ElapsedMilliseconds, desiredLatencyMs + 50, "incorrect latency");
        }
 public void CanThrowExceptionOnRequestStream()
 {
     var f = new TestRequestFactory();
     f.CreateTestRequest("", TimeSpan.FromMilliseconds(0), new Exception("request stream exception"), null, null);
     WebRequest r = f.Create("http://testuri.org");
     r.GetRequestStream();
     Assert.Fail("Expected exception");
 }
 public void CanCreateTestRequest()
 {
     var f = new TestRequestFactory();
     const string expected = "foo";
     f.CreateTestRequest(expected);
     WebRequest r = f.Create("http://testuri.org");
     string actual = new StreamReader(r.GetResponse().GetResponseStream()).ReadToEnd();
     Assert.AreEqual(expected, actual);
 }
        public void CanInduceLatencyInSyncGetResponse()
        {
            const int desiredLatencyMs = 200;

            var f = new TestRequestFactory();
            const string expected = "foo";
            f.CreateTestRequest(expected, TimeSpan.FromMilliseconds(desiredLatencyMs), null, null, null);
            WebRequest webRequest = f.Create("http://testuri.org");
            var sw = new Stopwatch();
            sw.Start();
            var actual = new StreamReader(webRequest.GetResponse().GetResponseStream()).ReadToEnd();
            sw.Stop();
            
            Assert.AreEqual(expected, actual);
            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, desiredLatencyMs - 50, "incorrect latency");
            // TODO: Sometimes this seems to take *much* longer on the build server
            // Assert.LessOrEqual(sw.ElapsedMilliseconds, desiredLatencyMs + 50, "incorrect latency");
        }
Example #6
0
        public void CanThrottle30Per5Seconds()
        {
            var f = new TestRequestFactory();
            var t = new ThrottedRequestQueue(TimeSpan.FromSeconds(5), 30, 10);

            var sw = new Stopwatch();
            sw.Start();

            var gate = new AutoResetEvent(false);

            for (int i = 0; i < 65; i++)
            {



                f.CreateTestRequest("foo" + i);
                const string url = "http://tmpuri.org";
                var r = f.Create(url);
                int i1 = i;
                          

                t.Enqueue(url, r, (ar, ignored) =>
                    {
                        string expected = "foo" + i1;
                        var response = r.EndGetResponse(ar);
                        var actual = new StreamReader(response.GetResponseStream()).ReadToEnd();
                        Assert.AreEqual(expected, actual);
                        if (i1 == 64)
                        {
                            gate.Set();
                        }
                    });


            }
            gate.WaitOne();

            sw.Stop();

            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 10000, "65 requests - expect > 10 seconds delay for 65 requests");
        }