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());
        }
        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());
        }
Beispiel #3
0
		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());
		}
Beispiel #4
0
			public _RefsChangedListener_1156(RefDatabase refDb, AtomicInteger changeCount, AtomicReference
				<StackOverflowError> error, AtomicReference<IOException> exception)
			{
				this.refDb = refDb;
				this.changeCount = changeCount;
				this.error = error;
				this.exception = exception;
			}
Beispiel #5
0
			public _ChangeListener_758(AtomicInteger atomicInteger, int kNDocs, CountDownLatch
				 doneSignal)
			{
				this.atomicInteger = atomicInteger;
				this.kNDocs = kNDocs;
				this.doneSignal = doneSignal;
			}
		/// <summary>Wrap a ProgressMonitor to be thread safe.</summary>
		/// <remarks>Wrap a ProgressMonitor to be thread safe.</remarks>
		/// <param name="pm">the underlying monitor to receive events.</param>
		public ThreadSafeProgressMonitor(ProgressMonitor pm)
		{
			this.pm = pm;
			this.Lock = new ReentrantLock();
			this.mainThread = Sharpen.Thread.CurrentThread();
			this.workers = new AtomicInteger(0);
			this.pendingUpdates = new AtomicInteger(0);
			this.process = Sharpen.Extensions.CreateSemaphore(0);
		}
			public _ChangeListener_78(AtomicInteger atomicInteger)
			{
				this.atomicInteger = atomicInteger;
			}
Beispiel #8
0
		/// <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());
		}
 public _TransactionalTask_1104(ApiTest _enclosing, int numberOfDocuments, Document
     [] docs, int numberOfUpdates, AtomicInteger numDocsUpdated, AtomicInteger numExceptions
     )
 {
     this._enclosing = _enclosing;
     this.numberOfDocuments = numberOfDocuments;
     this.docs = docs;
     this.numberOfUpdates = numberOfUpdates;
     this.numDocsUpdated = numDocsUpdated;
     this.numExceptions = numExceptions;
 }
 /// <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());
 }