public HttpClient GetHttpClient()
            {
                CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();

                mockHttpClient.AddResponderThrowExceptionAllRequests();
                return(mockHttpClient);
            }
        /// <exception cref="System.Exception"></exception>
        public virtual void TestHeaders()
        {
            CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();

            mockHttpClient.AddResponderThrowExceptionAllRequests();
            HttpClientFactory mockHttpClientFactory = new _HttpClientFactory_741(mockHttpClient
                                                                                 );
            Uri remote = GetReplicationURL();

            manager.SetDefaultHttpClientFactory(mockHttpClientFactory);
            Replication puller = database.CreatePullReplication(remote);
            IDictionary <string, object> headers = new Dictionary <string, object>();

            headers.Put("foo", "bar");
            puller.SetHeaders(headers);
            puller.Start();
            Sharpen.Thread.Sleep(2000);
            puller.Stop();
            bool foundFooHeader             = false;
            IList <HttpWebRequest> requests = mockHttpClient.GetCapturedRequests();

            foreach (HttpWebRequest request in requests)
            {
                Header[] requestHeaders = request.GetHeaders("foo");
                foreach (Header requestHeader in requestHeaders)
                {
                    foundFooHeader = true;
                    NUnit.Framework.Assert.AreEqual("bar", requestHeader.GetValue());
                }
            }
            NUnit.Framework.Assert.IsTrue(foundFooHeader);
            manager.SetDefaultHttpClientFactory(null);
        }
            public HttpClient GetHttpClient()
            {
                CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();

                mockHttpClient.SetResponder("_changes", new _Responder_136());
                return(mockHttpClient);
            }
                /// <exception cref="System.IO.IOException"></exception>
                public HttpResponse Execute(HttpRequestMessage httpUriRequest)
                {
                    string json = "{\"results\":[\n" + "{\"seq\":\"*:1\",\"id\":\"doc1-138\",\"changes\":[{\"rev\":\"1-82d\"}]}],\n"
                                  + "\"last_seq\":\"*:50\"}";

                    return(CustomizableMockHttpClient.GenerateHttpResponseObject(json));
                }
Example #5
0
        /// <exception cref="System.Exception"></exception>
        public virtual void TestChangeTrackerBackoffInvalidJson()
        {
            CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();

            mockHttpClient.AddResponderReturnInvalidChangesFeedJson();
            TestChangeTrackerBackoff(mockHttpClient);
        }
Example #6
0
        /// <exception cref="System.Exception"></exception>
        public virtual void TestChangeTrackerBackoffExceptions()
        {
            CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();

            mockHttpClient.AddResponderThrowExceptionAllRequests();
            TestChangeTrackerBackoff(mockHttpClient);
        }
            public HttpClient GetHttpClient()
            {
                CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
                int statusCode = 500;

                mockHttpClient.AddResponderFailAllRequests(statusCode);
                return(mockHttpClient);
            }
Example #8
0
 public HttpClient GetHttpClient()
 {
     if (useMockReplicator)
     {
         CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
         mockHttpClient.SetResponder("_changes", new _Responder_62());
         return(mockHttpClient);
     }
     else
     {
         return(new DefaultHttpClient());
     }
 }
Example #9
0
        /// <exception cref="System.Exception"></exception>
        public virtual void TestChangeTrackerInvalidJson()
        {
            Uri testURL = GetReplicationURL();
            CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();

            mockHttpClient.AddResponderThrowExceptionAllRequests();
            IChangeTrackerClient client        = new _ChangeTrackerClient_290(mockHttpClient);
            ChangeTracker        changeTracker = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode
                                                                   .LongPoll, 0, client);
            var task = Task.Factory.StartNew(() =>
            {
                changeTracker.Start();
            });

            task.Start();
            try
            {
                // expected behavior:
                // when:
                //    mockHttpClient throws IOExceptions -> it should start high and then back off and numTimesExecute should be low
                for (int i = 0; i < 30; i++)
                {
                    int numTimesExectutedAfter10seconds = 0;
                    try
                    {
                        Sharpen.Thread.Sleep(1000);
                        // take a snapshot of num times the http client was called after 10 seconds
                        if (i == 10)
                        {
                            numTimesExectutedAfter10seconds = mockHttpClient.GetCapturedRequests().Count;
                        }
                        // take another snapshot after 20 seconds have passed
                        if (i == 20)
                        {
                            // by now it should have backed off, so the delta between 10s and 20s should be small
                            int delta = mockHttpClient.GetCapturedRequests().Count - numTimesExectutedAfter10seconds;
                            NUnit.Framework.Assert.IsTrue(delta < 25);
                        }
                    }
                    catch (Exception e)
                    {
                        Sharpen.Runtime.PrintStackTrace(e);
                    }
                }
            }
            finally
            {
                changeTracker.Stop();
            }
        }
Example #10
0
            public HttpClient GetHttpClient()
            {
                CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();

                CustomizableMockHttpClient.Responder         sentinal   = this.DefaultChangesResponder();
                Queue <CustomizableMockHttpClient.Responder> responders = new List <CustomizableMockHttpClient.Responder
                                                                                    >();

                responders.AddItem(this.DefaultChangesResponder());
                responders.AddItem(CustomizableMockHttpClient.TransientErrorResponder(errorCode,
                                                                                      statusMessage));
                ResponderChain responderChain = new ResponderChain(responders, sentinal);

                mockHttpClient.SetResponder("_changes", responderChain);
                return(mockHttpClient);
            }
Example #11
0
        /// <exception cref="System.Exception"></exception>
        private void TestChangeTrackerBackoff(CustomizableMockHttpClient mockHttpClient)
        {
            Uri                 testURL = GetReplicationURL();
            CountDownLatch      changeTrackerFinishedSignal = new CountDownLatch(1);
            ChangeTrackerClient client = new _ChangeTrackerClient_263(changeTrackerFinishedSignal
                                                                      , mockHttpClient);
            ChangeTracker changeTracker = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode
                                                            .LongPoll, false, 0, client);

            changeTracker.SetUsePOST(IsTestingAgainstSyncGateway());
            changeTracker.Start();
            // sleep for a few seconds
            Sharpen.Thread.Sleep(5 * 1000);
            // make sure we got less than 10 requests in those 10 seconds (if it was hammering, we'd get a lot more)
            NUnit.Framework.Assert.IsTrue(mockHttpClient.GetCapturedRequests().Count < 25);
            NUnit.Framework.Assert.IsTrue(changeTracker.backoff.GetNumAttempts() > 0);
            mockHttpClient.ClearResponders();
            mockHttpClient.AddResponderReturnEmptyChangesFeed();
            // at this point, the change tracker backoff should cause it to sleep for about 3 seconds
            // and so lets wait 3 seconds until it wakes up and starts getting valid responses
            Sharpen.Thread.Sleep(3 * 1000);
            // now find the delta in requests received in a 2s period
            int before = mockHttpClient.GetCapturedRequests().Count;

            Sharpen.Thread.Sleep(2 * 1000);
            int after = mockHttpClient.GetCapturedRequests().Count;

            // assert that the delta is high, because at this point the change tracker should
            // be hammering away
            NUnit.Framework.Assert.IsTrue((after - before) > 25);
            // the backoff numAttempts should have been reset to 0
            NUnit.Framework.Assert.IsTrue(changeTracker.backoff.GetNumAttempts() == 0);
            changeTracker.Stop();
            try
            {
                bool success = changeTrackerFinishedSignal.Await(300, TimeUnit.Seconds);
                NUnit.Framework.Assert.IsTrue(success);
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }
        }
 public _HttpClientFactory_741(CustomizableMockHttpClient mockHttpClient)
 {
     this.mockHttpClient = mockHttpClient;
 }
 public _ChangeTrackerClient_290(CustomizableMockHttpClient mockHttpClient)
 {
     this.mockHttpClient = mockHttpClient;
 }
Example #14
0
 public _ChangeTrackerClient_263(CountDownLatch changeTrackerFinishedSignal, CustomizableMockHttpClient
                                 mockHttpClient)
 {
     this.changeTrackerFinishedSignal = changeTrackerFinishedSignal;
     this.mockHttpClient = mockHttpClient;
 }