CountDown() public method

public CountDown ( ) : void
return void
コード例 #1
0
        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);
                    }
                }
            }
        }
コード例 #2
0
        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.");
        }
コード例 #3
0
        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);
        }
コード例 #4
0
		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);
		}
コード例 #5
0
        /// <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();
        }
コード例 #6
0
		public virtual void TestPusher()
		{
			var replicationDoneSignal = new CountDownLatch(1);
			var remote = GetReplicationURL();
			var docIdTimestamp = Convert.ToString(Runtime.CurrentTimeMillis());

			// Create some documents:
			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, database);
			var status = new Status();
			rev1 = database.PutRevision(rev1, null, false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

			documentProperties.Put("_rev", rev1.GetRevId());
			documentProperties["UPDATED"] = true;
			var rev2 = database.PutRevision(new RevisionInternal(documentProperties, database), rev1.GetRevId(), false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

			documentProperties = new Dictionary<string, object>();
			var doc2Id = string.Format("doc2-{0}", docIdTimestamp);
			documentProperties["_id"] = doc2Id;
			documentProperties["baz"] = 666;
			documentProperties["fnord"] = true;

			database.PutRevision(new RevisionInternal(documentProperties, database), null, false, status);
            Assert.AreEqual(StatusCode.Created, status.GetCode());

			var continuous = false;
			var repl = database.CreatePushReplication(remote);
            repl.Continuous = continuous;

            //repl.CreateTarget = false; 

			// Check the replication's properties:
			Assert.AreEqual(database, repl.LocalDatabase);
			Assert.AreEqual(remote, repl.RemoteUrl);
			Assert.IsFalse(repl.IsPull);
            Assert.IsFalse(repl.Continuous);
            //Assert.IsTrue(repl.CreateTarget);
			Assert.IsNull(repl.Filter);
			Assert.IsNull(repl.FilterParams);
			// TODO: CAssertNil(r1.doc_ids);
			// TODO: CAssertNil(r1.headers);
			// Check that the replication hasn't started running:
			Assert.IsFalse(repl.IsRunning);
            Assert.AreEqual((int)repl.Status, (int)ReplicationStatus.Stopped);
			Assert.AreEqual(0, repl.CompletedChangesCount);
			Assert.AreEqual(0, repl.ChangesCount);
			Assert.IsNull(repl.LastError);
            RunReplication(repl);
			// make sure doc1 is there
			// TODO: make sure doc2 is there (refactoring needed)
            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);
            var getDocTask = Task.Factory.StartNew(()=>
                {
                    var httpclient = new HttpClient();
                    HttpResponseMessage response;
                    string responseString = null;
                    try
                    {
                        var responseTask = httpclient.GetAsync(pathToDoc.ToString());
                        responseTask.Wait(TimeSpan.FromSeconds(10));
                        response = responseTask.Result;
                        var statusLine = response.StatusCode;
                        NUnit.Framework.Assert.IsTrue(statusLine == HttpStatusCode.OK);
                        if (statusLine == HttpStatusCode.OK)
                        {
                            var responseStringTask = response.Content.ReadAsStringAsync();
                            responseStringTask.Wait(TimeSpan.FromSeconds(10));
                            responseString = responseStringTask.Result;
                            NUnit.Framework.Assert.IsTrue(responseString.Contains(doc1Id));
                            Log.D(ReplicationTest.Tag, "result: " + responseString);
                        }
                        else
                        {
                            var statusReason = response.ReasonPhrase;
                            response.Dispose();
                            throw new IOException(statusReason);
                        }
                    }
                    catch (ProtocolViolationException e)
                    {
                        NUnit.Framework.Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
                    }
                    catch (IOException e)
                    {
                        NUnit.Framework.Assert.IsNull(e, "Got IOException: " + e.Message);
                    }
                    httpRequestDoneSignal.CountDown();
                });
			//Closes the connection.
			Log.D(Tag, "Waiting for http request to finish");
			try
			{
                var result = httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10));
                Assert.IsTrue(result, "Could not retrieve the new doc from the sync gateway.");
				Log.D(Tag, "http request finished");
			}
			catch (Exception e)
			{
				Sharpen.Runtime.PrintStackTrace(e);
			}
			Log.D(Tag, "testPusher() finished");
		}
コード例 #7
0
        private void PushDocumentToSyncGateway(string docId, string docJson)
        {
            var url = new Uri(string.Format("{0}/{1}", GetReplicationURL(), docId));
            var doneSignal = new CountDownLatch(1);
            Task.Factory.StartNew(() =>
            {
                HttpClient httpclient = null;
                try
                {
                    httpclient = new HttpClient();
                    var request = new HttpRequestMessage();
                    request.Headers.Add("Accept", "*/*");
                    var postTask = httpclient.PutAsync(url.AbsoluteUri, 
                        new StringContent(docJson, Encoding.UTF8, "application/json"));
                    var response = postTask.Result;

                    Assert.IsTrue(response.StatusCode == HttpStatusCode.Created || 
                        response.StatusCode == HttpStatusCode.Conflict);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error while pusing document to sync gateway : " + e.Message);
                    throw e;
                }
                finally
                {
                    httpclient.Dispose();
                }
                doneSignal.CountDown();
            });

            var success = doneSignal.Await(TimeSpan.FromSeconds(10));
            Assert.IsTrue(success);
        }
コード例 #8
0
		public virtual void TestPusherDeletedDoc()
		{
            Assert.Fail(); // TODO.ZJG: Needs debugging, overflows stack.

			CountDownLatch replicationDoneSignal = new CountDownLatch(1);
			Uri remote = GetReplicationURL();
			string docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis());
			// Create some documentsConvert
			IDictionary<string, object> documentProperties = new Dictionary<string, object>();
			string doc1Id = string.Format("doc1-{0}", docIdTimestamp);
			documentProperties["_id"] = doc1Id;
			documentProperties["foo"] = 1;
			documentProperties["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(StatusCode.Created, status.GetCode());
            documentProperties["_rev"] = rev1.GetRevId();
			documentProperties["UPDATED"] = true;
			documentProperties["_deleted"] = true;
			RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties
				, database), rev1.GetRevId(), false, status);
            NUnit.Framework.Assert.IsTrue((int)status.GetCode() >= 200 && (int)status.GetCode() < 300);
            var repl = database.CreatePushReplication(remote);
            ((Pusher)repl).CreateTarget = true;
			RunReplication(repl);
			// make sure doc1 is deleted
			Uri replicationUrlTrailing = new Uri(string.Format("{0}/", remote.ToString()
				));
			Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id);
			Log.D(Tag, "Send http request to " + pathToDoc);
			CountDownLatch httpRequestDoneSignal = new CountDownLatch(1);
            var getDocTask = Task.Factory.StartNew(()=>
                {
                    var httpclient = new HttpClient();
                    HttpResponseMessage response;
                    string responseString = null;
                    try
                    {
                        var responseTask = httpclient.GetAsync(pathToDoc.ToString());
                        responseTask.Wait();
                        response = responseTask.Result;
                        var statusLine = response.StatusCode;
                        Log.D(ReplicationTest.Tag, "statusLine " + statusLine);
                        Assert.AreEqual(HttpStatusCode.NotFound, statusLine.GetStatusCode());                        
                    }
                    catch (ProtocolViolationException e)
                    {
                        NUnit.Framework.Assert.IsNull(e, "Got ClientProtocolException: " + e.Message);
                    }
                    catch (IOException e)
                    {
                        NUnit.Framework.Assert.IsNull(e, "Got IOException: " + e.Message);
                    }
                    finally
                    {
                        httpRequestDoneSignal.CountDown();
                    }
                });
            getDocTask.Start();
			Log.D(Tag, "Waiting for http request to finish");
			try
			{
                httpRequestDoneSignal.Await(TimeSpan.FromSeconds(10));
				Log.D(Tag, "http request finished");
			}
			catch (Exception e)
			{
				Sharpen.Runtime.PrintStackTrace(e);
			}
			Log.D(Tag, "testPusherDeletedDoc() finished");
		}
コード例 #9
0
		private CountDownLatch ReplicationWatcherThread(Replication replication)
		{
            var doneSignal = new CountDownLatch(2);
            Task.Factory.StartNew(()=>
                {
                    var started = false;
                    var done = false;

                    while (!done)
                    {
                        started |= replication.IsRunning;
                        var statusIsDone = (
                            replication.Status == ReplicationStatus.Stopped 
                            || replication.Status == ReplicationStatus.Idle
                        );
                        if (started && statusIsDone)
                        {
                            done = true;
                        }
                        try
                        {
                            Thread.Sleep(10000);
                        }
                        catch (Exception e)
                        {
                            Runtime.PrintStackTrace(e);
                        }
                    }
                    doneSignal.CountDown();
                });
            return doneSignal;
		}
        public void TestDocumentChangeListener() {
            var doc = database.CreateDocument();
            var counter = new CountDownLatch(1);
            doc.Change += (sender, e) => counter.CountDown();

            doc.CreateRevision().Save();

            var success = counter.Await(TimeSpan.FromSeconds(5));
            Assert.IsTrue(success);
        }
コード例 #11
0
ファイル: ApiTest.cs プロジェクト: Redth/couchbase-lite-net
        public void TestAsyncViewQuery()
		{
			var doneSignal = new CountDownLatch(1);
            var db = StartDatabase();

			View view = db.GetView("vu");
            view.SetMap((document, emitter) => emitter (document ["sequence"], null), "1");

            const int kNDocs = 50;
			CreateDocuments(db, kNDocs);

			var query = view.CreateQuery();
			query.StartKey=23;
			query.EndKey=33;

            var task = query.RunAsync().ContinueWith((resultTask) => 
            {
                Log.I (LiteTestCase.Tag, "Async query finished!");
                var rows = resultTask.Result;

                Assert.IsNotNull (rows);
                Assert.AreEqual (rows.Count, 11);

                var expectedKey = 23;
                for (IEnumerator<QueryRow> it = rows; it.MoveNext ();) {
                    var row = it.Current;
                    Assert.AreEqual (row.Document.Database, db);
                    Assert.AreEqual (row.Key, expectedKey);
                    ++expectedKey;
                }
                doneSignal.CountDown ();
            });

			Log.I(Tag, "Waiting for async query to finish...");

            var success = task.Wait(TimeSpan.FromSeconds(10));
			Assert.IsTrue(success, "Done signal timed out..StartKey=ry never ran");
		}
コード例 #12
0
ファイル: ApiTest.cs プロジェクト: Redth/couchbase-lite-net
        public void TestChangeTracking()
		{
            var doneSignal = new CountDownLatch(1);
            var db = StartDatabase();
            db.Changed += (sender, e) => doneSignal.CountDown();

            var task = CreateDocumentsAsync(db, 5);

			// We expect that the changes reported by the server won't be notified, because those revisions
			// are already cached in memory.
            var success = doneSignal.Await(TimeSpan.FromSeconds(10));
			Assert.IsTrue(success);
			Assert.AreEqual(5, db.GetLastSequenceNumber());

            // Give transaction time to complete.
            System.Threading.Thread.Sleep(500);
            Assert.IsTrue(task.Status.HasFlag(TaskStatus.RanToCompletion));
		}
コード例 #13
0
ファイル: ApiTest.cs プロジェクト: Redth/couchbase-lite-net
        /// <exception cref="System.Exception"></exception>
        public void RunLiveQuery(String methodNameToCall)
        {
            var db = StartDatabase();

            var doneSignal = new CountDownLatch(11); // FIXME.ZJG: Not sure why, but now Changed is only called once.

            // 11 corresponds to startKey = 23; endKey = 33
            // run a live query
            var view = db.GetView("vu");
            view.SetMap((document, emitter) => emitter (document ["sequence"], 1), "1");

            var query = view.CreateQuery().ToLiveQuery();
            query.StartKey = 23;
            query.EndKey = 33;

            Log.I(Tag, "Created  " + query);

            // these are the keys that we expect to see in the livequery change listener callback
            var expectedKeys = new HashSet<Int64>();
            for (var i = 23; i < 34; i++)
            {
                expectedKeys.AddItem(i);
            }

            // install a change listener which decrements countdown latch when it sees a new
            // key from the list of expected keys
            EventHandler<QueryChangeEventArgs> handler = (sender, e) => {
                var rows = e.Rows;
                foreach(var row in rows)
                {
                    if (expectedKeys.Contains((Int64)row.Key))
                    {
                        Console.WriteLine(Tag + " doneSignal decremented " + doneSignal.Count);
                        doneSignal.CountDown();
                    }
                }
            };

            query.Changed += handler;

            // create the docs that will cause the above change listener to decrement countdown latch
            var createTask = CreateDocumentsAsync(db, n: 50);
            createTask.Wait(TimeSpan.FromSeconds(5));
            if (methodNameToCall.Equals("start"))
            {
                // start the livequery running asynchronously
                query.Start();
            }
            else
            {
                Assert.IsNull(query.Rows);

                query.Run();

                // this will block until the query completes
                Assert.IsNotNull(query.Rows);
            }

            // wait for the doneSignal to be finished
            var success = doneSignal.Await(TimeSpan.FromSeconds(5));
            Assert.IsTrue(success, "Done signal timed out live query never ran");

            // stop the livequery since we are done with it
            query.Changed -= handler;
            query.Stop();
        }
コード例 #14
0
        public void Test30LiveQuery()
        {
            RunTest("Test30LiveQuery", (parameters) =>
            {
                var numDocs = Convert.ToInt32(parameters[NUMDOCS_KEY]);
                var docSize = Convert.ToInt32(parameters[DOCSIZE_KEY]);

                // Prepare document content
                var sb = new StringBuilder();
                for (var s = 0; s < docSize; s++)
                {
                    sb.Append("1");
                }
                var name = sb.ToString();

                var props = new Dictionary<string, object>();
                props["name"] = name;

                var doneSignal = new CountDownLatch(1);

                // Run a live query
                var view = database.GetView("vu");
                view.SetMap((document, emit) =>
                {
                    emit(document["sequence"], null);
                }, "1");

                var liveQuery = view.CreateQuery().ToLiveQuery();
                liveQuery.Changed += (sender, e) => 
                {
                    var rows = e.Rows;
                    var count = rows.Count;
                    if (count == numDocs)
                    {
                        doneSignal.CountDown();
                    }
                };
                liveQuery.Start();

                var stopwatch = Stopwatch.StartNew();

                database.RunAsync((db) => 
                {
                    database.BeginTransaction();

                    for (var i = 0; i < numDocs; i++)
                    {
                        var doc = database.CreateDocument();
                        props["sequence"] = i;
                        var rev = doc.PutProperties(props);
                        Assert.IsNotNull(rev);
                    }

                    db.EndTransaction(true);
                });

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

                stopwatch.Stop();

                return stopwatch.ElapsedMilliseconds;
            });
        }