public void TestChangeTrackerWithDocsIds() { var testURL = GetReplicationURL(); var remoteSession = new RemoteSession(new RemoteSessionContructorOptions { BaseUrl = testURL }); remoteSession.SetupHttpClientFactory(null, database, "docids"); remoteSession.Setup(new ReplicationOptions()); var changeTracker = ChangeTrackerFactory.Create(new ChangeTrackerOptions { DatabaseUri = testURL, Mode = ChangeTrackerMode.LongPoll, IncludeConflicts = false, Client = new ChangeTrackerTestClient(null, null), RetryStrategy = new ExponentialBackoffStrategy(2), RemoteSession = remoteSession }); var docIds = new List <string>(); docIds.Add("doc1"); docIds.Add("doc2"); changeTracker.DocIDs = docIds; var parameters = changeTracker.GetChangesFeedParams(); Assert.AreEqual("_doc_ids", parameters["filter"]); AssertEnumerablesAreEqual(docIds, (IEnumerable)parameters["doc_ids"]); }
public void TestWebSocketChangeTrackerFallback() { if (!Boolean.Parse((string)GetProperty("replicationTestsEnabled"))) { Assert.Inconclusive("Replication tests disabled."); return; } var signal = new CountdownEvent(1); var client = new ChangeTrackerTestClient(signal, null); using (var remoteDb = _sg.CreateDatabase("web_socket_scratch")) { var remoteSession = new RemoteSession(new RemoteSessionContructorOptions { BaseUrl = remoteDb.RemoteUri }); remoteSession.SetupHttpClientFactory(null, database, "live"); remoteSession.Setup(new ReplicationOptions()); var tracker = new BadWebSocketChangeTracker(new ChangeTrackerOptions { DatabaseUri = remoteDb.RemoteUri, IncludeConflicts = false, Client = client, RetryStrategy = new ExponentialBackoffStrategy(2), RemoteSession = remoteSession }); tracker.Start(); Assert.IsTrue(signal.Wait(TimeSpan.FromSeconds(20))); Assert.IsFalse(tracker.CanConnect); } }
public void TestChangeTrackerGiveUp() { var factory = new MockHttpClientFactory(); var httpHandler = (MockHttpRequestHandler)factory.HttpHandler; httpHandler.AddResponderThrowExceptionAllRequests(); var changeTrackerFinishedSignal = new CountdownEvent(1); var client = new ChangeTrackerTestClient(changeTrackerFinishedSignal, null); client.HttpClientFactory = factory; var testUrl = GetReplicationURL(); var scheduler = new SingleTaskThreadpoolScheduler(); var remoteSession = new RemoteSession(new RemoteSessionContructorOptions { BaseUrl = testUrl, WorkExecutor = new TaskFactory(scheduler) }); remoteSession.SetupHttpClientFactory(client.HttpClientFactory, database, "giveup"); remoteSession.Setup(new ReplicationOptions()); var changeTracker = ChangeTrackerFactory.Create(new ChangeTrackerOptions { DatabaseUri = testUrl, Mode = ChangeTrackerMode.OneShot, IncludeConflicts = true, Client = client, RetryStrategy = new ExponentialBackoffStrategy(2), WorkExecutor = new TaskFactory(scheduler), RemoteSession = remoteSession }); changeTracker.Start(); Assert.IsTrue(changeTrackerFinishedSignal.Wait(TimeSpan.FromSeconds(30))); }
private void ChangeTrackerTestWithMode(ChangeTrackerMode mode) { var changeTrackerFinishedSignal = new CountdownEvent(1); var changeReceivedSignal = new CountdownEvent(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(System.Net.HttpStatusCode.OK, null, json)); }); var testUrl = GetReplicationURL(); var scheduler = new SingleTaskThreadpoolScheduler(); var remoteSession = new RemoteSession(new RemoteSessionContructorOptions { WorkExecutor = new TaskFactory(scheduler), BaseUrl = testUrl }); remoteSession.SetupHttpClientFactory(client.HttpClientFactory, database, "testwithmode"); remoteSession.Setup(new ReplicationOptions()); var changeTracker = ChangeTrackerFactory.Create(new ChangeTrackerOptions { DatabaseUri = testUrl, Mode = mode, IncludeConflicts = false, Client = client, RetryStrategy = new ExponentialBackoffStrategy(2), WorkExecutor = new TaskFactory(scheduler), RemoteSession = remoteSession }); changeTracker.ActiveOnly = true; changeTracker.Start(); var success = changeReceivedSignal.Wait(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); changeTracker.Stop(); success = changeTrackerFinishedSignal.Wait(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); }
public void TestLiveChangeTrackerLongPoll() { TestLiveChangeTracker((uri, client) => { var remoteSession = new RemoteSession(new RemoteSessionContructorOptions { BaseUrl = uri }); remoteSession.SetupHttpClientFactory(null, database, "live"); remoteSession.Setup(new ReplicationOptions()); var tracker = ChangeTrackerFactory.Create(new ChangeTrackerOptions { DatabaseUri = uri, Mode = ChangeTrackerMode.OneShot, IncludeConflicts = false, Client = client, RetryStrategy = new ExponentialBackoffStrategy(2), RemoteSession = remoteSession }); tracker.Continuous = true; return(tracker); }, true); }
public void TestLiveChangeTrackerOneShot(bool continuous) { TestLiveChangeTracker((uri, client) => { var remoteSession = new RemoteSession(new RemoteSessionContructorOptions { BaseUrl = uri }); remoteSession.SetupHttpClientFactory(null, database); remoteSession.Setup(new ReplicationOptions()); var tracker = ChangeTrackerFactory.Create(new ChangeTrackerOptions { DatabaseUri = uri, Mode = ChangeTrackerMode.OneShot, IncludeConflicts = false, Client = client, RetryStrategy = new ExponentialBackoffStrategy(2), RemoteSession = remoteSession }); tracker.Continuous = continuous; tracker.PollInterval = TimeSpan.FromSeconds(5); return(tracker); }, continuous); }
private void RunChangeTrackerTransientError( ChangeTrackerMode mode, Int32 errorCode, string statusMessage, Int32 numExpectedChangeCallbacks) { var changeTrackerFinishedSignal = new CountdownEvent(1); var changeReceivedSignal = new CountdownEvent(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 remoteSession = new RemoteSession(new RemoteSessionContructorOptions { WorkExecutor = new TaskFactory(scheduler), BaseUrl = testUrl }); remoteSession.SetupHttpClientFactory(client.HttpClientFactory, database, "bar"); remoteSession.Setup(new ReplicationOptions()); var changeTracker = ChangeTrackerFactory.Create(new ChangeTrackerOptions { DatabaseUri = testUrl, Mode = mode, IncludeConflicts = false, Client = client, RetryStrategy = new ExponentialBackoffStrategy(2), WorkExecutor = new TaskFactory(scheduler), RemoteSession = remoteSession }); changeTracker.Start(); var success = changeReceivedSignal.Wait(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); changeTracker.Stop(); success = changeTrackerFinishedSignal.Wait(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); }
private void TestChangeTrackerBackoff(MockHttpClientFactory httpClientFactory) { var changeTrackerFinishedSignal = new CountdownEvent(1); var client = new ChangeTrackerTestClient(changeTrackerFinishedSignal, null); client.HttpClientFactory = httpClientFactory; var testUrl = GetReplicationURL(); var scheduler = new SingleTaskThreadpoolScheduler(); var opts = new RemoteSessionContructorOptions { BaseUrl = testUrl, WorkExecutor = new TaskFactory(scheduler) }; var remoteSession = new RemoteSession(opts); remoteSession.SetupHttpClientFactory(client.HttpClientFactory, database, "foo"); remoteSession.Setup(new ReplicationOptions()); var changeTracker = ChangeTrackerFactory.Create(new ChangeTrackerOptions { DatabaseUri = testUrl, Mode = ChangeTrackerMode.LongPoll, IncludeConflicts = true, Client = client, RetryStrategy = new ExponentialBackoffStrategy(2), WorkExecutor = new TaskFactory(scheduler), RemoteSession = remoteSession }); changeTracker.Continuous = true; changeTracker.Start(); // sleep for a few seconds Sleep(8 * 1000); // make sure we got less than 10 requests in those 8 seconds (if it was hammering, we'd get a lot more) var handler = client.HttpRequestHandler; Assert.Less(handler.CapturedRequests.Count, 10); Assert.Greater(changeTracker.Backoff.NumAttempts, 0, String.Format("Observed attempts: {0}", 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 Sleep(10 * 1000); // now find the delta in requests received in a 2s period int before = handler.CapturedRequests.Count; 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.Greater((after - before), 25); // the backoff numAttempts should have been reset to 0 Assert.IsTrue(changeTracker.Backoff.NumAttempts == 0); changeTracker.Stop(); var success = changeTrackerFinishedSignal.Wait(TimeSpan.FromSeconds(30)); Assert.IsTrue(success); }