// Reproduces issue #167
 // https://github.com/couchbase/couchbase-lite-android/issues/167
 /// <exception cref="System.Exception"></exception>
 public virtual void TestPushPurgedDoc()
 {
     int numBulkDocRequests = 0;
     HttpPost lastBulkDocsRequest = null;
     IDictionary<string, object> properties = new Dictionary<string, object>();
     properties.Put("testName", "testPurgeDocument");
     Document doc = CreateDocumentWithProperties(database, properties);
     NUnit.Framework.Assert.IsNotNull(doc);
     CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
     mockHttpClient.AddResponderRevDiffsAllMissing();
     mockHttpClient.SetResponseDelayMilliseconds(250);
     mockHttpClient.AddResponderFakeLocalDocumentUpdate404();
     HttpClientFactory mockHttpClientFactory = new _HttpClientFactory_169(mockHttpClient
         );
     Uri remote = GetReplicationURL();
     manager.SetDefaultHttpClientFactory(mockHttpClientFactory);
     Replication pusher = database.CreatePushReplication(remote);
     pusher.SetContinuous(true);
     CountDownLatch replicationCaughtUpSignal = new CountDownLatch(1);
     pusher.AddChangeListener(new _ChangeListener_199(replicationCaughtUpSignal));
     pusher.Start();
     // wait until that doc is pushed
     bool didNotTimeOut = replicationCaughtUpSignal.Await(60, TimeUnit.Seconds);
     NUnit.Framework.Assert.IsTrue(didNotTimeOut);
     // at this point, we should have captured exactly 1 bulk docs request
     numBulkDocRequests = 0;
     foreach (HttpWebRequest capturedRequest in mockHttpClient.GetCapturedRequests())
     {
         if (capturedRequest is HttpPost && ((HttpPost)capturedRequest).GetURI().ToString(
             ).EndsWith("_bulk_docs"))
         {
             lastBulkDocsRequest = (HttpPost)capturedRequest;
             numBulkDocRequests += 1;
         }
     }
     NUnit.Framework.Assert.AreEqual(1, numBulkDocRequests);
     // that bulk docs request should have the "start" key under its _revisions
     IDictionary<string, object> jsonMap = CustomizableMockHttpClient.GetJsonMapFromRequest
         ((HttpPost)lastBulkDocsRequest);
     IList docs = (IList)jsonMap.Get("docs");
     IDictionary<string, object> onlyDoc = (IDictionary)docs[0];
     IDictionary<string, object> revisions = (IDictionary)onlyDoc.Get("_revisions");
     NUnit.Framework.Assert.IsTrue(revisions.ContainsKey("start"));
     // now add a new revision, which will trigger the pusher to try to push it
     properties = new Dictionary<string, object>();
     properties.Put("testName2", "update doc");
     UnsavedRevision unsavedRevision = doc.CreateRevision();
     unsavedRevision.SetUserProperties(properties);
     unsavedRevision.Save();
     // but then immediately purge it
     doc.Purge();
     // wait for a while to give the replicator a chance to push it
     // (it should not actually push anything)
     Sharpen.Thread.Sleep(5 * 1000);
     // we should not have gotten any more _bulk_docs requests, because
     // the replicator should not have pushed anything else.
     // (in the case of the bug, it was trying to push the purged revision)
     numBulkDocRequests = 0;
     foreach (HttpWebRequest capturedRequest_1 in mockHttpClient.GetCapturedRequests())
     {
         if (capturedRequest_1 is HttpPost && ((HttpPost)capturedRequest_1).GetURI().ToString
             ().EndsWith("_bulk_docs"))
         {
             numBulkDocRequests += 1;
         }
     }
     NUnit.Framework.Assert.AreEqual(1, numBulkDocRequests);
     pusher.Stop();
 }
 /// <summary>Regression test for issue couchbase/couchbase-lite-android#174</summary>
 /// <exception cref="System.Exception"></exception>
 public virtual void TestAllLeafRevisionsArePushed()
 {
     CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
     mockHttpClient.AddResponderRevDiffsAllMissing();
     mockHttpClient.SetResponseDelayMilliseconds(250);
     mockHttpClient.AddResponderFakeLocalDocumentUpdate404();
     HttpClientFactory mockHttpClientFactory = new _HttpClientFactory_1165(mockHttpClient
         );
     manager.SetDefaultHttpClientFactory(mockHttpClientFactory);
     Document doc = database.CreateDocument();
     SavedRevision rev1a = doc.CreateRevision().Save();
     SavedRevision rev2a = CreateRevisionWithRandomProps(rev1a, false);
     SavedRevision rev3a = CreateRevisionWithRandomProps(rev2a, false);
     // delete the branch we've been using, then create a new one to replace it
     SavedRevision rev4a = rev3a.DeleteDocument();
     SavedRevision rev2b = CreateRevisionWithRandomProps(rev1a, true);
     NUnit.Framework.Assert.AreEqual(rev2b.GetId(), doc.GetCurrentRevisionId());
     // sync with remote DB -- should push both leaf revisions
     Replication push = database.CreatePushReplication(GetReplicationURL());
     RunReplication(push);
     NUnit.Framework.Assert.IsNull(push.GetLastError());
     // find the _revs_diff captured request and decode into json
     bool foundRevsDiff = false;
     IList<HttpWebRequest> captured = mockHttpClient.GetCapturedRequests();
     foreach (HttpWebRequest httpRequest in captured)
     {
         if (httpRequest is HttpPost)
         {
             HttpPost httpPost = (HttpPost)httpRequest;
             if (httpPost.GetURI().ToString().EndsWith("_revs_diff"))
             {
                 foundRevsDiff = true;
                 IDictionary<string, object> jsonMap = CustomizableMockHttpClient.GetJsonMapFromRequest
                     (httpPost);
                 // assert that it contains the expected revisions
                 IList<string> revisionIds = (IList)jsonMap.Get(doc.GetId());
                 NUnit.Framework.Assert.AreEqual(2, revisionIds.Count);
                 NUnit.Framework.Assert.IsTrue(revisionIds.Contains(rev4a.GetId()));
                 NUnit.Framework.Assert.IsTrue(revisionIds.Contains(rev2b.GetId()));
             }
         }
     }
     NUnit.Framework.Assert.IsTrue(foundRevsDiff);
 }