2 * @brief Tests of closing databases.
4 /* Copyright 2008,2009 Lemur Consulting Ltd
5 * Copyright 2009,2012,2015 Olly Betts
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "api_closedb.h"
28 #include "safeunistd.h"
31 #include "testutils.h"
35 #define COUNT_CLOSEDEXC(CODE) \
38 } catch (const Xapian::DatabaseClosedError &) { \
42 #define IF_NOT_CLOSEDEXC(CODE) \
47 } catch (const Xapian::DatabaseClosedError &) { \
51 } while (false); if (hadexc)
53 // Iterators used by closedb1.
54 struct closedb1_iterators
{
56 Xapian::Document doc1
;
57 Xapian::PostingIterator pl1
;
58 Xapian::PostingIterator pl2
;
59 Xapian::PostingIterator pl1end
;
60 Xapian::PostingIterator pl2end
;
61 Xapian::TermIterator tl1
;
62 Xapian::TermIterator tlend
;
63 Xapian::TermIterator atl1
;
64 Xapian::TermIterator atlend
;
65 Xapian::PositionIterator pil1
;
66 Xapian::PositionIterator pilend
;
68 void setup(Xapian::Database db_
) {
71 // Set up the iterators for the test.
72 pl1
= db
.postlist_begin("paragraph");
73 pl2
= db
.postlist_begin("this");
75 pl1end
= db
.postlist_end("paragraph");
76 pl2end
= db
.postlist_end("this");
77 tl1
= db
.termlist_begin(1);
78 tlend
= db
.termlist_end(1);
79 atl1
= db
.allterms_begin("t");
80 atlend
= db
.allterms_end("t");
81 pil1
= db
.positionlist_begin(1, "paragraph");
82 pilend
= db
.positionlist_end(1, "paragraph");
86 int closedexc_count
= 0;
89 // Getting a document may throw closed.
90 IF_NOT_CLOSEDEXC(doc1
= db
.get_document(1)) {
91 COUNT_CLOSEDEXC(TEST_EQUAL(doc1
.get_data().substr(0, 33),
92 "This is a test document used with"));
93 COUNT_CLOSEDEXC(doc1
.termlist_begin());
96 // Causing the database to access its files raises the "database
98 COUNT_CLOSEDEXC(db
.postlist_begin("paragraph"));
99 COUNT_CLOSEDEXC(db
.get_document(1).get_value(1));
100 COUNT_CLOSEDEXC(db
.termlist_begin(1));
101 COUNT_CLOSEDEXC(db
.positionlist_begin(1, "paragraph"));
102 COUNT_CLOSEDEXC(db
.allterms_begin());
103 COUNT_CLOSEDEXC(db
.allterms_begin("p"));
104 COUNT_CLOSEDEXC(db
.get_termfreq("paragraph"));
105 COUNT_CLOSEDEXC(db
.get_collection_freq("paragraph"));
106 COUNT_CLOSEDEXC(db
.term_exists("paragraph"));
107 COUNT_CLOSEDEXC(db
.get_value_freq(1));
108 COUNT_CLOSEDEXC(db
.get_value_lower_bound(1));
109 COUNT_CLOSEDEXC(db
.get_value_upper_bound(1));
110 COUNT_CLOSEDEXC(db
.valuestream_begin(1));
111 COUNT_CLOSEDEXC(db
.get_doclength(1));
112 COUNT_CLOSEDEXC(db
.get_unique_terms(1));
114 // Reopen raises the "database closed" error.
115 COUNT_CLOSEDEXC(db
.reopen());
117 TEST_NOT_EQUAL(pl1
, pl1end
);
118 TEST_NOT_EQUAL(pl2
, pl2end
);
119 TEST_NOT_EQUAL(tl1
, tlend
);
120 TEST_NOT_EQUAL(atl1
, atlend
);
121 TEST_NOT_EQUAL(pil1
, pilend
);
123 COUNT_CLOSEDEXC(db
.postlist_begin("paragraph"));
125 COUNT_CLOSEDEXC(TEST_EQUAL(*pl1
, 1));
126 COUNT_CLOSEDEXC(TEST_EQUAL(pl1
.get_doclength(), 28));
127 COUNT_CLOSEDEXC(TEST_EQUAL(pl1
.get_unique_terms(), 21));
129 COUNT_CLOSEDEXC(TEST_EQUAL(*pl2
, 2));
130 COUNT_CLOSEDEXC(TEST_EQUAL(pl2
.get_doclength(), 81));
131 COUNT_CLOSEDEXC(TEST_EQUAL(pl2
.get_unique_terms(), 56));
133 COUNT_CLOSEDEXC(TEST_EQUAL(*tl1
, "a"));
134 COUNT_CLOSEDEXC(TEST_EQUAL(tl1
.get_wdf(), 2));
135 COUNT_CLOSEDEXC(TEST_EQUAL(tl1
.get_termfreq(), 3));
137 COUNT_CLOSEDEXC(TEST_EQUAL(*atl1
, "test"));
138 COUNT_CLOSEDEXC(TEST_EQUAL(atl1
.get_termfreq(), 1));
140 COUNT_CLOSEDEXC(TEST_EQUAL(*pil1
, 12));
142 // Advancing the iterator may or may not raise an error, but if it
143 // doesn't it must return the correct answers.
144 bool advanced
= false;
148 } catch (const Xapian::DatabaseClosedError
&) {}
151 COUNT_CLOSEDEXC(TEST_EQUAL(*pl1
, 2));
152 COUNT_CLOSEDEXC(TEST_EQUAL(pl1
.get_doclength(), 81));
153 COUNT_CLOSEDEXC(TEST_EQUAL(pl1
.get_unique_terms(), 56));
160 } catch (const Xapian::DatabaseClosedError
&) {}
163 COUNT_CLOSEDEXC(TEST_EQUAL(*pl2
, 3));
164 COUNT_CLOSEDEXC(TEST_EQUAL(pl2
.get_doclength(), 15));
165 COUNT_CLOSEDEXC(TEST_EQUAL(pl2
.get_unique_terms(), 14));
172 } catch (const Xapian::DatabaseClosedError
&) {}
175 COUNT_CLOSEDEXC(TEST_EQUAL(*tl1
, "api"));
176 COUNT_CLOSEDEXC(TEST_EQUAL(tl1
.get_wdf(), 1));
177 COUNT_CLOSEDEXC(TEST_EQUAL(tl1
.get_termfreq(), 1));
184 } catch (const Xapian::DatabaseClosedError
&) {}
187 COUNT_CLOSEDEXC(TEST_EQUAL(*atl1
, "that"));
188 COUNT_CLOSEDEXC(TEST_EQUAL(atl1
.get_termfreq(), 2));
195 } catch (const Xapian::DatabaseClosedError
&) {}
198 COUNT_CLOSEDEXC(TEST_EQUAL(*pil1
, 28));
201 return closedexc_count
;
205 // Test for closing a database
206 DEFINE_TESTCASE(closedb1
, backend
) {
207 Xapian::Database
db(get_database("apitest_simpledata"));
208 closedb1_iterators iters
;
210 // Run the test, checking that we get no "closed" exceptions.
212 int closedexc_count
= iters
.perform();
213 TEST_EQUAL(closedexc_count
, 0);
215 // Setup for the next test.
218 // Close the database.
221 // Dup stdout to the fds which the database was using, to try to catch
222 // issues with lingering references to closed fds (regression test for
223 // early development versions of honey).
225 for (int i
= 0; i
!= 6; ++i
) {
226 fds
.push_back(dup(1));
229 // Reopening a closed database should always raise DatabaseClosedError.
230 TEST_EXCEPTION(Xapian::DatabaseClosedError
, db
.reopen());
232 // Run the test again, checking that we get some "closed" exceptions.
233 closedexc_count
= iters
.perform();
234 TEST_NOT_EQUAL(closedexc_count
, 0);
236 // get_description() shouldn't throw an exception. Actually do something
237 // with the description, in case this method is marked as "pure" in the
239 TEST(!db
.get_description().empty());
241 // Calling close repeatedly is okay.
249 // Test closing a writable database, and that it drops the lock.
250 DEFINE_TESTCASE(closedb2
, writable
&& path
) {
251 Xapian::WritableDatabase
dbw1(get_named_writable_database("apitest_closedb2"));
252 TEST_EXCEPTION(Xapian::DatabaseLockError
,
253 Xapian::WritableDatabase
db(get_named_writable_database_path("apitest_closedb2"),
256 Xapian::WritableDatabase dbw2
= get_named_writable_database("apitest_closedb2");
257 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
258 dbw1
.postlist_begin("paragraph"));
259 TEST_EQUAL(dbw2
.postlist_begin("paragraph"), dbw2
.postlist_end("paragraph"));
262 /// Check API methods which might either work or throw an exception.
263 DEFINE_TESTCASE(closedb3
, backend
) {
264 Xapian::Database
db(get_database("etext"));
265 const string
& uuid
= db
.get_uuid();
268 TEST_EQUAL(db
.get_uuid(), uuid
);
269 } catch (const Xapian::DatabaseClosedError
&) {
272 TEST(db
.has_positions());
273 } catch (const Xapian::DatabaseClosedError
&) {
276 TEST_EQUAL(db
.get_doccount(), 566);
277 } catch (const Xapian::DatabaseClosedError
&) {
280 TEST_EQUAL(db
.get_lastdocid(), 566);
281 } catch (const Xapian::DatabaseClosedError
&) {
284 TEST_REL(db
.get_doclength_lower_bound(), <, db
.get_avlength());
285 } catch (const Xapian::DatabaseClosedError
&) {
288 TEST_REL(db
.get_doclength_upper_bound(), >, db
.get_avlength());
289 } catch (const Xapian::DatabaseClosedError
&) {
292 TEST(db
.get_wdf_upper_bound("king"));
293 } catch (const Xapian::DatabaseClosedError
&) {
296 // For non-remote databases, keep_alive() is a no-op anyway.
298 } catch (const Xapian::DatabaseClosedError
&) {
302 /// Regression test for bug fixed in 1.1.4 - close() should implicitly commit().
303 DEFINE_TESTCASE(closedb4
, writable
&& !inmemory
) {
304 Xapian::WritableDatabase
wdb(get_writable_database());
305 wdb
.add_document(Xapian::Document());
306 TEST_EQUAL(wdb
.get_doccount(), 1);
308 Xapian::Database
db(get_writable_database_as_database());
309 TEST_EQUAL(db
.get_doccount(), 1);
312 /// Test the effects of close() on transactions
313 DEFINE_TESTCASE(closedb5
, transactions
) {
315 // If a transaction is active, close() shouldn't implicitly commit().
316 Xapian::WritableDatabase wdb
= get_writable_database();
317 wdb
.begin_transaction();
318 wdb
.add_document(Xapian::Document());
319 TEST_EQUAL(wdb
.get_doccount(), 1);
321 Xapian::Database db
= get_writable_database_as_database();
322 TEST_EQUAL(db
.get_doccount(), 0);
326 // Same test but for an unflushed transaction.
327 Xapian::WritableDatabase wdb
= get_writable_database();
328 wdb
.begin_transaction(false);
329 wdb
.add_document(Xapian::Document());
330 TEST_EQUAL(wdb
.get_doccount(), 1);
332 Xapian::Database db
= get_writable_database_as_database();
333 TEST_EQUAL(db
.get_doccount(), 0);
337 // commit_transaction() throws InvalidOperationError when
338 // not in a transaction.
339 Xapian::WritableDatabase wdb
= get_writable_database();
341 TEST_EXCEPTION(Xapian::InvalidOperationError
,
342 wdb
.commit_transaction());
344 // begin_transaction() is no-op or throws DatabaseClosedError. We may be
345 // able to call db.begin_transaction(), but we can't make any changes
346 // inside that transaction. If begin_transaction() succeeds, then
347 // commit_transaction() either end the transaction or throw
348 // DatabaseClosedError.
349 bool advanced
= false;
351 wdb
.begin_transaction();
353 } catch (const Xapian::DatabaseClosedError
&) {
357 wdb
.commit_transaction();
358 } catch (const Xapian::DatabaseClosedError
&) {
364 // Same test but for cancel_transaction().
365 Xapian::WritableDatabase wdb
= get_writable_database();
367 TEST_EXCEPTION(Xapian::InvalidOperationError
,
368 wdb
.cancel_transaction());
370 bool advanced
= false;
372 wdb
.begin_transaction();
374 } catch (const Xapian::DatabaseClosedError
&) {
378 wdb
.cancel_transaction();
379 } catch (const Xapian::DatabaseClosedError
&) {
385 /// Database::keep_alive() should fail after close() for a remote database.
386 DEFINE_TESTCASE(closedb6
, remote
) {
387 Xapian::Database
db(get_database("etext"));
392 FAIL_TEST("Expected DatabaseClosedError wasn't thrown");
393 } catch (const Xapian::DatabaseClosedError
&) {
397 // Test WritableDatabase methods.
398 DEFINE_TESTCASE(closedb7
, writable
) {
399 Xapian::WritableDatabase
db(get_writable_database());
400 db
.add_document(Xapian::Document());
403 // Since we can't make any changes which need to be committed,
404 // db.commit() is a no-op, and so doesn't have to fail.
407 } catch (const Xapian::DatabaseClosedError
&) {
410 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
411 db
.add_document(Xapian::Document()));
412 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
413 db
.delete_document(1));
414 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
415 db
.replace_document(1, Xapian::Document()));
416 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
417 db
.replace_document(2, Xapian::Document()));
418 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
419 db
.replace_document("Qi", Xapian::Document()));
422 // Test spelling related methods.
423 DEFINE_TESTCASE(closedb8
, writable
&& spelling
) {
424 Xapian::WritableDatabase
db(get_writable_database());
425 db
.add_spelling("pneumatic");
426 db
.add_spelling("pneumonia");
429 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
430 db
.add_spelling("penmanship"));
431 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
432 db
.remove_spelling("pneumatic"));
433 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
434 db
.get_spelling_suggestion("newmonia"));
435 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
436 db
.spellings_begin());
439 // Test synonym related methods.
440 DEFINE_TESTCASE(closedb9
, writable
&& synonyms
) {
441 Xapian::WritableDatabase
db(get_writable_database());
442 db
.add_synonym("color", "colour");
443 db
.add_synonym("honor", "honour");
446 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
447 db
.add_synonym("behavior", "behaviour"));
448 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
449 db
.remove_synonym("honor", "honour"));
450 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
451 db
.clear_synonyms("honor"));
452 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
453 db
.synonyms_begin("color"));
454 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
455 db
.synonym_keys_begin());
458 // Test metadata related methods.
459 DEFINE_TESTCASE(closedb10
, writable
&& metadata
) {
460 Xapian::WritableDatabase
db(get_writable_database());
461 db
.set_metadata("foo", "FOO");
462 db
.set_metadata("bar", "BAR");
465 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
466 db
.set_metadata("test", "TEST"));
467 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
468 db
.get_metadata("foo"));
469 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
470 db
.get_metadata("bar"));
471 TEST_EXCEPTION(Xapian::DatabaseClosedError
,
472 db
.metadata_keys_begin());