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 = "";
            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");
 }
Ejemplo n.º 3
0
        public void CanThrottle30Per5Seconds()
        {
            var f = new TestRequestFactory();
            var t = new ThrottledRequestQueue(TimeSpan.FromSeconds(5), 30, 10, "");
            bool quit = false;
            var outerGate = new AutoResetEvent(false);
            new Thread(() =>
            {



                while (!quit)
                {
                    new ManualResetEvent(false).WaitOne(100);
                    t.ProcessQueue(null);
                }

                outerGate.Set();

            }).Start();

            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();
            quit = true;
            outerGate.WaitOne();
            sw.Stop();

            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 10000, "65 requests - expect > 10 seconds delay for 65 requests");
        }
 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);
 }
        private Client BuildClientAndSetupResponse(string expectedJson)
        {

            TestRequestFactory factory = new TestRequestFactory();
            var requestController = new RequestController(TimeSpan.FromSeconds(0), 2, factory, new DummyJsonExceptionFactory(),  new ThrottledRequestQueue(TimeSpan.FromSeconds(5), 30, 10, "default"));

            var ctx = new Client(new Uri("http://foo.bar"), requestController);
            factory.CreateTestRequest(expectedJson);
            return ctx;
        }
Ejemplo n.º 6
0
        public void CanThrottle1Per3Seconds()
        {
            var f = new TestRequestFactory();
            var t = new ThrottledRequestQueue(TimeSpan.FromSeconds(3), 1, 10, "");
            var handles = new List<WaitHandle>();
            bool quit = false;
            var outerGate = new AutoResetEvent(false);
            new Thread(()=>
                           {
                               
                               

                               while(!quit)
                               {
                                   new ManualResetEvent(false).WaitOne(100);
                                   t.ProcessQueue(null);                                   
                               }

                               outerGate.Set();

                           }).Start();

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

            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());
            quit = true;
            outerGate.WaitOne();
            sw.Stop();

            Assert.GreaterOrEqual(sw.ElapsedMilliseconds, 5800, "3 requests, 2 throttled - expect almost 6 seconds delay for 3 requests");
        }
Ejemplo n.º 7
0
        public static MockClient AuthenticatedClient()
        {
            _requestFactory = new TestRequestFactory();
            var requestController = new RequestController(TimeSpan.FromSeconds(0), 0, _requestFactory, new ErrorResponseDTOJsonExceptionFactory(), new ThrottledRequestQueue(TimeSpan.FromSeconds(5), 30, 10, "data"), new ThrottledRequestQueue(TimeSpan.FromSeconds(3), 1, 3, "trading"));

            var authenticatedClient = new MockClient
            {
                _client =
                    new Client(new Uri("https://mock.server.com/TradingAPI"), "mockAPIKEY",
                               requestController)
                    {
                        UserName = "******",
                        Session = "MOCKSESSION"
                    }
            };
            return authenticatedClient;
        }
        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");
        }