protected virtual void RunReplication(Replication replication)
        {
            var replicationDoneSignal = new CountdownEvent(1);
            var observer = new ReplicationObserver(replicationDoneSignal);

            replication.Changed += observer.Changed;
            replication.Start();
            var success = replicationDoneSignal.Wait(TimeSpan.FromSeconds(60));

            Assert.IsTrue(success);

            replication.Changed -= observer.Changed;
        }
        public void StopReplication(Replication replication)
        {
            var replicationDoneSignal      = new CountDownLatch(1);
            var replicationStoppedObserver = new ReplicationObserver(replicationDoneSignal);

            replication.Changed += replicationStoppedObserver.Changed;
            replication.Stop();

            var success = replicationDoneSignal.Await(TimeSpan.FromSeconds(30));

            Assert.IsTrue(success);

            // give a little padding to give it a chance to save a checkpoint
            System.Threading.Thread.Sleep(2 * 1000);
        }
示例#3
0
        protected override void RunReplication(Replication replication)
        {
            var replicationDoneSignal = new CountdownEvent(1);
            var observer = new ReplicationObserver(replicationDoneSignal);

            replication.Changed += observer.Changed;
            replication.Start();

            var replicationDoneSignalPolling = ReplicationWatcherThread(replication);

            var success = replicationDoneSignal.Wait(TimeSpan.FromSeconds(15));

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

            replication.Changed -= observer.Changed;
        }
示例#4
0
        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 void RunPushReplicationWithTransientError(Int32 errorCode, string statusMessage, Boolean expectError) 
        {
            var properties1 = new Dictionary<string, object>() 
            {
                {"doc1", "testPushReplicationTransientError"}
            };
            CreateDocumentWithProperties(database, properties1);

            var httpClientFactory = new MockHttpClientFactory(false);
            var httpHandler = httpClientFactory.HttpHandler; 
            manager.DefaultHttpClientFactory = httpClientFactory;

            MockHttpRequestHandler.HttpResponseDelegate sentinal = MockHttpRequestHandler.FakeBulkDocs;

            var responders = new List<MockHttpRequestHandler.HttpResponseDelegate>();
            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);
            };

            httpHandler.SetResponder("_bulk_docs", chainResponder);

            // Create a replication observer to wait until replication finishes
            var replicationDoneSignal = new CountdownEvent(1);
            var replicationFinishedObserver = new ReplicationObserver(replicationDoneSignal);
            using (var remoteDb = _sg.CreateDatabase(TempDbName())) {
                var pusher = database.CreatePushReplication(remoteDb.RemoteUri);
                pusher.Changed += replicationFinishedObserver.Changed;

                // save the checkpoint id for later usage
                var checkpointId = pusher.RemoteCheckpointDocID();

                // kick off the replication
                pusher.Start();

                // wait for it to finish
                var success = replicationDoneSignal.Wait(TimeSpan.FromSeconds(30));
                Assert.IsTrue(success);

                if (expectError) {
                    Assert.IsNotNull(pusher.LastError);
                }
                else {
                    Assert.IsNull(pusher.LastError);
                }

                // workaround for the fact that the replicationDoneSignal.Await() call could unblock before all
                // the statements in Replication.Stopped() have even had a chance to execute.
                Sleep(500);

                var localLastSequence = database.LastSequenceWithCheckpointId(checkpointId);
                if (expectError) {
                    Assert.Null(localLastSequence);
                }
                else {
                    Assert.IsNotNull(localLastSequence);
                }
            }
        }
        public void TestPushReplicationCanMissDocs()
        {
            Assert.Inconclusive("Not sure this is a valid test.");
            if (!Boolean.Parse((string)Runtime.Properties["replicationTestsEnabled"]))
            {
                Assert.Inconclusive("Replication tests disabled.");
                return;
            }
            Assert.AreEqual(0, database.GetLastSequenceNumber());

            var properties1 = new Dictionary<string, object>();
            properties1["doc1"] = "testPushReplicationCanMissDocs";
            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, doc2Responder);

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

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

            Assert.IsNotNull(pusher.LastError);

            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);
        }
        private void RunReplication(Replication replication)
        {
            var replicationDoneSignal = new CountdownEvent(1);
            var observer = new ReplicationObserver(replicationDoneSignal);
            replication.Changed += observer.Changed;
            replication.Start();

            var replicationDoneSignalPolling = ReplicationWatcherThread(replication, observer);

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

            var success = WaitHandle.WaitAll(new[] { replicationDoneSignal.WaitHandle, replicationDoneSignalPolling.WaitHandle }, 60*1000);
            Assert.IsTrue(success);

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

            replication.Changed -= observer.Changed;
        }
        private static CountdownEvent ReplicationWatcherThread(Replication replication, ReplicationObserver observer)
        {
            var started = replication.IsRunning;
            var doneSignal = new CountdownEvent(1);

            Task.Factory.StartNew(()=>
            {
                var done = observer.IsReplicationFinished(); //Prevent race condition where the replicator stops before this portion is reached
                while (!done)
                {
                    if (replication.IsRunning)
                    {
                        started = true;
                    }

                    var statusIsDone = (
                        replication.Status == ReplicationStatus.Stopped 
                        || replication.Status == ReplicationStatus.Idle
                    );

                    if (started && statusIsDone)
                    {
                        break;
                    }

                    try
                    {
                        Thread.Sleep(1000);
                    }
                    catch (Exception e)
                    {
                        Runtime.PrintStackTrace(e);
                    }
                }
                doneSignal.Signal();
            });

            return doneSignal;
        }
        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;
        }
        public void StopReplication(Replication replication)
        {
            var replicationDoneSignal = new CountdownEvent(1);
            var replicationStoppedObserver = new ReplicationObserver(replicationDoneSignal);
            replication.Changed += replicationStoppedObserver.Changed;
            replication.Stop();

            var success = replicationDoneSignal.Wait(TimeSpan.FromSeconds(30));
            Assert.IsTrue(success);

            // give a little padding to give it a chance to save a checkpoint
            System.Threading.Thread.Sleep(2 * 1000);
        }
示例#11
0
        protected virtual void RunReplication(Replication replication)
        {
            var replicationDoneSignal = new CountdownEvent(1);
            var observer = new ReplicationObserver(replicationDoneSignal);
            replication.Changed += observer.Changed;
            replication.Start();
            var success = replicationDoneSignal.Wait(TimeSpan.FromSeconds(60));
            Assert.IsTrue(success);

            replication.Changed -= observer.Changed;
        }
        protected override void RunReplication(Replication replication)
        {
            var replicationDoneSignal = new CountdownEvent(1);
            var observer = new ReplicationObserver(replicationDoneSignal);
            replication.Changed += observer.Changed;
            replication.Start();

            var replicationDoneSignalPolling = ReplicationWatcherThread(replication);

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

            replication.Changed -= observer.Changed;
        }
示例#13
0
        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);
        }