/// <exception cref="System.Exception"></exception>
 public virtual void TestOnlineOfflinePusher()
 {
     Uri remote = GetReplicationURL();
     // mock sync gateway
     CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
     mockHttpClient.AddResponderFakeLocalDocumentUpdate404();
     mockHttpClient.AddResponderRevDiffsSmartResponder();
     HttpClientFactory mockHttpClientFactory = MockFactoryFactory(mockHttpClient);
     manager.SetDefaultHttpClientFactory(mockHttpClientFactory);
     // create a replication observer
     CountDownLatch replicationDoneSignal = new CountDownLatch(1);
     LiteTestCase.ReplicationFinishedObserver replicationFinishedObserver = new LiteTestCase.ReplicationFinishedObserver
         (replicationDoneSignal);
     // create a push replication
     Replication pusher = database.CreatePushReplication(remote);
     Log.D(Database.Tag, "created pusher: " + pusher);
     pusher.AddChangeListener(replicationFinishedObserver);
     pusher.SetContinuous(true);
     pusher.Start();
     for (int i = 0; i < 5; i++)
     {
         Log.D(Database.Tag, "testOnlineOfflinePusher, i: " + i);
         string docFieldName = "testOnlineOfflinePusher" + i;
         // put the replication offline
         PutReplicationOffline(pusher);
         // add a response listener to wait for a bulk_docs request from the pusher
         CountDownLatch gotBulkDocsRequest = new CountDownLatch(1);
         CustomizableMockHttpClient.ResponseListener bulkDocsListener = new _ResponseListener_1334
             (docFieldName, gotBulkDocsRequest);
         mockHttpClient.AddResponseListener(bulkDocsListener);
         // add a document
         string docFieldVal = "foo" + i;
         IDictionary<string, object> properties = new Dictionary<string, object>();
         properties.Put(docFieldName, docFieldVal);
         CreateDocumentWithProperties(database, properties);
         // put the replication online, which should trigger it to send outgoing bulk_docs request
         PutReplicationOnline(pusher);
         // wait until we get a bulk docs request
         Log.D(Database.Tag, "waiting for bulk docs request with " + docFieldName);
         bool succeeded = gotBulkDocsRequest.Await(90, TimeUnit.Seconds);
         NUnit.Framework.Assert.IsTrue(succeeded);
         Log.D(Database.Tag, "got bulk docs request with " + docFieldName);
         mockHttpClient.RemoveResponseListener(bulkDocsListener);
         mockHttpClient.ClearCapturedRequests();
     }
     Log.D(Database.Tag, "calling pusher.stop()");
     pusher.Stop();
     Log.D(Database.Tag, "called pusher.stop()");
     // wait for replication to finish
     Log.D(Database.Tag, "waiting for replicationDoneSignal");
     bool didNotTimeOut = replicationDoneSignal.Await(90, TimeUnit.Seconds);
     Log.D(Database.Tag, "done waiting for replicationDoneSignal.  didNotTimeOut: " + 
         didNotTimeOut);
     NUnit.Framework.Assert.IsTrue(didNotTimeOut);
     NUnit.Framework.Assert.IsFalse(pusher.IsRunning());
 }
		/// <exception cref="System.Exception"></exception>
		public virtual void TestOnlineOfflinePusher()
		{
			Uri remote = GetReplicationURL();
			// mock sync gateway
			CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient();
			mockHttpClient.AddResponderFakeLocalDocumentUpdate404();
			mockHttpClient.AddResponderRevDiffsSmartResponder();
			HttpClientFactory mockHttpClientFactory = MockFactoryFactory(mockHttpClient);
			manager.SetDefaultHttpClientFactory(mockHttpClientFactory);
			// create a push replication
			Replication pusher = database.CreatePushReplication(remote);
			Log.D(Database.Tag, "created pusher: " + pusher);
			pusher.SetContinuous(true);
			pusher.Start();
			for (int i = 0; i < 5; i++)
			{
				Log.D(Database.Tag, "testOnlineOfflinePusher, i: " + i);
				// put the replication offline
				PutReplicationOffline(pusher);
				// add a document
				string docFieldName = "testOnlineOfflinePusher" + i;
				string docFieldVal = "foo" + i;
				IDictionary<string, object> properties = new Dictionary<string, object>();
				properties.Put(docFieldName, docFieldVal);
				CreateDocumentWithProperties(database, properties);
				// add a response listener to wait for a bulk_docs request from the pusher
				CountDownLatch gotBulkDocsRequest = new CountDownLatch(1);
				CustomizableMockHttpClient.ResponseListener bulkDocsListener = new _ResponseListener_1145
					(gotBulkDocsRequest);
				mockHttpClient.AddResponseListener(bulkDocsListener);
				// put the replication online, which should trigger it to send outgoing bulk_docs request
				PutReplicationOnline(pusher);
				// wait until we get a bulk docs request
				Log.D(Database.Tag, "waiting for bulk docs request");
				bool succeeded = gotBulkDocsRequest.Await(120, TimeUnit.Seconds);
				NUnit.Framework.Assert.IsTrue(succeeded);
				Log.D(Database.Tag, "got bulk docs request, verifying captured requests");
				mockHttpClient.RemoveResponseListener(bulkDocsListener);
				// workaround bug https://github.com/couchbase/couchbase-lite-android/issues/219
				Sharpen.Thread.Sleep(2000);
				// make sure that doc was pushed out in a bulk docs request
				bool foundExpectedDoc = false;
				IList<HttpWebRequest> capturedRequests = mockHttpClient.GetCapturedRequests();
				foreach (HttpWebRequest capturedRequest in capturedRequests)
				{
					Log.D(Database.Tag, "captured request: " + capturedRequest);
					if (capturedRequest is HttpPost)
					{
						HttpPost capturedPostRequest = (HttpPost)capturedRequest;
						Log.D(Database.Tag, "capturedPostRequest: " + capturedPostRequest.GetURI().GetPath
							());
						if (capturedPostRequest.GetURI().GetPath().EndsWith("_bulk_docs"))
						{
							ArrayList docs = CustomizableMockHttpClient.ExtractDocsFromBulkDocsPost(capturedRequest
								);
							NUnit.Framework.Assert.AreEqual(1, docs.Count);
							IDictionary<string, object> doc = (IDictionary)docs[0];
							Log.D(Database.Tag, "doc from captured request: " + doc);
							Log.D(Database.Tag, "docFieldName: " + docFieldName);
							Log.D(Database.Tag, "expected docFieldVal: " + docFieldVal);
							Log.D(Database.Tag, "actual doc.get(docFieldName): " + doc.Get(docFieldName));
							NUnit.Framework.Assert.AreEqual(docFieldVal, doc.Get(docFieldName));
							foundExpectedDoc = true;
						}
					}
				}
				NUnit.Framework.Assert.IsTrue(foundExpectedDoc);
				mockHttpClient.ClearCapturedRequests();
			}
		}