public void TestChangeListenerNotification() { const int numDocs = 50; var atomicInteger = new AtomicInteger(0); database.Changed += (sender, e) => atomicInteger.IncrementAndGet(); CreateDocuments(database, numDocs); Assert.AreEqual(numDocs, atomicInteger.Get()); }
public virtual void TestRefsChangedStackOverflow() { FileRepository newRepo = CreateBareRepository(); RefDatabase refDb = newRepo.RefDatabase; FilePath packedRefs = new FilePath(newRepo.Directory, "packed-refs"); NUnit.Framework.Assert.IsTrue(packedRefs.CreateNewFile()); AtomicReference<StackOverflowError> error = new AtomicReference<StackOverflowError >(); AtomicReference<IOException> exception = new AtomicReference<IOException>(); AtomicInteger changeCount = new AtomicInteger(); newRepo.Listeners.AddRefsChangedListener(new _RefsChangedListener_1156(refDb, changeCount , error, exception)); refDb.GetRefs("ref"); refDb.GetRefs("ref"); NUnit.Framework.Assert.IsNull(error.Get()); NUnit.Framework.Assert.IsNull(exception.Get()); NUnit.Framework.Assert.AreEqual(1, changeCount.Get()); }
public void TestChangeListenerNotificationBatching() { const int numDocs = 50; var atomicInteger = new AtomicInteger(0); var doneSignal = new CountDownLatch(1); database.Changed += (sender, e) => atomicInteger.IncrementAndGet(); database.RunInTransaction(() => { CreateDocuments(database, numDocs); doneSignal.CountDown(); return true; }); var success = doneSignal.Await(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); Assert.AreEqual(1, atomicInteger.Get()); }
/// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/84</summary> /// <exception cref="System.Exception"></exception> public virtual void TestLiveQueryStop() { int kNDocs = 100; CountDownLatch doneSignal = new CountDownLatch(1); Database db = StartDatabase(); // run a live query View view = db.GetView("vu"); view.SetMap(new _Mapper_746(), "1"); LiveQuery query = view.CreateQuery().ToLiveQuery(); AtomicInteger atomicInteger = new AtomicInteger(0); // install a change listener which decrements countdown latch when it sees a new // key from the list of expected keys LiveQuery.ChangeListener changeListener = new _ChangeListener_758(atomicInteger, kNDocs, doneSignal); query.AddChangeListener(changeListener); // create the docs that will cause the above change listener to decrement countdown latch Log.D(Database.Tag, "testLiveQueryStop: createDocumentsAsync()"); CreateDocumentsAsync(db, kNDocs); Log.D(Database.Tag, "testLiveQueryStop: calling query.start()"); query.Start(); // wait until the livequery is called back with kNDocs docs Log.D(Database.Tag, "testLiveQueryStop: waiting for doneSignal"); bool success = doneSignal.Await(120, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); Log.D(Database.Tag, "testLiveQueryStop: waiting for query.stop()"); query.Stop(); // after stopping the query, we should not get any more livequery callbacks, even // if we add more docs to the database and pause (to give time for potential callbacks) int numTimesCallbackCalled = atomicInteger.Get(); Log.D(Database.Tag, "testLiveQueryStop: numTimesCallbackCalled is: " + numTimesCallbackCalled + ". Now adding docs"); for (int i = 0; i < 10; i++) { CreateDocuments(db, 1); Log.D(Database.Tag, "testLiveQueryStop: add a document. atomicInteger.get(): " + atomicInteger.Get()); NUnit.Framework.Assert.AreEqual(numTimesCallbackCalled, atomicInteger.Get()); Sharpen.Thread.Sleep(200); } NUnit.Framework.Assert.AreEqual(numTimesCallbackCalled, atomicInteger.Get()); }
/// <summary> /// When making inserts in a transaction, the change notifications should /// be batched into a single change notification (rather than a change notification /// for each insert) /// </summary> /// <exception cref="System.Exception"></exception> public virtual void TestChangeListenerNotificationBatching() { int numDocs = 50; AtomicInteger atomicInteger = new AtomicInteger(0); CountDownLatch countDownLatch = new CountDownLatch(1); database.AddChangeListener(new _ChangeListener_78(atomicInteger)); database.RunInTransaction(new _TransactionalTask_85(this, numDocs, countDownLatch )); bool success = countDownLatch.Await(30, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); NUnit.Framework.Assert.AreEqual(1, atomicInteger.Get()); }
/// <summary> /// When making inserts outside of a transaction, there should be a change notification /// for each insert (no batching) /// </summary> /// <exception cref="System.Exception"></exception> public virtual void TestChangeListenerNotification() { int numDocs = 50; AtomicInteger atomicInteger = new AtomicInteger(0); CountDownLatch countDownLatch = new CountDownLatch(1); database.AddChangeListener(new _ChangeListener_112(atomicInteger)); CreateDocuments(database, numDocs); NUnit.Framework.Assert.AreEqual(numDocs, atomicInteger.Get()); }
/// <summary>https://github.com/couchbase/couchbase-lite-android/issues/220</summary> /// <exception cref="System.Exception"></exception> public virtual void FailingTestMultiDocumentUpdateInTransaction() { int numberOfDocuments = 10; int numberOfUpdates = 10; Document[] docs = new Document[numberOfDocuments]; database.RunInTransaction(new _TransactionalTask_1086(this, numberOfDocuments, docs )); AtomicInteger numDocsUpdated = new AtomicInteger(0); AtomicInteger numExceptions = new AtomicInteger(0); database.RunInTransaction(new _TransactionalTask_1104(this, numberOfDocuments, docs , numberOfUpdates, numDocsUpdated, numExceptions)); //toggle value of check property NUnit.Framework.Assert.AreEqual(numberOfDocuments * numberOfUpdates, numDocsUpdated .Get()); NUnit.Framework.Assert.AreEqual(0, numExceptions.Get()); }
/// <summary>https://github.com/couchbase/couchbase-lite-android/issues/220</summary> /// <exception cref="System.Exception"></exception> public virtual void TestMultiDocumentUpdate() { int numberOfDocuments = 10; int numberOfUpdates = 10; Document[] docs = new Document[numberOfDocuments]; for (int j = 0; j < numberOfDocuments; j++) { IDictionary<string, object> prop = new Dictionary<string, object>(); prop.Put("foo", "bar"); prop.Put("toogle", true); Document document = CreateDocumentWithProperties(database, prop); docs[j] = document; } AtomicInteger numDocsUpdated = new AtomicInteger(0); AtomicInteger numExceptions = new AtomicInteger(0); for (int j_1 = 0; j_1 < numberOfDocuments; j_1++) { Document doc = docs[j_1]; for (int k = 0; k < numberOfUpdates; k++) { IDictionary<string, object> contents = new Hashtable(doc.GetProperties()); bool wasChecked = (bool)contents.Get("toogle"); //toggle value of check property contents.Put("toogle", !wasChecked); try { doc.PutProperties(contents); numDocsUpdated.IncrementAndGet(); } catch (CouchbaseLiteException cblex) { Log.E(Tag, "Document update failed", cblex); numExceptions.IncrementAndGet(); } } } NUnit.Framework.Assert.AreEqual(numberOfDocuments * numberOfUpdates, numDocsUpdated .Get()); NUnit.Framework.Assert.AreEqual(0, numExceptions.Get()); }