/// <exception cref="System.Exception"></exception>
 public virtual void ChangeTrackerTestWithMode(ChangeTracker.ChangeTrackerMode mode
     , bool useMockReplicator)
 {
     CountDownLatch changeTrackerFinishedSignal = new CountDownLatch(1);
     CountDownLatch changeReceivedSignal = new CountDownLatch(1);
     Uri testURL = GetReplicationURL();
     ChangeTrackerClient client = new _ChangeTrackerClient_42(changeTrackerFinishedSignal
         , useMockReplicator, changeReceivedSignal);
     ChangeTracker changeTracker = new ChangeTracker(testURL, mode, false, 0, client);
     changeTracker.SetUsePOST(IsTestingAgainstSyncGateway());
     changeTracker.Start();
     try
     {
         bool success = changeReceivedSignal.Await(300, TimeUnit.Seconds);
         NUnit.Framework.Assert.IsTrue(success);
     }
     catch (Exception e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
     }
     changeTracker.Stop();
     try
     {
         bool success = changeTrackerFinishedSignal.Await(300, TimeUnit.Seconds);
         NUnit.Framework.Assert.IsTrue(success);
     }
     catch (Exception e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
     }
 }
 /// <exception cref="System.Exception"></exception>
 public virtual void TestChangeTracker()
 {
     CountDownLatch changeTrackerFinishedSignal = new CountDownLatch(1);
     Uri testURL = GetReplicationURL();
     ChangeTrackerClient client = new _ChangeTrackerClient_31(changeTrackerFinishedSignal
         );
     ChangeTracker changeTracker = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode
         .OneShot, 0, client);
     changeTracker.Start();
     try
     {
         bool success = changeTrackerFinishedSignal.Await(300, TimeUnit.Seconds);
         NUnit.Framework.Assert.IsTrue(success);
     }
     catch (Exception e)
     {
         Sharpen.Runtime.PrintStackTrace(e);
     }
 }
        public void TestChangeListenerNotificationBatching()
        {
            const int numDocs = 50;
            var atomicInteger = 0;
            var doneSignal = new CountDownLatch(1);

            database.Changed += (sender, e) => Interlocked.Increment (ref atomicInteger);

            database.RunInTransaction(() =>
            {
                CreateDocuments(database, numDocs);
                doneSignal.CountDown();
                return true;
            });

            var success = doneSignal.Await(TimeSpan.FromSeconds(30));
            Assert.IsTrue(success);
            Assert.AreEqual(1, atomicInteger);
        }
        /// <exception cref="System.IO.IOException"></exception>
        private void AddDocWithId(string docId, string attachmentName)
        {
            string docJson;
            if (attachmentName != null)
            {
                // add attachment to document
                var attachmentStream = (InputStream)GetAsset(attachmentName);
                var baos = new MemoryStream();
                attachmentStream.Wrapped.CopyTo(baos);
                var attachmentBase64 = Convert.ToBase64String(baos.ToArray());
                docJson = String.Format("{{\"foo\":1,\"bar\":false, \"_attachments\": {{ \"i_use_couchdb.png\": {{ \"content_type\": \"image/png\", \"data\": \"{0}\" }} }} }}", attachmentBase64);
            }
            else
            {
                docJson = @"{""foo"":1,""bar"":false}";
            }

            // push a document to server
            var replicationUrlTrailingDoc1 = new Uri(string.Format("{0}/{1}", GetReplicationURL(), docId));
            var pathToDoc1 = new Uri(replicationUrlTrailingDoc1, docId);
            Log.D(Tag, "Send http request to " + pathToDoc1);
            CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
            Task.Factory.StartNew(() =>
            {
                var httpclient = new HttpClient(); //CouchbaseLiteHttpClientFactory.Instance.GetHttpClient();
                HttpResponseMessage response;

                try
                {
                    var request = new HttpRequestMessage();
                    request.Headers.Add("Accept", "*/*");

                    var postTask = httpclient.PutAsync(pathToDoc1.AbsoluteUri, new StringContent(docJson, Encoding.UTF8, "application/json"));
                    response = postTask.Result;
                    var statusLine = response.StatusCode;
                    Log.D(ReplicationTest.Tag, "Got response: " + statusLine);
                    Assert.IsTrue(statusLine == HttpStatusCode.Created);
                }
                catch (ProtocolViolationException e)
                {
                    Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
                }
                catch (IOException e)
                {
                    Assert.IsNull(e, "Got IOException: " + e.Message);
                }

                httpRequestDoneSignal.CountDown();
            });

            Log.D(Tag, "Waiting for http request to finish");
            try
            {
                Assert.IsTrue(httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10)));
                Log.D(Tag, "http request finished");
            }
            catch (Exception e)
            {
                Sharpen.Runtime.PrintStackTrace(e);
            }

            WorkaroundSyncGatewayRaceCondition();
        }
        public void TestPushReplicationCanMissDocs()
        {
            Assert.AreEqual(0, database.LastSequenceNumber);

            var properties1 = new Dictionary<string, object>();
            properties1["doc1"] = "testPushReplicationCanMissDocs";
            var doc1 = CreateDocumentWithProperties(database, properties1);

            var properties2 = new Dictionary<string, object>();
            properties2["doc2"] = "testPushReplicationCanMissDocs";
            var doc2 = CreateDocumentWithProperties(database, properties2);

            var doc2UnsavedRev = doc2.CreateRevision();
            var attachmentStream = GetAsset("attachment.png");
            doc2UnsavedRev.SetAttachment("attachment.png", "image/png", attachmentStream);
            var doc2Rev = doc2UnsavedRev.Save();
            Assert.IsNotNull(doc2Rev);

            var httpClientFactory = new MockHttpClientFactory();
            manager.DefaultHttpClientFactory = httpClientFactory;

            var httpHandler = httpClientFactory.HttpHandler; 
            httpHandler.AddResponderFakeLocalDocumentUpdate404();

            var json = "{\"error\":\"not_found\",\"reason\":\"missing\"}";
            MockHttpRequestHandler.HttpResponseDelegate bulkDocsResponder = (request) =>
            {
                return MockHttpRequestHandler.GenerateHttpResponseMessage(HttpStatusCode.NotFound, null, json);
            };
            httpHandler.SetResponder("_bulk_docs", bulkDocsResponder);

            MockHttpRequestHandler.HttpResponseDelegate doc2Responder = (request) =>
            {
                var responseObject = new Dictionary<string, object>();
                responseObject["id"] = doc2.Id;
                responseObject["ok"] = true;
                responseObject["rev"] = doc2.CurrentRevisionId;
                return  MockHttpRequestHandler.GenerateHttpResponseMessage(responseObject);
            };
            httpHandler.SetResponder(doc2.Id, bulkDocsResponder);

            var replicationDoneSignal = new CountDownLatch(1);
            var observer = new ReplicationObserver(replicationDoneSignal);
            var pusher = database.CreatePushReplication(GetReplicationURL());
            pusher.Changed += observer.Changed;
            pusher.Start();

            var success = replicationDoneSignal.Await(TimeSpan.FromSeconds(5));
            Assert.IsTrue(success);

            Assert.IsNotNull(pusher.LastError);

            System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(500));

            var localLastSequence = database.LastSequenceWithCheckpointId(pusher.RemoteCheckpointDocID());

            Log.D(Tag, "dtabase.lastSequenceWithCheckpointId(): " + localLastSequence);
            Log.D(Tag, "doc2.getCUrrentRevision().getSequence(): " + doc2.CurrentRevision.Sequence);

            // Since doc1 failed, the database should _not_ have had its lastSequence bumped to doc2's sequence number.
            // If it did, it's bug: github.com/couchbase/couchbase-lite-java-core/issues/95
            Assert.IsFalse(doc2.CurrentRevision.Sequence.ToString().Equals(localLastSequence));
            Assert.IsNull(localLastSequence);
            Assert.IsTrue(doc2.CurrentRevision.Sequence > 0);
        }
 public ChangeTrackerTestClient(CountDownLatch stoppedSignal, CountDownLatch changedSignal)
 {
     this.stoppedSignal = stoppedSignal;
     this.changedSignal = changedSignal;
     HttpClientFactory = new MockHttpClientFactory();
 }
        private void TestChangeTrackerBackoff(MockHttpClientFactory httpClientFactory)
        {
            var changeTrackerFinishedSignal = new CountDownLatch(1);
            var client = new ChangeTrackerTestClient(changeTrackerFinishedSignal, null);
            client.HttpClientFactory = httpClientFactory;

            var testUrl = GetReplicationURL();
            var scheduler = new SingleTaskThreadpoolScheduler();
            var changeTracker = new ChangeTracker(testUrl, ChangeTrackerMode.LongPoll, 0, false, client, new TaskFactory(scheduler));

            changeTracker.UsePost = IsSyncGateway(testUrl);
            changeTracker.Start();

            // sleep for a few seconds
            Thread.Sleep(15 * 1000);

            // make sure we got less than 10 requests in those 10 seconds (if it was hammering, we'd get a lot more)
            var handler = client.HttpRequestHandler;
            Assert.IsTrue(handler.CapturedRequests.Count < 25);
            Assert.IsTrue(changeTracker.backoff.NumAttempts > 0, "Observed attempts: {0}".Fmt(changeTracker.backoff.NumAttempts));

            handler.ClearResponders();
            handler.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
            Thread.Sleep(3 * 1000);

            // now find the delta in requests received in a 2s period
            int before = handler.CapturedRequests.Count;
            Thread.Sleep(2 * 1000);
            int after = handler.CapturedRequests.Count;

            // assert that the delta is high, because at this point the change tracker should
            // be hammering away
            Assert.IsTrue((after - before) > 25);

            // the backoff numAttempts should have been reset to 0
            Assert.IsTrue(changeTracker.backoff.NumAttempts == 0);

            changeTracker.Stop();

            var success = changeTrackerFinishedSignal.Await(TimeSpan.FromSeconds(30));
            Assert.IsTrue(success);
        }
        public void TestBatcherBatchSize5()
        {
            doneSignal = new CountDownLatch(10);

            inboxCapacity = 10;
            processorDelay = 1000;

            var scheduler = new SingleThreadTaskScheduler();
            var batcher = new Batcher<string>(new TaskFactory(scheduler), 
                inboxCapacity, processorDelay, TestBatcherBatchSize5Processor);

            var objectsToQueue = new List<string>();
            for (var i = 0; i < inboxCapacity * 10; i++)
            {
                objectsToQueue.Add(i.ToString());
                if (objectsToQueue.Count == 5)
                {
                    batcher.QueueObjects(objectsToQueue);
                    objectsToQueue.Clear();
                }
            }

            var success = doneSignal.Await(TimeSpan.FromSeconds(35));
            Assert.IsTrue(success);
        }
			internal ReplicationObserver(ReplicationTest _enclosing, CountDownLatch doneSignal
				)
			{
				this._enclosing = _enclosing;
				this.doneSignal = doneSignal;
			}
		/// <exception cref="System.Exception"></exception>
		public virtual void TestGoOffline()
		{
			Uri remote = GetReplicationURL();
			CountDownLatch replicationDoneSignal = new CountDownLatch(1);
			Replication repl = database.CreatePullReplication(remote);
			repl.SetContinuous(true);
			repl.Start();
			repl.GoOffline();
			NUnit.Framework.Assert.IsTrue(repl.GetStatus() == Replication.ReplicationStatus.ReplicationOffline
				);
		}
		/// <exception cref="System.Exception"></exception>
		public virtual void TestFetchRemoteCheckpointDoc()
		{
			HttpClientFactory mockHttpClientFactory = new _HttpClientFactory_583();
			Log.D("TEST", "testFetchRemoteCheckpointDoc() called");
			string dbUrlString = "http://fake.test-url.com:4984/fake/";
			Uri remote = new Uri(dbUrlString);
			database.SetLastSequence("1", remote, true);
			// otherwise fetchRemoteCheckpoint won't contact remote
			Replication replicator = new Pusher(database, remote, false, mockHttpClientFactory
				, manager.GetWorkExecutor());
			CountDownLatch doneSignal = new CountDownLatch(1);
			ReplicationTest.ReplicationObserver replicationObserver = new ReplicationTest.ReplicationObserver
				(this, doneSignal);
			replicator.AddChangeListener(replicationObserver);
			replicator.FetchRemoteCheckpointDoc();
			Log.D(Tag, "testFetchRemoteCheckpointDoc() Waiting for replicator to finish");
			try
			{
				bool succeeded = doneSignal.Await(300, TimeUnit.Seconds);
				NUnit.Framework.Assert.IsTrue(succeeded);
				Log.D(Tag, "testFetchRemoteCheckpointDoc() replicator finished");
			}
			catch (Exception e)
			{
				Sharpen.Runtime.PrintStackTrace(e);
			}
			string errorMessage = "Since we are passing in a mock http client that always throws "
				 + "errors, we expect the replicator to be in an error state";
			NUnit.Framework.Assert.IsNotNull(errorMessage, replicator.GetLastError());
		}
			public _Runnable_482(Replication replication, CountDownLatch doneSignal)
			{
				this.replication = replication;
				this.doneSignal = doneSignal;
			}
		private CountDownLatch ReplicationWatcherThread(Replication replication)
		{
			CountDownLatch doneSignal = new CountDownLatch(1);
			new Sharpen.Thread(new _Runnable_482(replication, doneSignal)).Start();
			return doneSignal;
		}
		private void RunReplication(Replication replication)
		{
			CountDownLatch replicationDoneSignal = new CountDownLatch(1);
			ReplicationTest.ReplicationObserver replicationObserver = new ReplicationTest.ReplicationObserver
				(this, replicationDoneSignal);
			replication.AddChangeListener(replicationObserver);
			replication.Start();
			CountDownLatch replicationDoneSignalPolling = ReplicationWatcherThread(replication
				);
			Log.D(Tag, "Waiting for replicator to finish");
			try
			{
				bool success = replicationDoneSignal.Await(300, TimeUnit.Seconds);
				NUnit.Framework.Assert.IsTrue(success);
				success = replicationDoneSignalPolling.Await(300, TimeUnit.Seconds);
				NUnit.Framework.Assert.IsTrue(success);
				Log.D(Tag, "replicator finished");
			}
			catch (Exception e)
			{
				Sharpen.Runtime.PrintStackTrace(e);
			}
		}
			public _BackgroundTask_376(Uri pathToDoc1, string docJson, CountDownLatch httpRequestDoneSignal
				)
			{
				this.pathToDoc1 = pathToDoc1;
				this.docJson = docJson;
				this.httpRequestDoneSignal = httpRequestDoneSignal;
			}
        public void TestBatcherLatencyTrickleIn()
        {
            doneSignal = new CountDownLatch(10);

            inboxCapacity = 100;
            processorDelay = 500;

            maxObservedDelta = -1L;

            var scheduler = new SingleThreadTaskScheduler();
            var batcher = new Batcher<long>(new TaskFactory(scheduler), 
                inboxCapacity, processorDelay, TestBatcherLatencyTrickleInProcessor);
                
            for (var i = 0; i < 10; i++)
            {            
                var objectsToQueue = new List<long>();
                objectsToQueue.Add(Runtime.CurrentTimeMillis());
                batcher.QueueObjects(objectsToQueue);
                System.Threading.Thread.Sleep(1000);
            }

            var success = doneSignal.Await(TimeSpan.FromSeconds(35));
            Assert.IsTrue(success);

            // we want the max observed delta between the time it was queued until the
            // time it was processed to be as small as possible.  since
            // there is some overhead, rather than using a hardcoded number
            // express it as a ratio of 1/4th the processor delay, asserting
            // that the entire processor delay never kicked in.
            int acceptableMaxDelta = processorDelay - 1;

            Log.V(Tag, string.Format("TestBatcherLatencyTrickleIn : maxObservedDelta: {0}", maxObservedDelta));

            Assert.IsTrue((maxObservedDelta < acceptableMaxDelta));
        }
		/// <exception cref="System.Exception"></exception>
		public virtual void TestPusher()
		{
			CountDownLatch replicationDoneSignal = new CountDownLatch(1);
			Uri remote = GetReplicationURL();
			string docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis());
			// Create some documents:
			IDictionary<string, object> documentProperties = new Dictionary<string, object>();
			string doc1Id = string.Format("doc1-%s", docIdTimestamp);
			documentProperties.Put("_id", doc1Id);
			documentProperties.Put("foo", 1);
			documentProperties.Put("bar", false);
			Body body = new Body(documentProperties);
			RevisionInternal rev1 = new RevisionInternal(body, database);
			Status status = new Status();
			rev1 = database.PutRevision(rev1, null, false, status);
			NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
			documentProperties.Put("_rev", rev1.GetRevId());
			documentProperties.Put("UPDATED", true);
			RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties
				, database), rev1.GetRevId(), false, status);
			NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
			documentProperties = new Dictionary<string, object>();
			string doc2Id = string.Format("doc2-%s", docIdTimestamp);
			documentProperties.Put("_id", doc2Id);
			documentProperties.Put("baz", 666);
			documentProperties.Put("fnord", true);
			database.PutRevision(new RevisionInternal(documentProperties, database), null, false
				, status);
			NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode());
			bool continuous = false;
			Replication repl = database.CreatePushReplication(remote);
			repl.SetContinuous(continuous);
			repl.SetCreateTarget(true);
			// Check the replication's properties:
			NUnit.Framework.Assert.AreEqual(database, repl.GetLocalDatabase());
			NUnit.Framework.Assert.AreEqual(remote, repl.GetRemoteUrl());
			NUnit.Framework.Assert.IsFalse(repl.IsPull());
			NUnit.Framework.Assert.IsFalse(repl.IsContinuous());
			NUnit.Framework.Assert.IsTrue(repl.ShouldCreateTarget());
			NUnit.Framework.Assert.IsNull(repl.GetFilter());
			NUnit.Framework.Assert.IsNull(repl.GetFilterParams());
			// TODO: CAssertNil(r1.doc_ids);
			// TODO: CAssertNil(r1.headers);
			// Check that the replication hasn't started running:
			NUnit.Framework.Assert.IsFalse(repl.IsRunning());
			NUnit.Framework.Assert.AreEqual(Replication.ReplicationStatus.ReplicationStopped, 
				repl.GetStatus());
			NUnit.Framework.Assert.AreEqual(0, repl.GetCompletedChangesCount());
			NUnit.Framework.Assert.AreEqual(0, repl.GetChangesCount());
			NUnit.Framework.Assert.IsNull(repl.GetLastError());
			RunReplication(repl);
			// make sure doc1 is there
			// TODO: make sure doc2 is there (refactoring needed)
			Uri replicationUrlTrailing = new Uri(string.Format("%s/", remote.ToExternalForm()
				));
			Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id);
			Log.D(Tag, "Send http request to " + pathToDoc);
			CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
			BackgroundTask getDocTask = new _BackgroundTask_122(pathToDoc, doc1Id, httpRequestDoneSignal
				);
			//Closes the connection.
			getDocTask.Execute();
			Log.D(Tag, "Waiting for http request to finish");
			try
			{
				httpRequestDoneSignal.Await(300, TimeUnit.Seconds);
				Log.D(Tag, "http request finished");
			}
			catch (Exception e)
			{
				Sharpen.Runtime.PrintStackTrace(e);
			}
			Log.D(Tag, "testPusher() finished");
		}
        private void ChangeTrackerTestWithMode(ChangeTrackerMode mode)
        {
            var changeTrackerFinishedSignal = new CountDownLatch(1);
            var changeReceivedSignal = new CountDownLatch(1);
            var client = new ChangeTrackerTestClient(changeTrackerFinishedSignal, changeReceivedSignal);

            client.ReceivedChangeDelegate = (IDictionary<string, object> change) =>
            {
                Assert.IsTrue(change.ContainsKey("seq"));
                Assert.AreEqual("1", change["seq"]);
            };

            var handler = client.HttpRequestHandler;

            handler.SetResponder("_changes", (request) => 
            {
                var json = "{\"results\":[\n" +
                    "{\"seq\":\"1\",\"id\":\"doc1-138\",\"changes\":[{\"rev\":\"1-82d\"}]}],\n" +
                    "\"last_seq\":\"*:50\"}";
                return MockHttpRequestHandler.GenerateHttpResponseMessage(HttpStatusCode.OK, null, json);
            });

            var testUrl = GetReplicationURL();
            var scheduler = new SingleTaskThreadpoolScheduler();
            var changeTracker = new ChangeTracker(testUrl, mode, 0, false, client, new TaskFactory(scheduler));

            changeTracker.UsePost = IsSyncGateway(testUrl);
            changeTracker.Start();

            var success = changeReceivedSignal.Await(TimeSpan.FromSeconds(30));
            Assert.IsTrue(success);

            changeTracker.Stop();

            success = changeTrackerFinishedSignal.Await(TimeSpan.FromSeconds(30));
            Assert.IsTrue(success);
        }
 public CyclicBarrier(int parties)
 {
     this.counter = new CountDownLatch(parties);
 }
        private void RunChangeTrackerTransientError(
            ChangeTrackerMode mode,
            Int32 errorCode,
            string statusMessage,
            Int32 numExpectedChangeCallbacks) 
        {
            var changeTrackerFinishedSignal = new CountDownLatch(1);
            var changeReceivedSignal = new CountDownLatch(numExpectedChangeCallbacks);
            var client = new ChangeTrackerTestClient(changeTrackerFinishedSignal, changeReceivedSignal);

            MockHttpRequestHandler.HttpResponseDelegate sentinal = RunChangeTrackerTransientErrorDefaultResponder();

            var responders = new List<MockHttpRequestHandler.HttpResponseDelegate>();
            responders.Add(RunChangeTrackerTransientErrorDefaultResponder());
            responders.Add(MockHttpRequestHandler.TransientErrorResponder(errorCode, statusMessage));

            MockHttpRequestHandler.HttpResponseDelegate chainResponder = (request) =>
            {
                if (responders.Count > 0) {
                    var responder = responders[0];
                    responders.RemoveAt(0);
                    return responder(request);
                }

                return sentinal(request);
            };

            var handler = client.HttpRequestHandler;
            handler.SetResponder("_changes", chainResponder);

            var testUrl = GetReplicationURL();
            var scheduler = new SingleTaskThreadpoolScheduler();
            var changeTracker = new ChangeTracker(testUrl, mode, 0, false, client, new TaskFactory(scheduler));

            changeTracker.UsePost = IsSyncGateway(testUrl);
            changeTracker.Start();

            var success = changeReceivedSignal.Await(TimeSpan.FromSeconds(30));
            Assert.IsTrue(success);

            changeTracker.Stop();

            success = changeTrackerFinishedSignal.Await(TimeSpan.FromSeconds(30));
            Assert.IsTrue(success);
        }
		public CyclicBarrier (int parties)
		{
			this.counter = new CountDownLatch (parties);
		}
 public _ChangeTrackerClient_119(CountDownLatch changeTrackerFinishedSignal, CountDownLatch
      changeReceivedSignal)
 {
     this.changeTrackerFinishedSignal = changeTrackerFinishedSignal;
     this.changeReceivedSignal = changeReceivedSignal;
 }
		public virtual void TestUpdateOnBackgroundThreads()
		{
			ThreadSafeProgressMonitorTest.MockProgressMonitor mock = new ThreadSafeProgressMonitorTest.MockProgressMonitor
				();
			ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
			pm.StartWorker();
			CountDownLatch doUpdate = new CountDownLatch(1);
			CountDownLatch didUpdate = new CountDownLatch(1);
			CountDownLatch doEndWorker = new CountDownLatch(1);
			Sharpen.Thread bg = new _Thread_128(pm, doUpdate, didUpdate, doEndWorker);
			bg.Start();
			pm.PollForUpdates();
			NUnit.Framework.Assert.AreEqual(0, mock.value);
			doUpdate.CountDown();
			Await(didUpdate);
			pm.PollForUpdates();
			NUnit.Framework.Assert.AreEqual(2, mock.value);
			doEndWorker.CountDown();
			pm.WaitForCompletion();
			NUnit.Framework.Assert.AreEqual(3, mock.value);
		}
 public _ChangeTrackerClient_31(CountDownLatch changeTrackerFinishedSignal)
 {
     this.changeTrackerFinishedSignal = changeTrackerFinishedSignal;
 }
			public _Thread_128(ThreadSafeProgressMonitor pm, CountDownLatch doUpdate, CountDownLatch
				 didUpdate, CountDownLatch doEndWorker)
			{
				this.pm = pm;
				this.doUpdate = doUpdate;
				this.didUpdate = didUpdate;
				this.doEndWorker = doEndWorker;
			}
        private void RunReplication(Replication replication)
        {
            var replicationDoneSignal = new CountDownLatch(1);
            var observer = new ReplicationObserver(replicationDoneSignal);
            replication.Changed += observer.Changed;
            replication.Start();

            var replicationDoneSignalPolling = ReplicationWatcherThread(replication);

            Log.D(Tag, "Waiting for replicator to finish.");

                var success = replicationDoneSignal.Await(TimeSpan.FromSeconds(15));
                Assert.IsTrue(success);
                success = replicationDoneSignalPolling.Wait(TimeSpan.FromSeconds(15));
                Assert.IsTrue(success);

                Log.D(Tag, "replicator finished");

            replication.Changed -= observer.Changed;
        }
		private static void Await(CountDownLatch cdl)
		{
			try
			{
				NUnit.Framework.Assert.IsTrue(cdl.Await(1000, TimeUnit.MILLISECONDS), "latch released"
					);
			}
			catch (Exception)
			{
				NUnit.Framework.Assert.Fail("Did not expect to be interrupted");
			}
		}
        private void VerifyRemoteDocExists(Uri remote, string docId)
        {
            var replicationUrlTrailing = new Uri(string.Format("{0}/", remote));
            var pathToDoc = new Uri(replicationUrlTrailing, docId);
            Log.D(Tag, "Send http request to " + pathToDoc);

            var httpRequestDoneSignal = new CountDownLatch(1);
            Task.Factory.StartNew(() =>
            {
                var httpclient = new HttpClient();
                HttpResponseMessage response;
                string responseString = null;
                try
                {
                    var responseTask = httpclient.GetAsync(pathToDoc.ToString());
                    responseTask.Wait(TimeSpan.FromSeconds(1));
                    response = responseTask.Result;
                    var statusLine = response.StatusCode;
                    Assert.IsTrue(statusLine == HttpStatusCode.OK);
                    if (statusLine == HttpStatusCode.OK)
                    {
                        var responseStringTask = response.Content.ReadAsStringAsync();
                        responseStringTask.Wait(TimeSpan.FromSeconds(10));
                        responseString = responseStringTask.Result;
                        Assert.IsTrue(responseString.Contains(docId));
                        Log.D(ReplicationTest.Tag, "result: " + responseString);
                    }
                    else
                    {
                        var statusReason = response.ReasonPhrase;
                        response.Dispose();
                        throw new IOException(statusReason);
                    }
                }
                catch (ProtocolViolationException e)
                {
                    Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
                }
                catch (IOException e)
                {
                    Assert.IsNull(e, "Got IOException: " + e.Message);
                }

                httpRequestDoneSignal.CountDown();
            });

            var result = httpRequestDoneSignal.Await(TimeSpan.FromSeconds(30));
            Assert.IsTrue(result, "Could not retrieve the new doc from the sync gateway.");
        }
 public ReplicationStoppedObserver(CountDownLatch doneSignal)
 {
     this.doneSignal = doneSignal;
 }
        public void TestPusherDeletedDoc()
        {
            if (!Boolean.Parse((string)Runtime.Properties["replicationTestsEnabled"]))
            {
                Assert.Inconclusive("Replication tests disabled.");
                return;
            }

            using (var remoteDb = _sg.CreateDatabase(TempDbName())) {
                var remote = remoteDb.RemoteUri;
                var docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis());

                // Create some documentsConvert
                var documentProperties = new Dictionary<string, object>();
                var doc1Id = string.Format("doc1-{0}", docIdTimestamp);
                documentProperties["_id"] = doc1Id;
                documentProperties["foo"] = 1;
                documentProperties["bar"] = false;

                var body = new Body(documentProperties);
                var rev1 = new RevisionInternal(body);
                rev1 = database.PutRevision(rev1, null, false);

                documentProperties["_rev"] = rev1.GetRevId();
                documentProperties["UPDATED"] = true;
                documentProperties["_deleted"] = true;
                database.PutRevision(new RevisionInternal(documentProperties), rev1.GetRevId(), false);

                var repl = database.CreatePushReplication(remote);
                if (!IsSyncGateway(remote)) {
                    ((Pusher)repl).CreateTarget = true;
                }

                RunReplication(repl);

                Assert.IsNull(repl.LastError);

                // make sure doc1 is deleted
                var replicationUrlTrailing = new Uri(string.Format("{0}/", remote));
                var pathToDoc = new Uri(replicationUrlTrailing, doc1Id);
                Log.D(Tag, "Send http request to " + pathToDoc);
                var httpRequestDoneSignal = new CountDownLatch(1);
                using (var httpclient = new HttpClient()) {
                    try {
                        var getDocResponse = httpclient.GetAsync(pathToDoc.ToString()).Result;
                        var statusLine = getDocResponse.StatusCode;
                        Log.D(ReplicationTest.Tag, "statusLine " + statusLine);
                        Assert.AreEqual(Couchbase.Lite.StatusCode.NotFound, statusLine.GetStatusCode());                        
                    }
                    catch (ProtocolViolationException e) {
                        Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
                    }
                    catch (IOException e) {
                        Assert.IsNull(e, "Got IOException: " + e.Message);
                    }
                    finally {
                        httpRequestDoneSignal.CountDown();
                    }
                    Log.D(Tag, "Waiting for http request to finish");
                    try {
                        httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10));
                        Log.D(Tag, "http request finished");
                    }
                    catch (Exception e) {
                        Runtime.PrintStackTrace(e);
                    }
                }
            }
        }
 public ReplicationErrorObserver(CountDownLatch doneSignal)
 {
     this.doneSignal = doneSignal;
 }