/// <exception cref="System.Exception"></exception> public virtual void ChangeTrackerTestWithMode(ChangeTracker.ChangeTrackerMode mode , bool useMockReplicator) { CountDownLatch changeTrackerFinishedSignal = new CountDownLatch(1); CountDownLatch changeReceivedSignal = new CountDownLatch(1); Uri testURL = GetReplicationURL(); ChangeTrackerClient client = new _ChangeTrackerClient_42(changeTrackerFinishedSignal , useMockReplicator, changeReceivedSignal); ChangeTracker changeTracker = new ChangeTracker(testURL, mode, false, 0, client); changeTracker.SetUsePOST(IsTestingAgainstSyncGateway()); changeTracker.Start(); try { bool success = changeReceivedSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } changeTracker.Stop(); try { bool success = changeTrackerFinishedSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
/// <exception cref="System.Exception"></exception> public virtual void TestChangeTracker() { CountDownLatch changeTrackerFinishedSignal = new CountDownLatch(1); Uri testURL = GetReplicationURL(); ChangeTrackerClient client = new _ChangeTrackerClient_31(changeTrackerFinishedSignal ); ChangeTracker changeTracker = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode .OneShot, 0, client); changeTracker.Start(); try { bool success = changeTrackerFinishedSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
private static void Await(CountDownLatch cdl) { try { NUnit.Framework.Assert.IsTrue(cdl.Await(1000, TimeUnit.MILLISECONDS), "latch released" ); } catch (Exception) { NUnit.Framework.Assert.Fail("Did not expect to be interrupted"); } }
//CHANGE TRACKING /// <exception cref="System.Exception"></exception> public virtual void TestChangeTracking() { CountDownLatch doneSignal = new CountDownLatch(1); Database db = StartDatabase(); db.AddChangeListener(new _ChangeListener_579(doneSignal)); CreateDocumentsAsync(db, 5); // We expect that the changes reported by the server won't be notified, because those revisions // are already cached in memory. bool success = doneSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); NUnit.Framework.Assert.AreEqual(5, db.GetLastSequenceNumber()); }
/// <exception cref="System.Exception"></exception> public virtual void TestPusher() { CountDownLatch replicationDoneSignal = new CountDownLatch(1); Uri remote = GetReplicationURL(); string docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis()); // Create some documents: IDictionary<string, object> documentProperties = new Dictionary<string, object>(); string doc1Id = string.Format("doc1-%s", docIdTimestamp); documentProperties.Put("_id", doc1Id); documentProperties.Put("foo", 1); documentProperties.Put("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(Status.Created, status.GetCode()); documentProperties.Put("_rev", rev1.GetRevId()); documentProperties.Put("UPDATED", true); RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties , database), rev1.GetRevId(), false, status); NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode()); documentProperties = new Dictionary<string, object>(); string doc2Id = string.Format("doc2-%s", docIdTimestamp); documentProperties.Put("_id", doc2Id); documentProperties.Put("baz", 666); documentProperties.Put("fnord", true); database.PutRevision(new RevisionInternal(documentProperties, database), null, false , status); NUnit.Framework.Assert.AreEqual(Status.Created, status.GetCode()); bool continuous = false; Replication repl = database.CreatePushReplication(remote); repl.SetContinuous(continuous); repl.SetCreateTarget(true); // Check the replication's properties: NUnit.Framework.Assert.AreEqual(database, repl.GetLocalDatabase()); NUnit.Framework.Assert.AreEqual(remote, repl.GetRemoteUrl()); NUnit.Framework.Assert.IsFalse(repl.IsPull()); NUnit.Framework.Assert.IsFalse(repl.IsContinuous()); NUnit.Framework.Assert.IsTrue(repl.ShouldCreateTarget()); NUnit.Framework.Assert.IsNull(repl.GetFilter()); NUnit.Framework.Assert.IsNull(repl.GetFilterParams()); // TODO: CAssertNil(r1.doc_ids); // TODO: CAssertNil(r1.headers); // Check that the replication hasn't started running: NUnit.Framework.Assert.IsFalse(repl.IsRunning()); NUnit.Framework.Assert.AreEqual(Replication.ReplicationStatus.ReplicationStopped, repl.GetStatus()); NUnit.Framework.Assert.AreEqual(0, repl.GetCompletedChangesCount()); NUnit.Framework.Assert.AreEqual(0, repl.GetChangesCount()); NUnit.Framework.Assert.IsNull(repl.GetLastError()); RunReplication(repl); // make sure doc1 is there // TODO: make sure doc2 is there (refactoring needed) Uri replicationUrlTrailing = new Uri(string.Format("%s/", remote.ToExternalForm() )); Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id); Log.D(Tag, "Send http request to " + pathToDoc); CountDownLatch httpRequestDoneSignal = new CountDownLatch(1); BackgroundTask getDocTask = new _BackgroundTask_122(pathToDoc, doc1Id, httpRequestDoneSignal ); //Closes the connection. getDocTask.Execute(); Log.D(Tag, "Waiting for http request to finish"); try { httpRequestDoneSignal.Await(300, TimeUnit.Seconds); Log.D(Tag, "http request finished"); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } Log.D(Tag, "testPusher() finished"); }
// kick something off that will s /// <exception cref="System.Exception"></exception> public virtual void RunLiveQuery(string methodNameToCall) { Database db = StartDatabase(); CountDownLatch doneSignal = new CountDownLatch(11); // 11 corresponds to startKey=23; endKey=33 // run a live query View view = db.GetView("vu"); view.SetMap(new _Mapper_817(), "1"); LiveQuery query = view.CreateQuery().ToLiveQuery(); query.SetStartKey(23); query.SetEndKey(33); Log.I(Tag, "Created " + query); // these are the keys that we expect to see in the livequery change listener callback ICollection<int> expectedKeys = new HashSet<int>(); for (int 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 LiveQuery.ChangeListener changeListener = new _ChangeListener_836(expectedKeys, doneSignal ); query.AddChangeListener(changeListener); // create the docs that will cause the above change listener to decrement countdown latch int kNDocs = 50; CreateDocumentsAsync(db, kNDocs); if (methodNameToCall.Equals("start")) { // start the livequery running asynchronously query.Start(); } else { if (methodNameToCall.Equals("startWaitForRows")) { query.Start(); query.WaitForRows(); } else { NUnit.Framework.Assert.IsNull(query.GetRows()); query.Run(); // this will block until the query completes NUnit.Framework.Assert.IsNotNull(query.GetRows()); } } // wait for the doneSignal to be finished bool success = doneSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue("Done signal timed out, live query never ran", success ); // stop the livequery since we are done with it query.RemoveChangeListener(changeListener); query.Stop(); }
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); }
private void ChangeTrackerTestWithMode(ChangeTrackerMode mode) { var changeTrackerFinishedSignal = new CountDownLatch(1); var changeReceivedSignal = new CountDownLatch(1); var client = new ChangeTrackerTestClient(changeTrackerFinishedSignal, changeReceivedSignal); client.ReceivedChangeDelegate = (IDictionary<string, object> change) => { Assert.IsTrue(change.ContainsKey("seq")); Assert.AreEqual("1", change["seq"]); }; var handler = client.HttpRequestHandler; handler.SetResponder("_changes", (request) => { var json = "{\"results\":[\n" + "{\"seq\":\"1\",\"id\":\"doc1-138\",\"changes\":[{\"rev\":\"1-82d\"}]}],\n" + "\"last_seq\":\"*:50\"}"; return MockHttpRequestHandler.GenerateHttpResponseMessage(HttpStatusCode.OK, null, json); }); var testUrl = GetReplicationURL(); var scheduler = new SingleTaskThreadpoolScheduler(); var changeTracker = new ChangeTracker(testUrl, mode, 0, false, client, new TaskFactory(scheduler)); changeTracker.UsePost = IsSyncGateway(testUrl); changeTracker.Start(); var success = changeReceivedSignal.Await(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); changeTracker.Stop(); success = changeTrackerFinishedSignal.Await(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); }
private void RunChangeTrackerTransientError( ChangeTrackerMode mode, Int32 errorCode, string statusMessage, Int32 numExpectedChangeCallbacks) { var changeTrackerFinishedSignal = new CountDownLatch(1); var changeReceivedSignal = new CountDownLatch(numExpectedChangeCallbacks); var client = new ChangeTrackerTestClient(changeTrackerFinishedSignal, changeReceivedSignal); MockHttpRequestHandler.HttpResponseDelegate sentinal = RunChangeTrackerTransientErrorDefaultResponder(); var responders = new List<MockHttpRequestHandler.HttpResponseDelegate>(); responders.Add(RunChangeTrackerTransientErrorDefaultResponder()); 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); }; var handler = client.HttpRequestHandler; handler.SetResponder("_changes", chainResponder); var testUrl = GetReplicationURL(); var scheduler = new SingleTaskThreadpoolScheduler(); var changeTracker = new ChangeTracker(testUrl, mode, 0, false, client, new TaskFactory(scheduler)); changeTracker.UsePost = IsSyncGateway(testUrl); changeTracker.Start(); var success = changeReceivedSignal.Await(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); changeTracker.Stop(); success = changeTrackerFinishedSignal.Await(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); }
/// <exception cref="System.Exception"></exception> public virtual void ChangeTrackerTestWithMode(ChangeTracker.ChangeTrackerMode mode ) { CountDownLatch changeTrackerFinishedSignal = new CountDownLatch(1); CountDownLatch changeReceivedSignal = new CountDownLatch(1); Uri testURL = GetReplicationURL(); IChangeTrackerClient client = new _ChangeTrackerClient_119(changeTrackerFinishedSignal , changeReceivedSignal); ChangeTracker changeTracker = new ChangeTracker(testURL, mode, 0, client); changeTracker.Start(); try { bool success = changeReceivedSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } changeTracker.Stop(); try { bool success = changeTrackerFinishedSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
/// <exception cref="System.Exception"></exception> private void TestChangeTrackerBackoff(CustomizableMockHttpClient mockHttpClient) { Uri testURL = GetReplicationURL(); CountDownLatch changeTrackerFinishedSignal = new CountDownLatch(1); ChangeTrackerClient client = new _ChangeTrackerClient_234(changeTrackerFinishedSignal , mockHttpClient); ChangeTracker changeTracker = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode .LongPoll, false, 0, client); changeTracker.Start(); // sleep for a few seconds Sharpen.Thread.Sleep(5 * 1000); // make sure we got less than 10 requests in those 10 seconds (if it was hammering, we'd get a lot more) NUnit.Framework.Assert.IsTrue(mockHttpClient.GetCapturedRequests().Count < 25); NUnit.Framework.Assert.IsTrue(changeTracker.backoff.GetNumAttempts() > 0); mockHttpClient.ClearResponders(); mockHttpClient.AddResponderReturnEmptyChangesFeed(); // at this point, the change tracker backoff should cause it to sleep for about 3 seconds // and so lets wait 3 seconds until it wakes up and starts getting valid responses Sharpen.Thread.Sleep(3 * 1000); // now find the delta in requests received in a 2s period int before = mockHttpClient.GetCapturedRequests().Count; Sharpen.Thread.Sleep(2 * 1000); int after = mockHttpClient.GetCapturedRequests().Count; // assert that the delta is high, because at this point the change tracker should // be hammering away NUnit.Framework.Assert.IsTrue((after - before) > 25); // the backoff numAttempts should have been reset to 0 NUnit.Framework.Assert.IsTrue(changeTracker.backoff.GetNumAttempts() == 0); changeTracker.Stop(); try { bool success = changeTrackerFinishedSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
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"); }
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"); }
/// <exception cref="System.UriFormatException"></exception> private void PushDocumentToSyncGateway(string docId, string docJson) { // push a document to server Uri replicationUrlTrailingDoc1 = new Uri(string.Format("%s/%s", GetReplicationURL ().ToExternalForm(), docId)); Uri pathToDoc1 = new Uri(replicationUrlTrailingDoc1, docId); Log.D(Tag, "Send http request to " + pathToDoc1); CountDownLatch httpRequestDoneSignal = new CountDownLatch(1); BackgroundTask getDocTask = new _BackgroundTask_139(pathToDoc1, docJson, httpRequestDoneSignal ); getDocTask.Execute(); Log.D(Tag, "Waiting for http request to finish"); try { httpRequestDoneSignal.Await(300, TimeUnit.Seconds); Log.D(Tag, "http request finished"); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
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); }
/// <exception cref="System.Exception"></exception> public virtual void TestAsyncViewQuery() { CountDownLatch doneSignal = new CountDownLatch(1); Database db = StartDatabase(); View view = db.GetView("vu"); view.SetMap(new _Mapper_883(), "1"); int kNDocs = 50; CreateDocuments(db, kNDocs); Query query = view.CreateQuery(); query.SetStartKey(23); query.SetEndKey(33); query.RunAsync(new _QueryCompleteListener_897(db, doneSignal)); Log.I(Tag, "Waiting for async query to finish..."); bool success = doneSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue("Done signal timed out, async query never ran", success ); }
public void TestBatcherLatencyTrickleIn() { doneSignal = new CountDownLatch(10); inboxCapacity = 100; processorDelay = 500; maxObservedDelta = -1L; var scheduler = new SingleThreadTaskScheduler(); var batcher = new Batcher<long>(new TaskFactory(scheduler), inboxCapacity, processorDelay, TestBatcherLatencyTrickleInProcessor); for (var i = 0; i < 10; i++) { var objectsToQueue = new List<long>(); objectsToQueue.Add(Runtime.CurrentTimeMillis()); batcher.QueueObjects(objectsToQueue); System.Threading.Thread.Sleep(1000); } var success = doneSignal.Await(TimeSpan.FromSeconds(35)); Assert.IsTrue(success); // we want the max observed delta between the time it was queued until the // time it was processed to be as small as possible. since // there is some overhead, rather than using a hardcoded number // express it as a ratio of 1/4th the processor delay, asserting // that the entire processor delay never kicked in. int acceptableMaxDelta = processorDelay - 1; Log.V(Tag, string.Format("TestBatcherLatencyTrickleIn : maxObservedDelta: {0}", maxObservedDelta)); Assert.IsTrue((maxObservedDelta < acceptableMaxDelta)); }
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); }
public void TestBatcherBatchSize5() { doneSignal = new CountDownLatch(10); inboxCapacity = 10; processorDelay = 1000; var scheduler = new SingleThreadTaskScheduler(); var batcher = new Batcher<string>(new TaskFactory(scheduler), inboxCapacity, processorDelay, TestBatcherBatchSize5Processor); var objectsToQueue = new List<string>(); for (var i = 0; i < inboxCapacity * 10; i++) { objectsToQueue.Add(i.ToString()); if (objectsToQueue.Count == 5) { batcher.QueueObjects(objectsToQueue); objectsToQueue.Clear(); } } var success = doneSignal.Await(TimeSpan.FromSeconds(35)); Assert.IsTrue(success); }
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); }
private void TestChangeTrackerBackoff(MockHttpClientFactory httpClientFactory) { var changeTrackerFinishedSignal = new CountDownLatch(1); var client = new ChangeTrackerTestClient(changeTrackerFinishedSignal, null); client.HttpClientFactory = httpClientFactory; var testUrl = GetReplicationURL(); var scheduler = new SingleTaskThreadpoolScheduler(); var changeTracker = new ChangeTracker(testUrl, ChangeTrackerMode.LongPoll, 0, false, client, new TaskFactory(scheduler)); changeTracker.UsePost = IsSyncGateway(testUrl); changeTracker.Start(); // sleep for a few seconds Thread.Sleep(15 * 1000); // make sure we got less than 10 requests in those 10 seconds (if it was hammering, we'd get a lot more) var handler = client.HttpRequestHandler; Assert.IsTrue(handler.CapturedRequests.Count < 25); Assert.IsTrue(changeTracker.backoff.NumAttempts > 0, "Observed attempts: {0}".Fmt(changeTracker.backoff.NumAttempts)); handler.ClearResponders(); handler.AddResponderReturnEmptyChangesFeed(); // at this point, the change tracker backoff should cause it to sleep for about 3 seconds // and so lets wait 3 seconds until it wakes up and starts getting valid responses Thread.Sleep(3 * 1000); // now find the delta in requests received in a 2s period int before = handler.CapturedRequests.Count; Thread.Sleep(2 * 1000); int after = handler.CapturedRequests.Count; // assert that the delta is high, because at this point the change tracker should // be hammering away Assert.IsTrue((after - before) > 25); // the backoff numAttempts should have been reset to 0 Assert.IsTrue(changeTracker.backoff.NumAttempts == 0); changeTracker.Stop(); var success = changeTrackerFinishedSignal.Await(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); }
/// <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(); }
/// <exception cref="System.Exception"></exception> public virtual void TestPusherDeletedDoc() { CountDownLatch replicationDoneSignal = new CountDownLatch(1); Uri remote = GetReplicationURL(); string docIdTimestamp = System.Convert.ToString(Runtime.CurrentTimeMillis()); // Create some documents: IDictionary<string, object> documentProperties = new Dictionary<string, object>(); string doc1Id = string.Format("doc1-%s", docIdTimestamp); documentProperties.Put("_id", doc1Id); documentProperties.Put("foo", 1); documentProperties.Put("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(Status.Created, status.GetCode()); documentProperties.Put("_rev", rev1.GetRevId()); documentProperties.Put("UPDATED", true); documentProperties.Put("_deleted", true); RevisionInternal rev2 = database.PutRevision(new RevisionInternal(documentProperties , database), rev1.GetRevId(), false, status); NUnit.Framework.Assert.IsTrue(status.GetCode() >= 200 && status.GetCode() < 300); Replication repl = database.CreatePushReplication(remote); ((Pusher)repl).SetCreateTarget(true); RunReplication(repl); // make sure doc1 is deleted Uri replicationUrlTrailing = new Uri(string.Format("%s/", remote.ToExternalForm() )); Uri pathToDoc = new Uri(replicationUrlTrailing, doc1Id); Log.D(Tag, "Send http request to " + pathToDoc); CountDownLatch httpRequestDoneSignal = new CountDownLatch(1); BackgroundTask getDocTask = new _BackgroundTask_216(pathToDoc, httpRequestDoneSignal ); getDocTask.Execute(); Log.D(Tag, "Waiting for http request to finish"); try { httpRequestDoneSignal.Await(300, TimeUnit.Seconds); Log.D(Tag, "http request finished"); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } Log.D(Tag, "testPusherDeletedDoc() finished"); }
/// <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()); }
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; }
/// <exception cref="System.IO.IOException"></exception> private void AddDocWithId(string docId, string attachmentName) { string docJson; if (attachmentName != null) { // add attachment to document InputStream attachmentStream = GetAsset(attachmentName); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.Copy(attachmentStream, baos); string attachmentBase64 = Base64.EncodeBytes(baos.ToByteArray()); docJson = string.Format("{\"foo\":1,\"bar\":false, \"_attachments\": { \"i_use_couchdb.png\": { \"content_type\": \"image/png\", \"data\": \"%s\" } } }" , attachmentBase64); } else { docJson = "{\"foo\":1,\"bar\":false}"; } // push a document to server Uri replicationUrlTrailingDoc1 = new Uri(string.Format("%s/%s", GetReplicationURL ().ToExternalForm(), docId)); Uri pathToDoc1 = new Uri(replicationUrlTrailingDoc1, docId); Log.D(Tag, "Send http request to " + pathToDoc1); CountDownLatch httpRequestDoneSignal = new CountDownLatch(1); BackgroundTask getDocTask = new _BackgroundTask_376(pathToDoc1, docJson, httpRequestDoneSignal ); getDocTask.Execute(); Log.D(Tag, "Waiting for http request to finish"); try { httpRequestDoneSignal.Await(300, TimeUnit.Seconds); Log.D(Tag, "http request finished"); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
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."); }
private void RunReplication(Replication replication) { CountDownLatch replicationDoneSignal = new CountDownLatch(1); ReplicationTest.ReplicationObserver replicationObserver = new ReplicationTest.ReplicationObserver (this, replicationDoneSignal); replication.AddChangeListener(replicationObserver); replication.Start(); CountDownLatch replicationDoneSignalPolling = ReplicationWatcherThread(replication ); Log.D(Tag, "Waiting for replicator to finish"); try { bool success = replicationDoneSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); success = replicationDoneSignalPolling.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); Log.D(Tag, "replicator finished"); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
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); } } } }
/// <exception cref="System.Exception"></exception> public virtual void TestFetchRemoteCheckpointDoc() { HttpClientFactory mockHttpClientFactory = new _HttpClientFactory_583(); Log.D("TEST", "testFetchRemoteCheckpointDoc() called"); string dbUrlString = "http://fake.test-url.com:4984/fake/"; Uri remote = new Uri(dbUrlString); database.SetLastSequence("1", remote, true); // otherwise fetchRemoteCheckpoint won't contact remote Replication replicator = new Pusher(database, remote, false, mockHttpClientFactory , manager.GetWorkExecutor()); CountDownLatch doneSignal = new CountDownLatch(1); ReplicationTest.ReplicationObserver replicationObserver = new ReplicationTest.ReplicationObserver (this, doneSignal); replicator.AddChangeListener(replicationObserver); replicator.FetchRemoteCheckpointDoc(); Log.D(Tag, "testFetchRemoteCheckpointDoc() Waiting for replicator to finish"); try { bool succeeded = doneSignal.Await(300, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(succeeded); Log.D(Tag, "testFetchRemoteCheckpointDoc() replicator finished"); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } string errorMessage = "Since we are passing in a mock http client that always throws " + "errors, we expect the replicator to be in an error state"; NUnit.Framework.Assert.IsNotNull(errorMessage, replicator.GetLastError()); }