Update for 1.4.18
[xapian.git] / xapian-core / tests / api_closedb.cc
blob600747b94a5e9a591ebeb56a0eadb9060327eaf5
1 /** @file
2 * @brief Tests of closing databases.
3 */
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
22 #include <config.h>
24 #include "api_closedb.h"
26 #include <xapian.h>
28 #include "safeunistd.h"
30 #include "apitest.h"
31 #include "testutils.h"
33 using namespace std;
35 #define COUNT_CLOSEDEXC(CODE) \
36 try { \
37 CODE; \
38 } catch (const Xapian::DatabaseClosedError &) { \
39 ++closedexc_count; \
42 #define IF_NOT_CLOSEDEXC(CODE) \
43 do { \
44 hadexc = false; \
45 try { \
46 CODE; \
47 } catch (const Xapian::DatabaseClosedError &) { \
48 ++closedexc_count; \
49 hadexc = true; \
50 } \
51 } while (false); if (hadexc)
53 // Iterators used by closedb1.
54 struct closedb1_iterators {
55 Xapian::Database db;
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_) {
69 db = db_;
71 // Set up the iterators for the test.
72 pl1 = db.postlist_begin("paragraph");
73 pl2 = db.postlist_begin("this");
74 ++pl2;
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");
85 int perform() {
86 int closedexc_count = 0;
87 bool hadexc;
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
97 // closed" error.
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;
145 try {
146 ++pl1;
147 advanced = true;
148 } catch (const Xapian::DatabaseClosedError &) {}
150 if (advanced) {
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));
156 advanced = false;
157 try {
158 ++pl2;
159 advanced = true;
160 } catch (const Xapian::DatabaseClosedError &) {}
162 if (advanced) {
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));
168 advanced = false;
169 try {
170 ++tl1;
171 advanced = true;
172 } catch (const Xapian::DatabaseClosedError &) {}
174 if (advanced) {
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));
180 advanced = false;
181 try {
182 ++atl1;
183 advanced = true;
184 } catch (const Xapian::DatabaseClosedError &) {}
186 if (advanced) {
187 COUNT_CLOSEDEXC(TEST_EQUAL(*atl1, "that"));
188 COUNT_CLOSEDEXC(TEST_EQUAL(atl1.get_termfreq(), 2));
191 advanced = false;
192 try {
193 ++pil1;
194 advanced = true;
195 } catch (const Xapian::DatabaseClosedError &) {}
197 if (advanced) {
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.
211 iters.setup(db);
212 int closedexc_count = iters.perform();
213 TEST_EQUAL(closedexc_count, 0);
215 // Setup for the next test.
216 iters.setup(db);
218 // Close the database.
219 db.close();
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).
224 vector<int> fds;
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
238 // future.
239 TEST(!db.get_description().empty());
241 // Calling close repeatedly is okay.
242 db.close();
244 for (int fd : fds) {
245 close(fd);
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"),
254 Xapian::DB_OPEN));
255 dbw1.close();
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();
266 db.close();
267 try {
268 TEST_EQUAL(db.get_uuid(), uuid);
269 } catch (const Xapian::DatabaseClosedError &) {
271 try {
272 TEST(db.has_positions());
273 } catch (const Xapian::DatabaseClosedError &) {
275 try {
276 TEST_EQUAL(db.get_doccount(), 566);
277 } catch (const Xapian::DatabaseClosedError &) {
279 try {
280 TEST_EQUAL(db.get_lastdocid(), 566);
281 } catch (const Xapian::DatabaseClosedError &) {
283 try {
284 TEST_REL(db.get_doclength_lower_bound(), <, db.get_avlength());
285 } catch (const Xapian::DatabaseClosedError &) {
287 try {
288 TEST_REL(db.get_doclength_upper_bound(), >, db.get_avlength());
289 } catch (const Xapian::DatabaseClosedError &) {
291 try {
292 TEST(db.get_wdf_upper_bound("king"));
293 } catch (const Xapian::DatabaseClosedError &) {
295 try {
296 // For non-remote databases, keep_alive() is a no-op anyway.
297 db.keep_alive();
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);
307 wdb.close();
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);
320 wdb.close();
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);
331 wdb.close();
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();
340 wdb.close();
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;
350 try {
351 wdb.begin_transaction();
352 advanced = true;
353 } catch (const Xapian::DatabaseClosedError &) {
355 if (advanced) {
356 try {
357 wdb.commit_transaction();
358 } catch (const Xapian::DatabaseClosedError &) {
364 // Same test but for cancel_transaction().
365 Xapian::WritableDatabase wdb = get_writable_database();
366 wdb.close();
367 TEST_EXCEPTION(Xapian::InvalidOperationError,
368 wdb.cancel_transaction());
370 bool advanced = false;
371 try {
372 wdb.begin_transaction();
373 advanced = true;
374 } catch (const Xapian::DatabaseClosedError &) {
376 if (advanced) {
377 try {
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"));
388 db.close();
390 try {
391 db.keep_alive();
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());
401 db.close();
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.
405 try {
406 db.commit();
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");
427 db.close();
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");
444 db.close();
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");
463 db.close();
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());