LCOV - code coverage report
Current view: top level - tests - api_replicate.cc (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 395 463 85.3 %
Date: 2019-06-30 05:20:33 Functions: 20 20 100.0 %
Branches: 448 2502 17.9 %

           Branch data     Line data    Source code
       1                 :            : /** @file api_replicate.cc
       2                 :            :  * @brief tests of replication functionality
       3                 :            :  */
       4                 :            : /* Copyright 2008 Lemur Consulting Ltd
       5                 :            :  * Copyright 2009,2010,2011,2012,2013,2014,2015,2016,2017 Olly Betts
       6                 :            :  * Copyright 2010 Richard Boulton
       7                 :            :  * Copyright 2011 Dan Colish
       8                 :            :  *
       9                 :            :  * This program is free software; you can redistribute it and/or
      10                 :            :  * modify it under the terms of the GNU General Public License as
      11                 :            :  * published by the Free Software Foundation; either version 2 of the
      12                 :            :  * License, or (at your option) any later version.
      13                 :            :  *
      14                 :            :  * This program is distributed in the hope that it will be useful,
      15                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17                 :            :  * GNU General Public License for more details.
      18                 :            :  *
      19                 :            :  * You should have received a copy of the GNU General Public License
      20                 :            :  * along with this program; if not, write to the Free Software
      21                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
      22                 :            :  * USA
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <config.h>
      26                 :            : 
      27                 :            : #include "api_replicate.h"
      28                 :            : 
      29                 :            : #include <xapian.h>
      30                 :            : #include "api/replication.h"
      31                 :            : 
      32                 :            : #include "apitest.h"
      33                 :            : #include "dbcheck.h"
      34                 :            : #include "errno_to_string.h"
      35                 :            : #include "fd.h"
      36                 :            : #include "filetests.h"
      37                 :            : #include "safedirent.h"
      38                 :            : #include "safefcntl.h"
      39                 :            : #include "safesysstat.h"
      40                 :            : #include "safeunistd.h"
      41                 :            : #include "setenv.h"
      42                 :            : #include "testsuite.h"
      43                 :            : #include "testutils.h"
      44                 :            : #include "unixcmds.h"
      45                 :            : 
      46                 :            : #include <sys/types.h>
      47                 :            : 
      48                 :            : #include <cerrno>
      49                 :            : #include <cstdlib>
      50                 :            : #include <string>
      51                 :            : 
      52                 :            : using namespace std;
      53                 :            : 
      54                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
      55                 :            : 
      56                 :         12 : static void rmtmpdir(const string & path) {
      57                 :         12 :     rm_rf(path);
      58                 :         12 : }
      59                 :            : 
      60                 :          6 : static void mktmpdir(const string & path) {
      61                 :          6 :     rmtmpdir(path);
      62 [ -  + ][ #  # ]:          6 :     if (mkdir(path.c_str(), 0700) == -1 && errno != EEXIST) {
                 [ -  + ]
      63         [ #  # ]:          0 :         FAIL_TEST("Can't make temporary directory");
      64                 :            :     }
      65                 :          6 : }
      66                 :            : 
      67                 :          1 : static off_t get_file_size(const string & path) {
      68                 :          1 :     off_t size = file_size(path);
      69         [ -  + ]:          1 :     if (errno) {
      70         [ #  # ]:          0 :         FAIL_TEST("Can't stat '" << path << "'");
      71                 :            :     }
      72                 :          1 :     return size;
      73                 :            : }
      74                 :            : 
      75                 :        328 : static size_t do_read(int fd, char * p, size_t desired)
      76                 :            : {
      77                 :        328 :     size_t total = 0;
      78         [ +  + ]:        656 :     while (desired) {
      79                 :        328 :         ssize_t c = read(fd, p, desired);
      80         [ -  + ]:        328 :         if (c == 0) return total;
      81         [ -  + ]:        328 :         if (c < 0) {
      82         [ #  # ]:          0 :             if (errno == EINTR) continue;
      83         [ #  # ]:          0 :             FAIL_TEST("Error reading from file");
      84                 :            :         }
      85                 :        328 :         p += c;
      86                 :        328 :         total += c;
      87                 :        328 :         desired -= c;
      88                 :            :     }
      89                 :        328 :     return total;
      90                 :            : }
      91                 :            : 
      92                 :        328 : static void do_write(int fd, const char * p, size_t n)
      93                 :            : {
      94         [ +  + ]:        656 :     while (n) {
      95                 :        328 :         ssize_t c = write(fd, p, n);
      96         [ -  + ]:        328 :         if (c < 0) {
      97         [ #  # ]:          0 :             if (errno == EINTR) continue;
      98         [ #  # ]:          0 :             FAIL_TEST("Error writing to file");
      99                 :            :         }
     100                 :        328 :         p += c;
     101                 :        328 :         n -= c;
     102                 :            :     }
     103                 :        328 : }
     104                 :            : 
     105                 :            : // Make a truncated copy of a file.
     106                 :            : static off_t
     107                 :         47 : truncated_copy(const string & srcpath, const string & destpath, off_t tocopy)
     108                 :            : {
     109         [ +  - ]:         47 :     FD fdin(open(srcpath.c_str(), O_RDONLY | O_BINARY));
     110         [ -  + ]:         47 :     if (fdin == -1) {
     111 [ #  # ][ #  # ]:          0 :         FAIL_TEST("Open failed (when opening '" << srcpath << "')");
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     112                 :            :     }
     113                 :            : 
     114         [ +  - ]:         94 :     FD fdout(open(destpath.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
     115         [ -  + ]:         47 :     if (fdout == -1) {
     116 [ #  # ][ #  # ]:          0 :         FAIL_TEST("Open failed (when creating '" << destpath << "')");
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     117                 :            :     }
     118                 :            : 
     119                 :         47 :     const int BUFSIZE = 1024;
     120                 :            :     char buf[BUFSIZE];
     121                 :         47 :     size_t total_bytes = 0;
     122         [ +  + ]:        375 :     while (tocopy > 0) {
     123                 :        328 :         size_t thiscopy = tocopy > BUFSIZE ? BUFSIZE : tocopy;
     124         [ +  - ]:        328 :         size_t bytes = do_read(fdin, buf, thiscopy);
     125         [ -  + ]:        328 :         if (thiscopy != bytes) {
     126 [ #  # ][ #  # ]:          0 :             FAIL_TEST("Couldn't read desired number of bytes from changeset");
         [ #  # ][ #  # ]
     127                 :            :         }
     128                 :        328 :         tocopy -= bytes;
     129                 :        328 :         total_bytes += bytes;
     130         [ +  - ]:        328 :         do_write(fdout, buf, bytes);
     131                 :            :     }
     132                 :            : 
     133 [ +  - ][ -  + ]:         47 :     if (close(fdout) == -1)
     134 [ #  # ][ #  # ]:          0 :         FAIL_TEST("Error closing file");
         [ #  # ][ #  # ]
     135                 :            : 
     136                 :         47 :     return total_bytes;
     137                 :            : }
     138                 :            : 
     139                 :            : static void
     140                 :         23 : get_changeset(const string & changesetpath,
     141                 :            :               Xapian::DatabaseMaster & master,
     142                 :            :               Xapian::DatabaseReplica & replica,
     143                 :            :               int expected_changesets,
     144                 :            :               int expected_fullcopies,
     145                 :            :               bool expected_changed,
     146                 :            :               bool full_copy = false)
     147                 :            : {
     148         [ +  - ]:         23 :     FD fd(open(changesetpath.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666));
     149         [ -  + ]:         23 :     if (fd == -1) {
     150 [ #  # ][ #  # ]:          0 :         FAIL_TEST("Open failed (when creating a new changeset file at '"
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     151                 :            :                   << changesetpath << "')");
     152                 :            :     }
     153                 :         23 :     Xapian::ReplicationInfo info1;
     154                 :            :     master.write_changesets_to_fd(fd,
     155                 :            :                                   full_copy ? "" : replica.get_revision_info(),
     156 [ +  + ][ +  - ]:         23 :                                   &info1);
         [ +  - ][ +  - ]
         [ +  + ][ #  # ]
     157                 :            : 
     158 [ -  + ][ #  # ]:         23 :     TEST_EQUAL(info1.changeset_count, expected_changesets);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     159 [ -  + ][ #  # ]:         23 :     TEST_EQUAL(info1.fullcopy_count, expected_fullcopies);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     160 [ -  + ][ #  # ]:         23 :     TEST_EQUAL(info1.changed, expected_changed);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     161                 :         23 : }
     162                 :            : 
     163                 :            : static int
     164                 :         69 : apply_changeset(const string & changesetpath,
     165                 :            :                 Xapian::DatabaseReplica & replica,
     166                 :            :                 int expected_changesets,
     167                 :            :                 int expected_fullcopies,
     168                 :            :                 bool expected_changed)
     169                 :            : {
     170         [ +  - ]:         69 :     FD fd(open(changesetpath.c_str(), O_RDONLY | O_BINARY));
     171         [ -  + ]:         69 :     if (fd == -1) {
     172 [ #  # ][ #  # ]:          0 :         FAIL_TEST("Open failed (when reading changeset file at '"
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     173                 :            :                   << changesetpath << "')");
     174                 :            :     }
     175                 :            : 
     176                 :         69 :     int count = 1;
     177         [ +  - ]:         69 :     replica.set_read_fd(fd);
     178                 :         69 :     Xapian::ReplicationInfo info1;
     179                 :         69 :     Xapian::ReplicationInfo info2;
     180                 :         69 :     bool client_changed = false;
     181 [ +  + ][ +  + ]:         82 :     while (replica.apply_next_changeset(&info2, 0)) {
     182                 :         13 :         ++count;
     183                 :         13 :         info1.changeset_count += info2.changeset_count;
     184                 :         13 :         info1.fullcopy_count += info2.fullcopy_count;
     185         [ +  - ]:         13 :         if (info2.changed)
     186                 :         13 :             client_changed = true;
     187                 :            :     }
     188                 :         22 :     info1.changeset_count += info2.changeset_count;
     189                 :         22 :     info1.fullcopy_count += info2.fullcopy_count;
     190         [ +  + ]:         22 :     if (info2.changed)
     191                 :          9 :         client_changed = true;
     192                 :            : 
     193 [ -  + ][ #  # ]:         22 :     TEST_EQUAL(info1.changeset_count, expected_changesets);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     194 [ -  + ][ #  # ]:         22 :     TEST_EQUAL(info1.fullcopy_count, expected_fullcopies);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     195 [ -  + ][ #  # ]:         22 :     TEST_EQUAL(client_changed, expected_changed);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     196                 :         69 :     return count;
     197                 :            : }
     198                 :            : 
     199                 :            : // Replicate from the master to the replica.
     200                 :            : // Returns the number of changesets which were applied.
     201                 :            : static int
     202                 :         22 : replicate(Xapian::DatabaseMaster & master,
     203                 :            :           Xapian::DatabaseReplica & replica,
     204                 :            :           const string & tempdir,
     205                 :            :           int expected_changesets,
     206                 :            :           int expected_fullcopies,
     207                 :            :           bool expected_changed,
     208                 :            :           bool full_copy = false)
     209                 :            : {
     210         [ +  - ]:         22 :     string changesetpath = tempdir + "/changeset";
     211                 :            :     get_changeset(changesetpath, master, replica,
     212                 :            :                   expected_changesets,
     213                 :            :                   expected_fullcopies,
     214                 :            :                   expected_changed,
     215         [ +  - ]:         22 :                   full_copy);
     216                 :            :     return apply_changeset(changesetpath, replica,
     217                 :            :                            expected_changesets,
     218                 :            :                            expected_fullcopies,
     219         [ +  - ]:         22 :                            expected_changed);
     220                 :            : }
     221                 :            : 
     222                 :            : // Check that the databases held at the given path are identical.
     223                 :            : static void
     224                 :         13 : check_equal_dbs(const string & masterpath, const string & replicapath)
     225                 :            : {
     226         [ +  - ]:         13 :     Xapian::Database master(masterpath);
     227         [ +  - ]:         26 :     Xapian::Database replica(replicapath);
     228                 :            : 
     229 [ +  - ][ +  - ]:         13 :     TEST_EQUAL(master.get_uuid(), master.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     230 [ +  - ][ +  - ]:         13 :     dbcheck(replica, master.get_doccount(), master.get_lastdocid());
                 [ +  - ]
     231                 :            : 
     232 [ +  - ][ +  - ]:        138 :     for (Xapian::TermIterator t = master.allterms_begin();
           [ +  -  +  + ]
     233         [ +  - ]:         92 :          t != master.allterms_end(); ++t) {
     234 [ +  - ][ +  - ]:         33 :         TEST_EQUAL(postlist_to_string(master, *t),
         [ +  - ][ +  - ]
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     235                 :            :                    postlist_to_string(replica, *t));
     236                 :         26 :     }
     237                 :         13 : }
     238                 :            : 
     239                 :            : #define set_max_changesets(N) setenv("XAPIAN_MAX_CHANGESETS", #N, 1)
     240                 :            : 
     241                 :            : struct unset_max_changesets_helper_ {
     242                 :          6 :     unset_max_changesets_helper_() { }
     243                 :         12 :     ~unset_max_changesets_helper_() { set_max_changesets(0); }
     244                 :            : };
     245                 :            : 
     246                 :            : // Ensure that we don't leave generation of changesets on for the next
     247                 :            : // testcase, even if this one exits with an exception.
     248                 :            : #define UNSET_MAX_CHANGESETS_AFTERWARDS unset_max_changesets_helper_ ezlxq
     249                 :            : 
     250                 :            : #endif
     251                 :            : 
     252                 :            : // #######################################################################
     253                 :            : // # Tests start here
     254                 :            : 
     255                 :            : // Basic test of replication functionality.
     256                 :          1 : DEFINE_TESTCASE(replicate1, replicas) {
     257                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     258                 :          1 :     UNSET_MAX_CHANGESETS_AFTERWARDS;
     259         [ +  - ]:          2 :     string tempdir = ".replicatmp";
     260         [ +  - ]:          1 :     mktmpdir(tempdir);
     261 [ +  - ][ +  - ]:          2 :     string masterpath = get_named_writable_database_path("master");
     262                 :            : 
     263                 :          1 :     set_max_changesets(10);
     264                 :            : 
     265         [ +  - ]:          2 :     Xapian::Document doc1;
     266 [ +  - ][ +  - ]:          1 :     doc1.set_data(string("doc1"));
     267 [ +  - ][ +  - ]:          1 :     doc1.add_posting("doc", 1);
     268 [ +  - ][ +  - ]:          1 :     doc1.add_posting("one", 1);
     269                 :            : 
     270 [ +  - ][ +  - ]:          2 :     Xapian::WritableDatabase orig(get_named_writable_database("master"));
                 [ +  - ]
     271         [ +  - ]:          2 :     Xapian::DatabaseMaster master(masterpath);
     272         [ +  - ]:          2 :     string replicapath = tempdir + "/replica";
     273                 :            :     {
     274         [ +  - ]:          1 :         Xapian::DatabaseReplica replica(replicapath);
     275                 :            : 
     276                 :            :         // Add a document to the original database.
     277         [ +  - ]:          1 :         orig.add_document(doc1);
     278         [ +  - ]:          1 :         orig.commit();
     279                 :            : 
     280                 :            :         // Apply the replication - we don't have changesets stored, so this
     281                 :            :         // should just do a database copy, and return a count of 1.
     282         [ +  - ]:          1 :         int count = replicate(master, replica, tempdir, 0, 1, true);
     283 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     284                 :            :         {
     285         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     286 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     287                 :            :         }
     288                 :            : 
     289                 :            :         // Repeating the replication should return a count of 1, since no
     290                 :            :         // further changes should need to be applied.
     291         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 0, 0, false);
     292 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     293                 :            :         {
     294         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     295 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     296                 :          1 :         }
     297                 :            :     }
     298                 :            :     {
     299                 :            :         // Regression test - if the replica was reopened, a full copy always
     300                 :            :         // used to occur, whether it was needed or not.  Fixed in revision
     301                 :            :         // #10117.
     302         [ +  - ]:          1 :         Xapian::DatabaseReplica replica(replicapath);
     303         [ +  - ]:          1 :         int count = replicate(master, replica, tempdir, 0, 0, false);
     304 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     305                 :            :         {
     306         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     307 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     308                 :            :         }
     309                 :            : 
     310         [ +  - ]:          1 :         orig.add_document(doc1);
     311         [ +  - ]:          1 :         orig.commit();
     312         [ +  - ]:          1 :         orig.add_document(doc1);
     313         [ +  - ]:          1 :         orig.commit();
     314                 :            : 
     315         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 2, 0, true);
     316 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 3);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     317                 :            :         {
     318         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     319 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     320                 :            :         }
     321                 :            : 
     322         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     323                 :            : 
     324                 :            :         // We need this inner scope to we close the replica before we remove
     325                 :            :         // the temporary directory on Windows.
     326                 :            :     }
     327                 :            : 
     328         [ +  - ]:          1 :     rmtmpdir(tempdir);
     329                 :            : #endif
     330                 :          1 :     return true;
     331                 :            : }
     332                 :            : 
     333                 :            : // Test replication from a replicated copy.
     334                 :          1 : DEFINE_TESTCASE(replicate2, replicas) {
     335                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     336 [ +  - ][ -  + ]:          1 :     SKIP_TEST_FOR_BACKEND("glass"); // Glass doesn't currently support this.
     337                 :          0 :     UNSET_MAX_CHANGESETS_AFTERWARDS;
     338                 :            : 
     339         [ #  # ]:          0 :     string tempdir = ".replicatmp";
     340         [ #  # ]:          0 :     mktmpdir(tempdir);
     341 [ #  # ][ #  # ]:          0 :     string masterpath = get_named_writable_database_path("master");
     342                 :            : 
     343                 :          0 :     set_max_changesets(10);
     344                 :            : 
     345                 :            :     {
     346 [ #  # ][ #  # ]:          0 :         Xapian::WritableDatabase orig(get_named_writable_database("master"));
                 [ #  # ]
     347         [ #  # ]:          0 :         Xapian::DatabaseMaster master(masterpath);
     348         [ #  # ]:          0 :         string replicapath = tempdir + "/replica";
     349         [ #  # ]:          0 :         Xapian::DatabaseReplica replica(replicapath);
     350                 :            : 
     351         [ #  # ]:          0 :         Xapian::DatabaseMaster master2(replicapath);
     352         [ #  # ]:          0 :         string replica2path = tempdir + "/replica2";
     353         [ #  # ]:          0 :         Xapian::DatabaseReplica replica2(replica2path);
     354                 :            : 
     355                 :            :         // Add a document to the original database.
     356         [ #  # ]:          0 :         Xapian::Document doc1;
     357 [ #  # ][ #  # ]:          0 :         doc1.set_data(string("doc1"));
     358 [ #  # ][ #  # ]:          0 :         doc1.add_posting("doc", 1);
     359 [ #  # ][ #  # ]:          0 :         doc1.add_posting("one", 1);
     360         [ #  # ]:          0 :         orig.add_document(doc1);
     361         [ #  # ]:          0 :         orig.commit();
     362                 :            : 
     363                 :            :         // Apply the replication - we don't have changesets stored, so this
     364                 :            :         // should just do a database copy, and return a count of 1.
     365 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master, replica, tempdir, 0, 1, true), 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     366         [ #  # ]:          0 :         check_equal_dbs(masterpath, replicapath);
     367                 :            : 
     368                 :            :         // Replicate from the replica.
     369 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master2, replica2, tempdir, 0, 1, true), 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     370         [ #  # ]:          0 :         check_equal_dbs(masterpath, replica2path);
     371                 :            : 
     372         [ #  # ]:          0 :         orig.add_document(doc1);
     373         [ #  # ]:          0 :         orig.commit();
     374         [ #  # ]:          0 :         orig.add_document(doc1);
     375         [ #  # ]:          0 :         orig.commit();
     376                 :            : 
     377                 :            :         // Replicate from the replica - should have no changes.
     378 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master2, replica2, tempdir, 0, 0, false), 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     379         [ #  # ]:          0 :         check_equal_dbs(replicapath, replica2path);
     380                 :            : 
     381                 :            :         // Replicate, and replicate from the replica - should have 2 changes.
     382 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master, replica, tempdir, 2, 0, 1), 3);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     383         [ #  # ]:          0 :         check_equal_dbs(masterpath, replicapath);
     384 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master2, replica2, tempdir, 2, 0, 1), 3);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     385         [ #  # ]:          0 :         check_equal_dbs(masterpath, replica2path);
     386                 :            : 
     387                 :            :         // Stop writing changesets, and make a modification
     388                 :          0 :         set_max_changesets(0);
     389         [ #  # ]:          0 :         orig.close();
     390 [ #  # ][ #  # ]:          0 :         orig = get_writable_database_again();
     391         [ #  # ]:          0 :         orig.add_document(doc1);
     392         [ #  # ]:          0 :         orig.commit();
     393                 :            : 
     394                 :            :         // Replication should do a full copy.
     395 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master, replica, tempdir, 0, 1, true), 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     396         [ #  # ]:          0 :         check_equal_dbs(masterpath, replicapath);
     397 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master2, replica2, tempdir, 0, 1, true), 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     398         [ #  # ]:          0 :         check_equal_dbs(masterpath, replica2path);
     399                 :            : 
     400                 :            :         // Start writing changesets, but only keep 1 in history, and make a
     401                 :            :         // modification.
     402                 :          0 :         set_max_changesets(1);
     403         [ #  # ]:          0 :         orig.close();
     404 [ #  # ][ #  # ]:          0 :         orig = get_writable_database_again();
     405         [ #  # ]:          0 :         orig.add_document(doc1);
     406         [ #  # ]:          0 :         orig.commit();
     407                 :            : 
     408                 :            :         // Replicate, and replicate from the replica - should have 1 changes.
     409 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master, replica, tempdir, 1, 0, 1), 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     410         [ #  # ]:          0 :         check_equal_dbs(masterpath, replicapath);
     411 [ #  # ][ #  # ]:          0 :         TEST_EQUAL(replicate(master2, replica2, tempdir, 1, 0, 1), 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     412         [ #  # ]:          0 :         check_equal_dbs(masterpath, replica2path);
     413                 :            : 
     414                 :            :         // Make two changes - only one changeset should be preserved.
     415         [ #  # ]:          0 :         orig.add_document(doc1);
     416         [ #  # ]:          0 :         orig.commit();
     417                 :            : 
     418                 :            :         // Replication should do a full copy, since one of the needed
     419                 :            :         // changesets is missing.
     420                 :            : 
     421                 :            :         // FIXME - the following tests are commented out because the backends
     422                 :            :         // don't currently tidy up old changesets correctly.
     423                 :            :         // TEST_EQUAL(replicate(master, replica, tempdir, 0, 1, true), 1);
     424                 :            :         // check_equal_dbs(masterpath, replicapath);
     425                 :            :         // TEST_EQUAL(replicate(master2, replica2, tempdir, 0, 1, true), 1);
     426                 :            :         // check_equal_dbs(masterpath, replica2path);
     427                 :            : 
     428                 :            :         // We need this inner scope to we close the replicas before we remove
     429                 :            :         // the temporary directory on Windows.
     430                 :            :     }
     431                 :            : 
     432         [ #  # ]:          0 :     rmtmpdir(tempdir);
     433                 :            : #endif
     434                 :          0 :     return true;
     435                 :            : }
     436                 :            : 
     437                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     438                 :            : static void
     439                 :          1 : replicate_with_brokenness(Xapian::DatabaseMaster & master,
     440                 :            :                           Xapian::DatabaseReplica & replica,
     441                 :            :                           const string & tempdir,
     442                 :            :                           int expected_changesets,
     443                 :            :                           int expected_fullcopies,
     444                 :            :                           bool expected_changed)
     445                 :            : {
     446         [ +  - ]:          1 :     string changesetpath = tempdir + "/changeset";
     447                 :            :     get_changeset(changesetpath, master, replica,
     448         [ +  - ]:          1 :                   1, 0, 1);
     449                 :            : 
     450                 :            :     // Try applying truncated changesets of various different lengths.
     451         [ +  - ]:          2 :     string brokenchangesetpath = tempdir + "/changeset_broken";
     452         [ +  - ]:          1 :     off_t filesize = get_file_size(changesetpath);
     453                 :          1 :     off_t len = 10;
     454                 :            :     off_t copylen;
     455         [ +  + ]:         48 :     while (len < filesize) {
     456         [ +  - ]:         47 :         copylen = truncated_copy(changesetpath, brokenchangesetpath, len);
     457 [ -  + ][ #  # ]:         47 :         TEST_EQUAL(copylen, len);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     458 [ +  - ][ +  - ]:         47 :         tout << "Trying replication with a changeset truncated to " << len <<
     459 [ +  - ][ +  - ]:         47 :                 " bytes, from " << filesize << " bytes\n";
                 [ +  - ]
     460 [ +  - ][ -  + ]:         47 :         TEST_EXCEPTION(Xapian::NetworkError,
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ -  + ][ -  +  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     461                 :            :                        apply_changeset(brokenchangesetpath, replica,
     462                 :            :                                        expected_changesets, expected_fullcopies,
     463                 :            :                                        expected_changed));
     464   [ +  +  +  + ]:         47 :         if (len < 30 || len >= filesize - 10) {
     465                 :            :             // For lengths near the beginning and end, increment size by 1
     466                 :         30 :             ++len;
     467                 :            :         } else {
     468                 :            :             // Don't bother incrementing by small amounts in the middle of
     469                 :            :             // the changeset.
     470                 :         17 :             len += 1000;
     471         [ +  + ]:         17 :             if (len >= filesize - 10) {
     472                 :          1 :                 len = filesize - 10;
     473                 :            :             }
     474                 :            :         }
     475                 :          1 :     }
     476                 :          1 : }
     477                 :            : #endif
     478                 :            : 
     479                 :            : // Test changesets which are truncated (and therefore invalid).
     480                 :          1 : DEFINE_TESTCASE(replicate3, replicas) {
     481                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     482                 :          1 :     UNSET_MAX_CHANGESETS_AFTERWARDS;
     483         [ +  - ]:          2 :     string tempdir = ".replicatmp";
     484         [ +  - ]:          1 :     mktmpdir(tempdir);
     485 [ +  - ][ +  - ]:          2 :     string masterpath = get_named_writable_database_path("master");
     486                 :            : 
     487                 :          1 :     set_max_changesets(10);
     488                 :            : 
     489                 :            :     {
     490 [ +  - ][ +  - ]:          1 :         Xapian::WritableDatabase orig(get_named_writable_database("master"));
                 [ +  - ]
     491         [ +  - ]:          2 :         Xapian::DatabaseMaster master(masterpath);
     492         [ +  - ]:          2 :         string replicapath = tempdir + "/replica";
     493         [ +  - ]:          2 :         Xapian::DatabaseReplica replica(replicapath);
     494                 :            : 
     495                 :            :         // Add a document to the original database.
     496         [ +  - ]:          2 :         Xapian::Document doc1;
     497 [ +  - ][ +  - ]:          1 :         doc1.set_data(string("doc1"));
     498 [ +  - ][ +  - ]:          1 :         doc1.add_posting("doc", 1);
     499 [ +  - ][ +  - ]:          1 :         doc1.add_posting("one", 1);
     500         [ +  - ]:          1 :         orig.add_document(doc1);
     501         [ +  - ]:          1 :         orig.commit();
     502                 :            : 
     503 [ +  - ][ -  + ]:          1 :         TEST_EQUAL(replicate(master, replica, tempdir, 0, 1, true), 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     504         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     505                 :            : 
     506                 :            :         // Make a changeset.
     507         [ +  - ]:          1 :         orig.add_document(doc1);
     508         [ +  - ]:          1 :         orig.commit();
     509                 :            : 
     510         [ +  - ]:          1 :         replicate_with_brokenness(master, replica, tempdir, 1, 0, true);
     511                 :            :         // Although it throws an error, the final replication in
     512                 :            :         // replicate_with_brokenness() updates the database, since it's just
     513                 :            :         // the end-of-replication message which is missing its body.
     514         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     515                 :            : 
     516                 :            :         // Check that the earlier broken replications didn't cause any problems
     517                 :            :         // for the next replication.
     518         [ +  - ]:          1 :         orig.add_document(doc1);
     519         [ +  - ]:          1 :         orig.commit();
     520 [ +  - ][ -  + ]:          2 :         TEST_EQUAL(replicate(master, replica, tempdir, 1, 0, true), 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     521                 :            : 
     522                 :            :         // We need this inner scope to we close the replica before we remove
     523                 :            :         // the temporary directory on Windows.
     524                 :            :     }
     525                 :            : 
     526         [ +  - ]:          1 :     rmtmpdir(tempdir);
     527                 :            : #endif
     528                 :          1 :     return true;
     529                 :            : }
     530                 :            : 
     531                 :            : // Tests for max_changesets
     532                 :          1 : DEFINE_TESTCASE(replicate4, replicas) {
     533                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     534                 :          1 :     UNSET_MAX_CHANGESETS_AFTERWARDS;
     535         [ +  - ]:          2 :     string tempdir = ".replicatmp";
     536         [ +  - ]:          1 :     mktmpdir(tempdir);
     537 [ +  - ][ +  - ]:          2 :     string masterpath = get_named_writable_database_path("master");
     538                 :            : 
     539                 :          1 :     set_max_changesets(1);
     540                 :            : 
     541                 :            :     {
     542 [ +  - ][ +  - ]:          1 :         Xapian::WritableDatabase orig(get_named_writable_database("master"));
                 [ +  - ]
     543         [ +  - ]:          2 :         Xapian::DatabaseMaster master(masterpath);
     544         [ +  - ]:          2 :         string replicapath = tempdir + "/replica";
     545         [ +  - ]:          2 :         Xapian::DatabaseReplica replica(replicapath);
     546                 :            : 
     547                 :            :         // Add a document with no positions to the original database.
     548         [ +  - ]:          2 :         Xapian::Document doc1;
     549 [ +  - ][ +  - ]:          1 :         doc1.set_data(string("doc1"));
     550 [ +  - ][ +  - ]:          1 :         doc1.add_term("nopos");
     551         [ +  - ]:          1 :         orig.add_document(doc1);
     552         [ +  - ]:          1 :         orig.commit();
     553                 :            : 
     554                 :            :         // Apply the replication - we don't have changesets stored, so this
     555                 :            :         // should just do a database copy, and return a count of 1.
     556         [ +  - ]:          1 :         int count = replicate(master, replica, tempdir, 0, 1, true);
     557 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     558                 :            :         {
     559         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     560 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     561                 :            :         }
     562                 :            : 
     563                 :            :         // Add a document with positional information to the original database.
     564 [ +  - ][ +  - ]:          1 :         doc1.add_posting("pos", 1);
     565         [ +  - ]:          1 :         orig.add_document(doc1);
     566         [ +  - ]:          1 :         orig.commit();
     567                 :            : 
     568                 :            :         // Replicate, and check that we have the positional information.
     569         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 1, 0, true);
     570 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     571                 :            :         {
     572         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     573 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     574                 :            :         }
     575         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     576                 :            : 
     577                 :            :         // Add a document with no positions to the original database.
     578         [ +  - ]:          2 :         Xapian::Document doc2;
     579 [ +  - ][ +  - ]:          1 :         doc2.set_data(string("doc2"));
     580 [ +  - ][ +  - ]:          1 :         doc2.add_term("nopos");
     581         [ +  - ]:          1 :         orig.add_document(doc2);
     582 [ +  - ][ +  - ]:          1 :         if (get_dbtype() == "glass") {
                 [ +  - ]
     583                 :            :             // FIXME: Needs to be pre-commit for glass
     584                 :          1 :             set_max_changesets(0);
     585                 :            :         }
     586         [ +  - ]:          1 :         orig.commit();
     587                 :            : 
     588                 :            :         // Replicate, and check that we have the positional information.
     589         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 1, 0, true);
     590 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     591                 :            :         {
     592         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     593 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     594                 :            :         }
     595         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     596 [ +  - ][ -  + ]:          1 :         TEST(!file_exists(masterpath + "/changes1"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     597                 :            : 
     598                 :            :         // Turn off replication, make sure we don't write anything.
     599 [ +  - ][ +  - ]:          1 :         if (get_dbtype() != "glass") {
                 [ -  + ]
     600                 :          0 :             set_max_changesets(0);
     601                 :            :         }
     602                 :            : 
     603                 :            :         // Add a document with no positions to the original database.
     604         [ +  - ]:          2 :         Xapian::Document doc3;
     605 [ +  - ][ +  - ]:          1 :         doc3.set_data(string("doc3"));
     606 [ +  - ][ +  - ]:          1 :         doc3.add_term("nonopos");
     607         [ +  - ]:          1 :         orig.add_document(doc3);
     608         [ +  - ]:          1 :         orig.commit();
     609                 :            : 
     610                 :            :         // Replicate, and check that we have the positional information.
     611         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 0, 1, true);
     612 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     613                 :            :         {
     614         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     615 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     616                 :            :         }
     617                 :            :         // Should have pulled a full copy
     618         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     619 [ +  - ][ -  + ]:          2 :         TEST(!file_exists(masterpath + "/changes3"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     620                 :            : 
     621                 :            :         // We need this inner scope to we close the replica before we remove
     622                 :            :         // the temporary directory on Windows.
     623                 :            :     }
     624                 :            : 
     625         [ +  - ]:          1 :     rmtmpdir(tempdir);
     626                 :            : #endif
     627                 :          1 :     return true;
     628                 :            : }
     629                 :            : 
     630                 :            : // Tests for max_changesets
     631                 :          1 : DEFINE_TESTCASE(replicate5, replicas) {
     632                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     633                 :          1 :     UNSET_MAX_CHANGESETS_AFTERWARDS;
     634         [ +  - ]:          2 :     string tempdir = ".replicatmp";
     635         [ +  - ]:          1 :     mktmpdir(tempdir);
     636 [ +  - ][ +  - ]:          2 :     string masterpath = get_named_writable_database_path("master");
     637                 :            : 
     638                 :          1 :     set_max_changesets(2);
     639                 :            : 
     640                 :            :     {
     641 [ +  - ][ +  - ]:          1 :         Xapian::WritableDatabase orig(get_named_writable_database("master"));
                 [ +  - ]
     642         [ +  - ]:          2 :         Xapian::DatabaseMaster master(masterpath);
     643         [ +  - ]:          2 :         string replicapath = tempdir + "/replica";
     644         [ +  - ]:          2 :         Xapian::DatabaseReplica replica(replicapath);
     645                 :            : 
     646                 :            :         // Add a document with no positions to the original database.
     647         [ +  - ]:          2 :         Xapian::Document doc1;
     648 [ +  - ][ +  - ]:          1 :         doc1.set_data(string("doc1"));
     649 [ +  - ][ +  - ]:          1 :         doc1.add_term("nopos");
     650         [ +  - ]:          1 :         orig.add_document(doc1);
     651         [ +  - ]:          1 :         orig.commit();
     652                 :            : 
     653                 :            :         // Apply the replication - we don't have changesets stored, so this
     654                 :            :         // should just do a database copy, and return a count of 1.
     655         [ +  - ]:          1 :         int count = replicate(master, replica, tempdir, 0, 1, true);
     656 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     657                 :            :         {
     658         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     659 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     660                 :            :         }
     661                 :            : 
     662                 :            :         // Add a document with positional information to the original database.
     663 [ +  - ][ +  - ]:          1 :         doc1.add_posting("pos", 1);
     664         [ +  - ]:          1 :         orig.add_document(doc1);
     665         [ +  - ]:          1 :         orig.commit();
     666                 :            : 
     667                 :            :         // Replicate, and check that we have the positional information.
     668         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 1, 0, true);
     669 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     670                 :            :         {
     671         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     672 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     673                 :            :         }
     674         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     675                 :            : 
     676                 :            :         // Add a document with no positions to the original database.
     677         [ +  - ]:          2 :         Xapian::Document doc2;
     678 [ +  - ][ +  - ]:          1 :         doc2.set_data(string("doc2"));
     679 [ +  - ][ +  - ]:          1 :         doc2.add_term("nopos");
     680         [ +  - ]:          1 :         orig.add_document(doc2);
     681         [ +  - ]:          1 :         orig.commit();
     682                 :            : 
     683                 :            :         // Replicate, and check that we have the positional information.
     684         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 1, 0, true);
     685 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     686                 :            :         {
     687         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     688 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     689                 :            :         }
     690         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     691                 :            : 
     692                 :            :         // Add a document with no positions to the original database.
     693         [ +  - ]:          2 :         Xapian::Document doc3;
     694 [ +  - ][ +  - ]:          1 :         doc3.set_data(string("doc3"));
     695 [ +  - ][ +  - ]:          1 :         doc3.add_term("nonopos");
     696         [ +  - ]:          1 :         orig.add_document(doc3);
     697         [ +  - ]:          1 :         orig.commit();
     698                 :            : 
     699                 :            :         // Replicate, and check that we have the positional information.
     700         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 1, 0, true);
     701 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     702                 :            :         {
     703         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     704 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     705                 :            :         }
     706         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     707                 :            : 
     708                 :            :         // Ensure that only these changesets exists
     709 [ +  - ][ -  + ]:          1 :         TEST(!file_exists(masterpath + "/changes1"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     710 [ +  - ][ -  + ]:          1 :         TEST(file_exists(masterpath + "/changes2"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     711 [ +  - ][ -  + ]:          1 :         TEST(file_exists(masterpath + "/changes3"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     712                 :            : 
     713                 :          1 :         set_max_changesets(3);
     714 [ +  - ][ +  - ]:          1 :         masterpath = get_named_writable_database_path("master");
                 [ +  - ]
     715                 :            : 
     716                 :            :         // Add a document with no positions to the original database.
     717         [ +  - ]:          2 :         Xapian::Document doc4;
     718 [ +  - ][ +  - ]:          1 :         doc4.set_data(string("doc4"));
     719 [ +  - ][ +  - ]:          1 :         doc4.add_term("nononopos");
     720         [ +  - ]:          1 :         orig.add_document(doc4);
     721         [ +  - ]:          1 :         orig.commit();
     722                 :            : 
     723                 :            :         // Replicate, and check that we have the positional information.
     724         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 1, 0, true);
     725 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     726                 :            :         {
     727         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     728 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     729                 :            :         }
     730         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     731                 :            : 
     732                 :            :         // Add a document with no positions to the original database.
     733         [ +  - ]:          2 :         Xapian::Document doc5;
     734 [ +  - ][ +  - ]:          1 :         doc5.set_data(string("doc5"));
     735 [ +  - ][ +  - ]:          1 :         doc5.add_term("nonononopos");
     736         [ +  - ]:          1 :         orig.add_document(doc5);
     737         [ +  - ]:          1 :         orig.commit();
     738                 :            : 
     739                 :            :         // Replicate, and check that we have the positional information.
     740         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 1, 0, true);
     741 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 2);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     742                 :            :         {
     743         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     744 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     745                 :            :         }
     746         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     747                 :            : 
     748 [ +  - ][ -  + ]:          1 :         TEST(!file_exists(masterpath + "/changes2"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     749 [ +  - ][ -  + ]:          1 :         TEST(file_exists(masterpath + "/changes3"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     750 [ +  - ][ -  + ]:          1 :         TEST(file_exists(masterpath + "/changes4"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     751 [ +  - ][ -  + ]:          2 :         TEST(file_exists(masterpath + "/changes5"));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     752                 :            : 
     753                 :            :         // We need this inner scope to we close the replica before we remove
     754                 :            :         // the temporary directory on Windows.
     755                 :            :     }
     756                 :            : 
     757         [ +  - ]:          1 :     rmtmpdir(tempdir);
     758                 :            : #endif
     759                 :          1 :     return true;
     760                 :            : }
     761                 :            : 
     762                 :            : /// Test --full-copy option.
     763                 :          1 : DEFINE_TESTCASE(replicate6, replicas) {
     764                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     765                 :          1 :     UNSET_MAX_CHANGESETS_AFTERWARDS;
     766         [ +  - ]:          2 :     string tempdir = ".replicatmp";
     767         [ +  - ]:          1 :     mktmpdir(tempdir);
     768 [ +  - ][ +  - ]:          2 :     string masterpath = get_named_writable_database_path("master");
     769                 :            : 
     770                 :          1 :     set_max_changesets(10);
     771                 :            : 
     772                 :            :     {
     773 [ +  - ][ +  - ]:          1 :         Xapian::WritableDatabase orig(get_named_writable_database("master"));
                 [ +  - ]
     774         [ +  - ]:          2 :         Xapian::DatabaseMaster master(masterpath);
     775         [ +  - ]:          2 :         string replicapath = tempdir + "/replica";
     776         [ +  - ]:          2 :         Xapian::DatabaseReplica replica(replicapath);
     777                 :            : 
     778                 :            :         // Add a document to the original database.
     779         [ +  - ]:          2 :         Xapian::Document doc1;
     780 [ +  - ][ +  - ]:          1 :         doc1.set_data(string("doc1"));
     781 [ +  - ][ +  - ]:          1 :         doc1.add_posting("doc", 1);
     782 [ +  - ][ +  - ]:          1 :         doc1.add_posting("one", 1);
     783         [ +  - ]:          1 :         orig.add_document(doc1);
     784         [ +  - ]:          1 :         orig.commit();
     785                 :            : 
     786 [ +  - ][ +  - ]:          1 :         rm_rf(masterpath + "1");
     787 [ +  - ][ +  - ]:          1 :         cp_R(masterpath, masterpath + "1");
     788                 :            : 
     789         [ +  - ]:          1 :         orig.add_document(doc1);
     790         [ +  - ]:          1 :         orig.commit();
     791                 :            : 
     792                 :            :         // Apply the replication - we don't have changesets stored, so this
     793                 :            :         // should just do a database copy, and return a count of 1.
     794         [ +  - ]:          1 :         int count = replicate(master, replica, tempdir, 0, 1, true);
     795 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     796                 :            :         {
     797         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     798 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     799                 :            :         }
     800                 :            : 
     801 [ +  - ][ +  - ]:          2 :         Xapian::DatabaseMaster master1(masterpath + "1");
     802                 :            : 
     803                 :            :         // Try to replicate an older version of the master.
     804         [ +  - ]:          1 :         count = replicate(master1, replica, tempdir, 0, 0, false);
     805 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     806                 :            : 
     807                 :            :         // Force a full copy.
     808         [ +  - ]:          1 :         count = replicate(master1, replica, tempdir, 0, 1, true, true);
     809 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     810                 :            : 
     811                 :            :         // Test we can still replicate.
     812         [ +  - ]:          1 :         orig.add_document(doc1);
     813         [ +  - ]:          1 :         orig.commit();
     814                 :            : 
     815         [ +  - ]:          1 :         count = replicate(master, replica, tempdir, 2, 0, true);
     816 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 3);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     817                 :            : 
     818         [ +  - ]:          2 :         check_equal_dbs(masterpath, replicapath);
     819                 :            : 
     820                 :            :         // We need this inner scope to we close the replica before we remove
     821                 :            :         // the temporary directory on Windows.
     822                 :            :     }
     823                 :            : 
     824         [ +  - ]:          1 :     rmtmpdir(tempdir);
     825                 :            : #endif
     826                 :          1 :     return true;
     827                 :            : }
     828                 :            : 
     829                 :            : /// Test healing a corrupt replica (new in 1.3.5).
     830                 :          1 : DEFINE_TESTCASE(replicate7, replicas) {
     831                 :            : #ifdef XAPIAN_HAS_REMOTE_BACKEND
     832                 :          1 :     UNSET_MAX_CHANGESETS_AFTERWARDS;
     833         [ +  - ]:          2 :     string tempdir = ".replicatmp";
     834         [ +  - ]:          1 :     mktmpdir(tempdir);
     835 [ +  - ][ +  - ]:          2 :     string masterpath = get_named_writable_database_path("master");
     836                 :            : 
     837                 :          1 :     set_max_changesets(10);
     838                 :            : 
     839 [ +  - ][ +  - ]:          2 :     Xapian::WritableDatabase orig(get_named_writable_database("master"));
                 [ +  - ]
     840         [ +  - ]:          2 :     Xapian::DatabaseMaster master(masterpath);
     841         [ +  - ]:          2 :     string replicapath = tempdir + "/replica";
     842                 :            :     {
     843         [ +  - ]:          1 :         Xapian::DatabaseReplica replica(replicapath);
     844                 :            : 
     845                 :            :         // Add a document to the original database.
     846         [ +  - ]:          2 :         Xapian::Document doc1;
     847 [ +  - ][ +  - ]:          1 :         doc1.set_data(string("doc1"));
     848 [ +  - ][ +  - ]:          1 :         doc1.add_posting("doc", 1);
     849 [ +  - ][ +  - ]:          1 :         doc1.add_posting("one", 1);
     850         [ +  - ]:          1 :         orig.add_document(doc1);
     851         [ +  - ]:          1 :         orig.commit();
     852                 :            : 
     853         [ +  - ]:          1 :         orig.add_document(doc1);
     854         [ +  - ]:          1 :         orig.commit();
     855                 :            : 
     856                 :            :         // Apply the replication - we don't have changesets stored, so this
     857                 :            :         // should just do a database copy, and return a count of 1.
     858         [ +  - ]:          1 :         int count = replicate(master, replica, tempdir, 0, 1, true);
     859 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     860                 :            :         {
     861         [ +  - ]:          1 :             Xapian::Database dbcopy(replicapath);
     862 [ +  - ][ +  - ]:          1 :             TEST_EQUAL(orig.get_uuid(), dbcopy.get_uuid());
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     863                 :          1 :         }
     864                 :            :     }
     865                 :            : 
     866                 :            :     {
     867                 :            :         // Corrupt replica by truncating all the files to size 0.
     868         [ +  - ]:          1 :         string d = replicapath;
     869         [ +  - ]:          1 :         d += "/replica_1";
     870         [ +  - ]:          1 :         DIR * dir = opendir(d.c_str());
     871 [ -  + ][ #  # ]:          1 :         TEST(dir != NULL);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     872                 :            :         while (true) {
     873                 :          9 :             errno = 0;
     874         [ +  - ]:          9 :             struct dirent * entry = readdir(dir);
     875         [ +  + ]:          9 :             if (!entry) {
     876         [ +  - ]:          1 :                 if (errno == 0)
     877                 :          1 :                     break;
     878 [ #  # ][ #  # ]:          0 :                 FAIL_TEST("readdir failed: " << errno_to_string(errno));
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     879                 :            :             }
     880                 :            : 
     881                 :            :             // Skip '.' and '..'.
     882         [ +  + ]:          8 :             if (entry->d_name[0] == '.') continue;
     883                 :            : 
     884         [ +  - ]:          6 :             string file = d;
     885         [ +  - ]:          6 :             file += '/';
     886         [ +  - ]:          6 :             file += entry->d_name;
     887         [ +  - ]:          6 :             int fd = open(file.c_str(), O_WRONLY|O_TRUNC, 0666);
     888 [ -  + ][ #  # ]:          6 :             TEST(fd != -1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     889 [ +  - ][ -  + ]:          6 :             TEST(close(fd) == 0);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     890                 :          8 :         }
     891         [ +  - ]:          1 :         closedir(dir);
     892                 :            :     }
     893                 :            : 
     894                 :            :     {
     895         [ +  - ]:          1 :         Xapian::DatabaseReplica replica(replicapath);
     896                 :            : 
     897                 :            :         // Replication should succeed and perform a full copy.
     898         [ +  - ]:          1 :         int count = replicate(master, replica, tempdir, 0, 1, true);
     899 [ -  + ][ #  # ]:          1 :         TEST_EQUAL(count, 1);
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     900                 :            : 
     901         [ +  - ]:          1 :         check_equal_dbs(masterpath, replicapath);
     902                 :            : 
     903                 :            :         // We need this inner scope to we close the replica before we remove
     904                 :            :         // the temporary directory on Windows.
     905                 :            :     }
     906                 :            : 
     907         [ +  - ]:          1 :     rmtmpdir(tempdir);
     908                 :            : #endif
     909                 :          1 :     return true;
     910                 :            : }

Generated by: LCOV version 1.11