Beispiel #1
0
        private void TestLiveChangeTracker(Func <Uri, IChangeTrackerClient, ChangeTracker> generator, bool continuous)
        {
            if (!Boolean.Parse((string)GetProperty("replicationTestsEnabled")))
            {
                Assert.Inconclusive("Replication tests disabled.");
                return;
            }

            var changedEvent  = new CountdownEvent(4);
            var caughtUpEvent = new AutoResetEvent(false);
            var stoppedEvent  = new CountdownEvent(1);
            var client        = new ChangeTrackerTestClient(stoppedEvent, changedEvent, caughtUpEvent);

            client.HttpRequestHandler.DefaultFail = false;
            using (var remoteDb = _sg.CreateDatabase("web_socket_scratch")) {
                var tracker = generator(remoteDb.RemoteUri, client);
                tracker.Start();
                Assert.IsTrue(caughtUpEvent.WaitOne(200000));

                if (continuous)
                {
                    remoteDb.AddDocument("newdoc", new Dictionary <string, object> {
                        { "foo", "bar" }
                    });
                    Assert.IsTrue(changedEvent.Wait(20000));

                    tracker.Stop();
                    Assert.IsTrue(stoppedEvent.Wait(20000));
                }
            }
        }
Beispiel #2
0
        public void TestEmptyContentType()
        {
            var sg = new SyncGateway(GetReplicationProtocol(), GetReplicationServer());

            using (var remoteDb = sg.CreateDatabase("empty-content-type")) {
                var doc     = database.CreateDocument();
                var unsaved = doc.CreateRevision();

                var data = Enumerable.Repeat <byte>((byte)80, 2500);
                unsaved.SetAttachment("attach", "", data);
                unsaved.Save();
                database.GetDocumentCount().Should().Be(1, "because a document was added");

                var push = database.CreatePushReplication(remoteDb.RemoteUri);
                push.Start();
                var now = DateTime.UtcNow;
                while (push.CompletedChangesCount < 1)
                {
                    Sleep(500);
                    if (DateTime.UtcNow - now > TimeSpan.FromSeconds(10))
                    {
                        throw new TimeoutException("Test timed out");
                    }
                }
            }
        }
Beispiel #3
0
        public void TestPushTimeSeries()
        {
            if (!Boolean.Parse((string)GetProperty("replicationTestsEnabled")))
            {
                Assert.Inconclusive("Replication tests disabled.");
                return;
            }

            using (var remoteDb = _sg.CreateDatabase(ScratchDbName)) {
                var tsPush = _ts.CreatePushReplication(remoteDb.RemoteUri, true);
                tsPush.Continuous = true;
                Console.WriteLine("Starting replication");

                var idleMre    = new ManualResetEventSlim();
                var stoppedMre = new ManualResetEventSlim();
                tsPush.Changed += (sender, e) =>
                {
                    if (e.CompletedChangesCount == 10 && e.Status == ReplicationStatus.Idle &&
                        e.Source.GetPendingDocumentIDs().Count == 0)
                    {
                        idleMre.Set();
                    }
                    else if (e.Status == ReplicationStatus.Stopped)
                    {
                        stoppedMre.Set();
                    }
                };

                var waitHandle = GenerateEventsAsync();
                tsPush.Start();

                // Generate events:

                Console.WriteLine("Waiting for events...");
                Assert.IsTrue(waitHandle.WaitOne(TimeSpan.FromSeconds(10)), "Waiting for events timed out");
                Console.WriteLine("Waiting for replication to finish...");
                idleMre.Reset();
                Assert.IsTrue(idleMre.Wait(TimeSpan.FromSeconds(10)), "Timed out waiting for replication");
                idleMre.Dispose();
                Assert.IsNull(tsPush.LastError);

                // Stop the replication:
                tsPush.Stop();
                Console.WriteLine("Waiting for replication to stop");
                Assert.IsTrue(stoppedMre.Wait(TimeSpan.FromSeconds(10)), "Timed out waiting for replication to stop");
                stoppedMre.Dispose();
                Assert.IsNull(tsPush.LastError);

                // Did the docs get purged?
                Assert.AreEqual(0, database.GetDocumentCount());
            }
        }
        public void TestExternalReplicationStart()
        {
            var sg  = new SyncGateway(GetReplicationProtocol(), GetReplicationServer());
            var tmp = EnsureEmptyDatabase("test_db");

            tmp.Close();

            SetupListener(false);
            CreateDocuments(database, 10);
            using (var remoteDb = sg.CreateDatabase("external_replication_test")) {
                var request = WebRequest.Create("http://localhost:" + _port + "/_replicate");
                request.ContentType = "application/json";
                request.Method      = "POST";
                var body  = String.Format("{{\"source\":\"{0}\",\"target\":\"{1}\"}}", database.Name, remoteDb.RemoteUri);
                var bytes = Encoding.UTF8.GetBytes(body);
                request.ContentLength = bytes.Length;
                request.GetRequestStream().Write(bytes, 0, bytes.Length);

                var response = (HttpWebResponse)request.GetResponse();
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

                request             = WebRequest.Create("http://localhost:" + _port + "/_replicate");
                request.ContentType = "application/json";
                request.Method      = "POST";
                body  = String.Format("{{\"source\":\"{0}\",\"target\":\"test_db\",\"create_target\":true}}", remoteDb.RemoteUri);
                bytes = Encoding.UTF8.GetBytes(body);
                request.ContentLength = bytes.Length;
                request.GetRequestStream().Write(bytes, 0, bytes.Length);

                response = (HttpWebResponse)request.GetResponse();
                Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);

                var createdDb = manager.GetExistingDatabase("test_db");
                Assert.IsNotNull(createdDb);
                Assert.AreEqual(10, createdDb.GetDocumentCount());
            }
        }
Beispiel #5
0
        public void TestFacebookAuth()
        {
            var sg = new SyncGateway("http", GetReplicationServer());

            using (var remoteDb = sg.CreateDatabase("facebook")) {
                remoteDb.DisableGuestAccess();
                var doneEvent = new ManualResetEvent(false);

                var accountStore = new ACAccountStore();
                var accountType  = accountStore.FindAccountType(ACAccountType.Facebook);

                var options = new AccountStoreOptions();
                options.FacebookAppId = FacebookAppId;
                options.SetPermissions(ACFacebookAudience.Friends, new [] { "email" });

                var       success = true;
                ACAccount account = null;
                accountStore.RequestAccess(accountType, options, (result, error) =>
                {
                    success = result;
                    if (success)
                    {
                        var accounts = accountStore.FindAccounts(accountType);
                        account      = accounts != null && accounts.Length > 0 ? accounts[0] : null;
                    }
                    else
                    {
                        Log.W(Tag, "Facebook Login needed. Go to Settings > Facebook and login.");
                        Log.E(Tag, "Facebook Request Access Error : " + error);
                    }
                    doneEvent.Set();
                });

                doneEvent.WaitOne(TimeSpan.FromSeconds(30));

                Assert.IsTrue(success);
                Assert.IsNotNull(account);

                var token = account.Credential.OAuthToken;
                Assert.IsNotNull(token);
                Assert.IsTrue(token.Length > 0);

                var url = remoteDb.RemoteUri;

                var cookieStore       = new CookieStore(manager.Directory);
                var httpClientFactory = new CouchbaseLiteHttpClientFactory(cookieStore);
                manager.DefaultHttpClientFactory = httpClientFactory;
                Replication replicator = database.CreatePushReplication(url);
                replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(token);

                Assert.IsNotNull(replicator);
                Assert.IsNotNull(replicator.Authenticator);
                Assert.IsTrue(replicator.Authenticator is TokenAuthenticator);

                CreateDocuments(database, 20);

                RunReplication(replicator);

                var urlStr = url.ToString();
                urlStr = urlStr.EndsWith("/") ? urlStr : urlStr + "/";
                //var cookies = httpClientFactory.GetCookieContainer().GetCookies(new Uri(urlStr));
                //Assert.IsTrue(cookies.Count == 1);
                //Assert.AreEqual("SyncGatewaySession", cookies[0].Name);
            }
        }