/// <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); } }
public ChangeTracker(Uri databaseURL, ChangeTracker.ChangeTrackerMode mode, bool includeConflicts, object lastSequenceID, ChangeTrackerClient client) { this.databaseURL = databaseURL; this.mode = mode; this.includeConflicts = includeConflicts; this.lastSequenceID = lastSequenceID; this.client = client; this.requestHeaders = new Dictionary<string, object>(); }
/// <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); } }
public void ChangeTrackerFinished(ChangeTracker tracker) { ChangeTrackerCaughtUp(tracker); }
public void ChangeTrackerStopped(ChangeTracker tracker) { Log.V(ChangeTrackerTest.Tag, "changeTrackerStopped"); }
public virtual void TestChangeTrackerWithDocsIds() { Uri testURL = GetReplicationURL(); ChangeTracker changeTrackerDocIds = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode .LongPoll, 0, null); IList<string> docIds = new AList<string>(); docIds.AddItem("doc1"); docIds.AddItem("doc2"); changeTrackerDocIds.SetDocIDs(docIds); string docIdsEncoded = URLEncoder.Encode("[\"doc1\",\"doc2\"]"); string expectedFeedPath = string.Format("_changes?feed=longpoll&limit=50&heartbeat=300000&since=0&filter=_doc_ids&doc_ids=%s" , docIdsEncoded); string changesFeedPath = changeTrackerDocIds.GetChangesFeedPath(); NUnit.Framework.Assert.AreEqual(expectedFeedPath, changesFeedPath); }
public void ChangeTrackerStopped(ChangeTracker tracker) { if (StoppedDelegate != null) { StoppedDelegate(tracker); } if (stoppedSignal != null) { stoppedSignal.CountDown(); } }
public void TestChangeTrackerWithFilterURL() { var testUrl = GetReplicationURL(); var changeTracker = new ChangeTracker(testUrl, ChangeTrackerMode.LongPoll, 0, false, null); // set filter changeTracker.SetFilterName("filter"); // build filter map var filterMap = new Dictionary<string, object>(); filterMap["param"] = "value"; // set filter map changeTracker.SetFilterParams(filterMap); Assert.AreEqual("_changes?feed=longpoll&limit=50&heartbeat=300000&since=0&filter=filter¶m=value", changeTracker.GetChangesFeedPath()); }
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); }
public void ChangeTrackerStopped(ChangeTracker tracker) { changeTrackerFinishedSignal.CountDown(); }
public _BackgroundTask_311(ChangeTracker changeTracker) { this.changeTracker = changeTracker; }
public void TestChangeTrackerWithDocsIds() { var testURL = GetReplicationURL(); var changeTracker = new ChangeTracker(testURL, ChangeTrackerMode .LongPoll, 0, false, null); var docIds = new List<string>(); docIds.Add("doc1"); docIds.Add("doc2"); changeTracker.SetDocIDs(docIds); var docIdsJson = "[\"doc1\",\"doc2\"]"; var parameters = changeTracker.GetChangesFeedParams(); Assert.AreEqual("_doc_ids", parameters["filter"]); AssertEnumerablesAreEqual(docIds, (IEnumerable)parameters["doc_ids"]); var body = changeTracker.GetChangesFeedPostBody(); Assert.IsTrue(body.Contains(docIdsJson)); }
/// <exception cref="System.Exception"></exception> private void RunChangeTrackerTransientError(ChangeTracker.ChangeTrackerMode mode, int errorCode, string statusMessage, int numExpectedChangeCallbacks) { CountDownLatch changeTrackerFinishedSignal = new CountDownLatch(1); CountDownLatch changeReceivedSignal = new CountDownLatch(numExpectedChangeCallbacks ); Uri testURL = GetReplicationURL(); ChangeTrackerClient client = new _ChangeTrackerClient_197(changeTrackerFinishedSignal , changeReceivedSignal, errorCode, statusMessage); ChangeTracker changeTracker = new ChangeTracker(testURL, mode, false, 0, client); changeTracker.SetUsePOST(IsTestingAgainstSyncGateway()); changeTracker.Start(); try { bool success = changeReceivedSignal.Await(30, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } changeTracker.Stop(); try { bool success = changeTrackerFinishedSignal.Await(30, TimeUnit.Seconds); NUnit.Framework.Assert.IsTrue(success); } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } }
public virtual void TestChangeTrackerWithDocsIds() { Uri testURL = GetReplicationURL(); ChangeTracker changeTrackerDocIds = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode .LongPoll, false, 0, null); IList<string> docIds = new AList<string>(); docIds.AddItem("doc1"); docIds.AddItem("doc2"); changeTrackerDocIds.SetDocIDs(docIds); string docIdsUnencoded = "[\"doc1\",\"doc2\"]"; string docIdsEncoded = URLEncoder.Encode(docIdsUnencoded); string expectedFeedPath = string.Format("_changes?feed=longpoll&limit=50&heartbeat=300000&since=0&filter=_doc_ids&doc_ids=%s" , docIdsEncoded); string changesFeedPath = changeTrackerDocIds.GetChangesFeedPath(); NUnit.Framework.Assert.AreEqual(expectedFeedPath, changesFeedPath); changeTrackerDocIds.SetUsePOST(true); IDictionary<string, object> postBodyMap = changeTrackerDocIds.ChangesFeedPOSTBodyMap (); NUnit.Framework.Assert.AreEqual("_doc_ids", postBodyMap.Get("filter")); NUnit.Framework.Assert.AreEqual(docIds, postBodyMap.Get("doc_ids")); string postBody = changeTrackerDocIds.ChangesFeedPOSTBody(); NUnit.Framework.Assert.IsTrue(postBody.Contains(docIdsUnencoded)); }
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); }
public ChangeTracker(Uri databaseURL, ChangeTracker.ChangeTrackerMode mode, bool includeConflicts, object lastSequenceID, ChangeTrackerClient client) { // does not work, do not use it. this.databaseURL = databaseURL; this.mode = mode; this.includeConflicts = includeConflicts; this.lastSequenceID = lastSequenceID; this.client = client; this.requestHeaders = new Dictionary<string, object>(); this.heartBeatSeconds = 300; this.limit = 50; }
public void TestChangeTrackerWithConflictsIncluded() { Uri testUrl = GetReplicationURL(); var changeTracker = new ChangeTracker(testUrl, ChangeTrackerMode.LongPoll, 0, true, null); Assert.AreEqual("_changes?feed=longpoll&limit=50&heartbeat=300000&style=all_docs&since=0", changeTracker.GetChangesFeedPath()); }
/// <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); } }
public void TestChangeTrackerWithDocsIds() { var testURL = GetReplicationURL(); var changeTracker = new ChangeTracker(testURL, ChangeTrackerMode .LongPoll, 0, false, null); var docIds = new List<string>(); docIds.AddItem("doc1"); docIds.AddItem("doc2"); changeTracker.SetDocIDs(docIds); var docIdsJson = "[\"doc1\",\"doc2\"]"; var docIdsEncoded = Uri.EscapeUriString(docIdsJson); var expectedFeedPath = string.Format("_changes?feed=longpoll&limit=50&heartbeat=300000&since=0&filter=_doc_ids&doc_ids={0}", docIdsEncoded); string changesFeedPath = changeTracker.GetChangesFeedPath(); Assert.AreEqual(expectedFeedPath, changesFeedPath); changeTracker.UsePost = true; var parameters = changeTracker.GetChangesFeedParams(); Assert.AreEqual("_doc_ids", parameters["filter"]); AssertEnumerablesAreEqual(docIds, (IEnumerable)parameters["doc_ids"]); var body = changeTracker.GetChangesFeedPostBody(); Assert.IsTrue(body.Contains(docIdsJson)); }
public virtual void TestChangeTrackerWithConflictsIncluded() { Uri testURL = GetReplicationURL(); ChangeTracker changeTracker = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode .LongPoll, true, 0, null); NUnit.Framework.Assert.AreEqual("_changes?feed=longpoll&limit=50&heartbeat=300000&style=all_docs&since=0" , changeTracker.GetChangesFeedPath()); }
/// <exception cref="System.Exception"></exception> public virtual void TestChangeTrackerWithFilterURL() { Uri testURL = GetReplicationURL(); ChangeTracker changeTracker = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode .LongPoll, 0, null); // set filter changeTracker.SetFilterName("filter"); // build filter map IDictionary<string, object> filterMap = new Dictionary<string, object>(); filterMap.Put("param", "value"); // set filter map changeTracker.SetFilterParams(filterMap); NUnit.Framework.Assert.AreEqual("_changes?feed=longpoll&limit=50&heartbeat=300000&since=0&filter=filter¶m=value" , changeTracker.GetChangesFeedPath()); }
/// <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); } }
/// <exception cref="System.Exception"></exception> public virtual void TestChangeTrackerInvalidJson() { Uri testURL = GetReplicationURL(); CustomizableMockHttpClient mockHttpClient = new CustomizableMockHttpClient(); mockHttpClient.AddResponderThrowExceptionAllRequests(); ChangeTrackerClient client = new _ChangeTrackerClient_290(mockHttpClient); ChangeTracker changeTracker = new ChangeTracker(testURL, ChangeTracker.ChangeTrackerMode .LongPoll, 0, client); BackgroundTask task = new _BackgroundTask_311(changeTracker); task.Execute(); try { // expected behavior: // when: // mockHttpClient throws IOExceptions -> it should start high and then back off and numTimesExecute should be low for (int i = 0; i < 30; i++) { int numTimesExectutedAfter10seconds = 0; try { Sharpen.Thread.Sleep(1000); // take a snapshot of num times the http client was called after 10 seconds if (i == 10) { numTimesExectutedAfter10seconds = mockHttpClient.GetCapturedRequests().Count; } // take another snapshot after 20 seconds have passed if (i == 20) { // by now it should have backed off, so the delta between 10s and 20s should be small int delta = mockHttpClient.GetCapturedRequests().Count - numTimesExectutedAfter10seconds; NUnit.Framework.Assert.IsTrue(delta < 25); } } catch (Exception e) { Sharpen.Runtime.PrintStackTrace(e); } } } finally { changeTracker.Stop(); } }
public void ChangeTrackerStopped(ChangeTracker tracker) { Log.V(ChangeTrackerTest.Tag, "changeTrackerStopped"); changeTrackerFinishedSignal.CountDown(); }
public void ChangeTrackerStopped(ChangeTracker tracker, ErrorResolution resolution) { WorkExecutor.StartNew(() => ProcessChangeTrackerStopped(tracker, resolution)); }
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); }
public void ChangeTrackerStopped(ChangeTracker tracker) { WorkExecutor.StartNew(() => ProcessChangeTrackerStopped(tracker)); }
public ChangeTracker(Uri databaseURL, ChangeTracker.ChangeTrackerMode mode, object lastSequenceID, Boolean includeConflicts, IChangeTrackerClient client) { this.databaseURL = databaseURL; this.mode = mode; this.includeConflicts = includeConflicts; this.lastSequenceID = lastSequenceID; this.client = client; this.RequestHeaders = new Dictionary<string, object>(); this.tokenSource = new CancellationTokenSource(); }