Example #1
0
        public void CanGetNewsHeadlinesAsync()
        {

            var requestFactory = new TestRequestFactory();

            var throttleScopes = new Dictionary<string, IThrottedRequestQueue>
                {
                    {"data", new ThrottedRequestQueue(TimeSpan.FromSeconds(5), 30, 10)},
                    {"trading", new ThrottedRequestQueue(TimeSpan.FromSeconds(3), 1, 10)}
                };

            requestFactory.CreateTestRequest(NewsHeadlines14);

            var ctx = new CIAPI.Rpc.Client(new Uri(TestConfig.ApiUrl), new RequestCache(), requestFactory, throttleScopes, 3)
            {
                UserName = TestConfig.ApiUsername,
                SessionId = TestConfig.ApiTestSessionId
            };


            ctx.BeginListNewsHeadlines("UK", 14, ar =>
            {
                ListNewsHeadlinesResponseDTO response = ctx.EndListNewsHeadlines(ar);
                Assert.AreEqual(14, response.Headlines.Length);
                EnqueueTestComplete();
            }, null);

        }
Example #2
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");
        }
Example #4
0
        public void CanLogoutAsync()
        {

            var requestFactory = new TestRequestFactory();

            var throttleScopes = new Dictionary<string, IThrottedRequestQueue>
                {
                    {"data", new ThrottedRequestQueue(TimeSpan.FromSeconds(5), 30, 10)},
                    {"trading", new ThrottedRequestQueue(TimeSpan.FromSeconds(3), 1, 10)}
                };

            requestFactory.CreateTestRequest(LoggedOut);

            var ctx = new CIAPI.Rpc.Client(new Uri(TestConfig.ApiUrl), new RequestCache(), requestFactory, throttleScopes, 3);

            ctx.BeginDeleteSession(TestConfig.ApiUsername, TestConfig.ApiTestSessionId, ar =>
            {
                EnqueueCallback(() =>
                {
                    var response = ctx.EndDeleteSession(ar);
                    Assert.IsTrue(response.LoggedOut);
                }
            );

                EnqueueTestComplete();
            }, null);
        }
 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");
 }
Example #6
0
        public void CanThrottle30Per5Seconds()
        {
            var f = new TestRequestFactory();
            var t = new ThrottedRequestQueue(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);
 }
Example #8
0
        public void ReproAbortedRequest()
        {
            TestRequestFactory factory = new TestRequestFactory();
            var requestController = new RequestController(TimeSpan.FromSeconds(0), 2, factory, new ErrorResponseDTOJsonExceptionFactory(), new ThrottedRequestQueue(TimeSpan.FromSeconds(5), 30, 10, "data"), new ThrottedRequestQueue(TimeSpan.FromSeconds(3), 1, 3, "trading"));

            var ctx = new CIAPI.Rpc.Client(new Uri(TestConfig.RpcUrl), requestController);
            ctx.UserName = TestConfig.ApiUsername;
            ctx.Session = TestConfig.ApiTestSessionId;

            factory.CreateTestRequest("{}", TimeSpan.FromMinutes(1));

            ctx.Market.GetMarketInformation("FOO");

        }
Example #9
0
        public static MockClient AuthenticatedClient()
        {
            _requestFactory = new TestRequestFactory();
            var requestController = new RequestController(TimeSpan.FromSeconds(0), 0, _requestFactory, new ErrorResponseDTOJsonExceptionFactory(), new ThrottedRequestQueue(TimeSpan.FromSeconds(5), 30, 10, "data"), new ThrottedRequestQueue(TimeSpan.FromSeconds(3), 1, 3, "trading"));

            var authenticatedClient = new MockClient
            {
                _client =
                    new Client(new Uri("https://mock.server.com/TradingAPI"),
                               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");
        }
Example #11
0
        private RpcClient CreateAuthenticatedApiClient()
        {
            var requestFactory = new TestRequestFactory();

            var throttleScopes = new Dictionary<string, IThrottedRequestQueue>
		                {
		                    {"data", new ThrottedRequestQueue(TimeSpan.FromSeconds(5), 30, 10)},
		                    {"trading", new ThrottedRequestQueue(TimeSpan.FromSeconds(3), 1, 10)}
		                };

            requestFactory.CreateTestRequest(LoggedIn);

            _testStreamingConnectionFactory = new TestStreamingConnectionFactory();

            var ctx = new RpcClient(new Uri(TestConfig.RpcUrl), new Uri(TestConfig.StreamingUrl), new RequestCache(), requestFactory, _testStreamingConnectionFactory, throttleScopes, 3);

            ctx.LogIn(TestConfig.ApiUsername, TestConfig.ApiPassword);
            return ctx;
        }
Example #12
0
        public void CanLoginAsync()
        {
            var requestFactory = new TestRequestFactory();

            var throttleScopes = new Dictionary<string, IThrottedRequestQueue>
                {
                    {"data", new ThrottedRequestQueue(TimeSpan.FromSeconds(5), 30, 10)},
                    {"trading", new ThrottedRequestQueue(TimeSpan.FromSeconds(3), 1, 10)}
                };

            var ctx = new CIAPI.Rpc.Client(new Uri(TestConfig.ApiUrl), new RequestCache(), requestFactory, throttleScopes, 3);

            requestFactory.CreateTestRequest(LoggedIn);

            ctx.BeginCreateSession( TestConfig.ApiUsername, TestConfig.ApiPassword, ar =>
            {
                var response = ctx.EndCreateSession(ar);
                Assert.IsNotNullOrEmpty(response.Session);
                EnqueueTestComplete();
            }, null);

        }
Example #13
0
        private CIAPI.Rpc.Client BuildClientAndSetupResponse(string expectedJson)
        {
            var requestFactory = new TestRequestFactory();
           
            requestFactory.CreateTestRequest(expectedJson);

            var ctx = new CIAPI.Rpc.Client(new Uri(TestConfig.RpcUrl), new RequestCache(), requestFactory, _standardThrottleScopes, 3);
            return ctx;
        } 
Example #14
0
        public void NonRetryableExceptionFailsInsteadOfRetrying()
        {
            Console.WriteLine("NonRetryableExceptionFailsInsteadOfRetrying");

            var requestFactory = new TestRequestFactory();

            var ctx = new Client(new Uri(TestConfig.RpcUrl), new RequestCache(), requestFactory, _standardThrottleScopes, 3);
            requestFactory.CreateTestRequest("", TimeSpan.FromMilliseconds(300), null, null, new WebException("(401) Unauthorized"));

            Assert.Throws<ApiException>(() => ctx.LogIn("foo", "bar"));
        }
Example #15
0
        public void ShouldThrowExceptionIfRequestTimesOut()
        {
            var requestFactory = new TestRequestFactory
                                     {
                                         RequestTimeout = TimeSpan.FromSeconds(1)
                                     };

            var ctx = new Client(new Uri(TestConfig.RpcUrl), new RequestCache(), requestFactory, _standardThrottleScopes, 3);
            requestFactory.CreateTestRequest(LoggedIn, TimeSpan.FromSeconds(300));

            Assert.Throws<ApiException>(() => ctx.LogIn("foo", "bar"));
        }
Example #16
0
        public void SpecificRequestExceptionsAreRetriedTheCorrectNumberOfTimes()
        {
            Console.WriteLine("SpecificRequestExceptionsAreRetriedTheCorrectNumberOfTimes");

            using (var gate = new ManualResetEvent(false))
            {
                var requestFactory = new TestRequestFactory();

                const int EXPECTED_RETRY_COUNT = 2;
                
                var ctx = new CIAPI.Rpc.Client(new Uri(TestConfig.RpcUrl), new RequestCache(), requestFactory, _standardThrottleScopes, EXPECTED_RETRY_COUNT)
                {
                    UserName = TestConfig.ApiUsername,
                    SessionId = TestConfig.ApiTestSessionId
                };

                requestFactory.CreateTestRequest(NewsHeadlines14, TimeSpan.FromMilliseconds(300), null, null, new WebException("(500) internal server error"));

                Exception exception = null;
                
                ctx.BeginListNewsHeadlines("UK", 14, ar =>
                    {
                        try
                        {
                            ctx.EndListNewsHeadlines(ar);
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                        }
                        finally
                        {
                            gate.Set();
                        }

                    }, null);
                gate.WaitOne(TimeSpan.FromSeconds(3));
                Assert.IsNotNull(exception);
                Assert.AreEqual(string.Format("(500) internal server error\r\nretried {0} times", EXPECTED_RETRY_COUNT),exception.Message);
                
            }
        }
Example #17
0
        private CIAPI.Rpc.Client BuildClientAndSetupResponse(string expectedJson)
        {

            TestRequestFactory factory = new TestRequestFactory();
            var requestController = new RequestController(TimeSpan.FromSeconds(0), 2, factory, new ErrorResponseDTOJsonExceptionFactory(), new ThrottedRequestQueue(TimeSpan.FromSeconds(5), 30, 10, "data"), new ThrottedRequestQueue(TimeSpan.FromSeconds(3), 1, 3, "trading"));

            var ctx = new CIAPI.Rpc.Client(new Uri(TestConfig.RpcUrl), requestController);
            factory.CreateTestRequest(expectedJson);
            return ctx;
        }