Example #1
0
        public void Dispose()
        {
            try
            {
                if (_replicator != null)
                {
                    StopReplication();

                    // Because the 'Stop' method for a Replicator instance is asynchronous
                    // we must wait for the status activity to be stopped before closing the database.
                    while (true)
                    {
                        if (_replicator.Status.Activity == ReplicatorActivityLevel.Stopped)
                        {
                            break;
                        }
                    }

                    _replicator.Dispose();
                }

                _database.Close();
                _database = null;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        protected override void Dispose(bool disposing)
        {
            _repl?.Dispose();
            _repl = null;

            base.Dispose(disposing);

            _otherDB.Delete();
            _otherDB = null;
        }
Example #3
0
        /// <inheritdoc />
        public void Dispose()
        {
            Ended?.Invoke(this, null);
            Replicator?.Stop();
            // Uncomment after DB019 (https://github.com/couchbase/couchbase-lite-net/issues/908)
            Replicator?.Dispose();

            Database?.Dispose();
            HttpClient.Dispose();
        }
Example #4
0
        private void RemoveReplicator()
        {
            if (IsStarted)
            {
                StopReplicator();
            }

            _repl?.RemoveChangeListener(_listenerToken);
            _repl?.Dispose();
        }
Example #5
0
        public void TestP2PPassiveCloseAll()
        {
            var listener          = new MessageEndpointListener(new MessageEndpointListenerConfiguration(_otherDB, ProtocolType.MessageStream));
            var serverConnection1 = new MockServerConnection(listener, ProtocolType.MessageStream);
            var serverConnection2 = new MockServerConnection(listener, ProtocolType.MessageStream);
            var errorLogic        = new ReconnectErrorLogic();
            var config            = new ReplicatorConfiguration(Db,
                                                                new MessageEndpoint("p2ptest1", serverConnection1, ProtocolType.MessageStream,
                                                                                    new MockConnectionFactory(errorLogic)))
            {
                Continuous = true
            };

            var config2 = new ReplicatorConfiguration(Db,
                                                      new MessageEndpoint("p2ptest2", serverConnection2, ProtocolType.MessageStream,
                                                                          new MockConnectionFactory(errorLogic)))
            {
                Continuous = true
            };

            var replicator = new Replicator(config);

            replicator.Start();
            var replicator2 = new Replicator(config2);

            replicator2.Start();

            var count = 0;

            while (count++ < 10 && replicator.Status.Activity != ReplicatorActivityLevel.Idle && replicator2.Status.Activity != ReplicatorActivityLevel.Idle)
            {
                Thread.Sleep(500);
                count.Should().BeLessThan(10, "because otherwise the replicator(s) never went idle");
            }

            errorLogic.ErrorActive = true;
            listener.CloseAll();
            count = 0;
            while (count++ < 10 && replicator.Status.Activity != ReplicatorActivityLevel.Stopped && replicator2.Status.Activity != ReplicatorActivityLevel.Stopped)
            {
                Thread.Sleep(500);
                count.Should().BeLessThan(10, "because otherwise the replicator(s) never stopped");
            }

            replicator.Status.Error.Should()
            .NotBeNull("because closing the passive side creates an error on the active one");
            replicator2.Status.Error.Should()
            .NotBeNull("because closing the passive side creates an error on the active one");
            replicator.Dispose();
            replicator2.Dispose();
        }
        private void Restart()
        {
            try
            {
                _replicator.RemoveChangeListener(_token);
                _replicator.Stop();
                _replicator = null;
                _replicator.Dispose();
            }
            catch (Exception)
            { }

            Connect();
        }
        protected override void Dispose(bool disposing)
        {
            _repl?.Dispose();
            _repl = null;

            base.Dispose(disposing);

            // HACK: Try to give time for all async operations to shut down before deleting
            // and starting another test
            Thread.Sleep(TimeSpan.FromMilliseconds(300));

            _otherDB.Delete();
            _otherDB = null;
        }
Example #8
0
        private void Restart(ReplicatorStatusMessage obj)
        {
            try
            {
                _replicator.RemoveChangeListener(_token);
                _replicator.Stop();
                _replicator.Dispose();
                _replicator = null;

                Connect();
            }
            catch (Exception e)
            {
#if DEBUG
#endif
            }
        }
Example #9
0
    void FixedUpdate()
    {
        // NOTE: This quitting process is a first draft and has not been tested yet!
        // Please make sure you test your own process before using in production.
        if (quitting)
        {
            if (replicator != null && replicator.Status.Activity != ReplicatorActivityLevel.Stopped)
            {
                try {
                    replicator.Stop();
                } catch (Exception) {}
            }
            else
            {
                if (replicator != null)
                {
                    replicator.Dispose();
                }

                try {
                    database.GetDocument("meta");
                    database.Close();
                    database.Dispose();
                } catch (ObjectDisposedException) {
                    allDisposed = true;
                    Application.Quit();
                }
            }
        }
        else if (!syncing)
        {
            if (doneSyncing)
            {
                statusLabel.text += $"\n- Done syncing: {replicator.Status.Activity}";
                doneSyncing       = false;
            }
            // Other updates
        }
    }
        public void TestReplicatorAndListenerOnSameDatabase()
        {
            using (var doc = new MutableDocument()) {
                OtherDb.Save(doc);
            }

            CreateListener();
            using (var doc1 = new MutableDocument()) {
                Db.Save(doc1);
            }

            var target  = new DatabaseEndpoint(Db);
            var config1 = CreateConfig(target, ReplicatorType.PushAndPull, true, sourceDb: OtherDb);
            var repl1   = new Replicator(config1);

            Database.Delete("urlepTestDb", Directory);
            var urlepTestDb = OpenDB("urlepTestDb");

            using (var doc2 = new MutableDocument()) {
                urlepTestDb.Save(doc2);
            }

            var config2 = CreateConfig(_listener.LocalEndpoint(), ReplicatorType.PushAndPull, true,
                                       serverCert: _listener.TlsIdentity.Certs[0], sourceDb: urlepTestDb);
            var repl2 = new Replicator(config2);

            var wait1 = new ManualResetEventSlim();
            var wait2 = new ManualResetEventSlim();
            EventHandler <ReplicatorStatusChangedEventArgs> changeListener = (sender, args) =>
            {
                if (args.Status.Activity == ReplicatorActivityLevel.Idle && args.Status.Progress.Completed ==
                    args.Status.Progress.Total)
                {
                    if (OtherDb.Count == 3 && Db.Count == 3 && urlepTestDb.Count == 3)
                    {
                        ((Replicator)sender).Stop();
                    }
                }
                else if (args.Status.Activity == ReplicatorActivityLevel.Stopped)
                {
                    if (sender == repl1)
                    {
                        wait1.Set();
                    }
                    else
                    {
                        wait2.Set();
                    }
                }
            };

            var token1 = repl1.AddChangeListener(changeListener);
            var token2 = repl2.AddChangeListener(changeListener);

            repl1.Start();
            repl2.Start();
            WaitHandle.WaitAll(new[] { wait1.WaitHandle, wait2.WaitHandle }, TimeSpan.FromSeconds(20))
            .Should().BeTrue();

            repl1.RemoveChangeListener(token1);
            repl2.RemoveChangeListener(token2);

            Db.Count.Should().Be(3, "because otherwise not all docs were received into Db");
            OtherDb.Count.Should().Be(3, "because otherwise not all docs were received into OtherDb");
            urlepTestDb.Count.Should().Be(3, "because otherwise not all docs were received into urlepTestDb");

            repl1.Dispose();
            repl2.Dispose();
            wait1.Dispose();
            wait2.Dispose();
            urlepTestDb.Delete();

            _listener.Stop();

            Thread.Sleep(500); // wait for everything to stop
        }
        // Two replicators, replicates docs to the listener; validates connection status
        private void ValidateMultipleReplicationsTo(ReplicatorType replicatorType)
        {
            ulong maxConnectionCount = 0UL;
            ulong maxActiveCount     = 0UL;

            var existingDocsInListener = _listener.Config.Database.Count;

            existingDocsInListener.Should().Be(1);

            using (var doc1 = new MutableDocument()) {
                Db.Save(doc1);
            }

            var target     = _listener.LocalEndpoint();
            var serverCert = _listener.TlsIdentity.Certs[0];
            var config1    = CreateConfig(target, replicatorType, true,
                                          serverCert: serverCert, sourceDb: Db);
            var repl1 = new Replicator(config1);

            Database.Delete("urlepTestDb", Directory);
            var urlepTestDb = OpenDB("urlepTestDb");

            using (var doc2 = new MutableDocument()) {
                urlepTestDb.Save(doc2);
            }

            var config2 = CreateConfig(target, replicatorType, true,
                                       serverCert: serverCert, sourceDb: urlepTestDb);
            var repl2 = new Replicator(config2);

            var wait1 = new ManualResetEventSlim();
            var wait2 = new ManualResetEventSlim();
            EventHandler <ReplicatorStatusChangedEventArgs> changeListener = (sender, args) =>
            {
                maxConnectionCount = Math.Max(maxConnectionCount, _listener.Status.ConnectionCount);
                maxActiveCount     = Math.Max(maxActiveCount, _listener.Status.ActiveConnectionCount);

                if (args.Status.Activity == ReplicatorActivityLevel.Idle && args.Status.Progress.Completed ==
                    args.Status.Progress.Total)
                {
                    if ((replicatorType == ReplicatorType.PushAndPull && OtherDb.Count == 3 &&
                         Db.Count == 3 && urlepTestDb.Count == 3) || (replicatorType == ReplicatorType.Pull && OtherDb.Count == 1 &&
                                                                      Db.Count == 2 && urlepTestDb.Count == 2))
                    {
                        ((Replicator)sender).Stop();
                    }
                }
                else if (args.Status.Activity == ReplicatorActivityLevel.Stopped)
                {
                    if (sender == repl1)
                    {
                        wait1.Set();
                    }
                    else
                    {
                        wait2.Set();
                    }
                }
            };

            var token1 = repl1.AddChangeListener(changeListener);
            var token2 = repl2.AddChangeListener(changeListener);

            repl1.Start();
            repl2.Start();

            while (repl1.Status.Activity != ReplicatorActivityLevel.Busy ||
                   repl2.Status.Activity != ReplicatorActivityLevel.Busy)
            {
                Thread.Sleep(100);
            }

            // For some reason running on mac throws off the timing enough so that the active connection count
            // of 1 is never seen.  So record the value right after it becomes busy.
            maxConnectionCount = Math.Max(maxConnectionCount, _listener.Status.ConnectionCount);
            maxActiveCount     = Math.Max(maxActiveCount, _listener.Status.ActiveConnectionCount);

            WaitHandle.WaitAll(new[] { wait1.WaitHandle, wait2.WaitHandle }, TimeSpan.FromSeconds(30))
            .Should().BeTrue();

            maxConnectionCount.Should().Be(2);
            maxActiveCount.Should().Be(2);

            // all data are transferred to/from
            if (replicatorType == ReplicatorType.PushAndPull)
            {
                _listener.Config.Database.Count.Should().Be(existingDocsInListener + 2UL);
                Db.Count.Should().Be(existingDocsInListener + 2UL);
                urlepTestDb.Count.Should().Be(existingDocsInListener + 2UL);
            }
            else if (replicatorType == ReplicatorType.Pull)
            {
                _listener.Config.Database.Count.Should().Be(1);
                Db.Count.Should().Be(existingDocsInListener + 1UL);
                urlepTestDb.Count.Should().Be(existingDocsInListener + 1UL);
            }

            repl1.RemoveChangeListener(token1);
            repl2.RemoveChangeListener(token2);

            repl1.Dispose();
            repl2.Dispose();
            wait1.Dispose();
            wait2.Dispose();
            urlepTestDb.Delete();

            _listener.Stop();

            Thread.Sleep(500);
        }
Example #12
0
        private void RunTwoStepContinuous(ReplicatorType type, string uid)
        {
            var listener = new MessageEndpointListener(new MessageEndpointListenerConfiguration(_otherDB, ProtocolType.ByteStream));
            var server   = new MockServerConnection(listener, ProtocolType.ByteStream);
            var config   = new ReplicatorConfiguration(Db,
                                                       new MessageEndpoint(uid, server, ProtocolType.ByteStream, new MockConnectionFactory(null)))
            {
                ReplicatorType = type,
                Continuous     = true
            };
            var replicator = new Replicator(config);

            replicator.Start();

            Database firstSource  = null;
            Database secondSource = null;
            Database firstTarget  = null;
            Database secondTarget = null;

            if (type == ReplicatorType.Push)
            {
                firstSource  = Db;
                secondSource = Db;
                firstTarget  = _otherDB;
                secondTarget = _otherDB;
            }
            else if (type == ReplicatorType.Pull)
            {
                firstSource  = _otherDB;
                secondSource = _otherDB;
                firstTarget  = Db;
                secondTarget = Db;
            }
            else
            {
                firstSource  = Db;
                secondSource = _otherDB;
                firstTarget  = _otherDB;
                secondTarget = Db;
            }

            using (var mdoc = new MutableDocument("livesindb")) {
                mdoc.SetString("name", "db");
                mdoc.SetInt("version", 1);
                firstSource.Save(mdoc);
            }

            var count = 0;

            while (replicator.Status.Progress.Completed == 0 ||
                   replicator.Status.Activity != ReplicatorActivityLevel.Idle)
            {
                Thread.Sleep(500);
                count++;
                count.Should().BeLessThan(10, "because otherwise the replicator did not advance");
            }

            var previousCompleted = replicator.Status.Progress.Completed;

            firstTarget.Count.Should().Be(1);

            using (var savedDoc = secondSource.GetDocument("livesindb"))
                using (var mdoc = savedDoc.ToMutable()) {
                    mdoc.SetInt("version", 2);
                    secondSource.Save(mdoc);
                }

            count = 0;
            while (replicator.Status.Progress.Completed == previousCompleted ||
                   replicator.Status.Activity != ReplicatorActivityLevel.Idle)
            {
                Thread.Sleep(500);
                count++;
                count.Should().BeLessThan(10, "because otherwise the replicator did not advance");
            }

            using (var savedDoc = secondTarget.GetDocument("livesindb")) {
                savedDoc.GetInt("version").Should().Be(2);
            }

            replicator.Stop();
            while (replicator.Status.Activity != ReplicatorActivityLevel.Stopped)
            {
                Thread.Sleep(100);
            }

            replicator.Dispose();
        }