geary/test/engine/db/db-versioned-database-test.vala
Michael James Gratton 743b24dd5d Make database classes more amenable to asynchronous use.
This makes both the open() and open_connection() methods on
Geary.DB.Database asynchronous, which allows the VersionedDatabase
open_background() and its hackery to be removed, and upgrades to be
performed asynchronously as well. It also adds a
exec_transaction_async() method to Connection, allowing an existing
object to also be used to establish an async transaction.

* src/engine/db/db-connection.vala (Connection): Add
  exec_transaction_async method, update doc comments.

* src/engine/db/db-database.vala (Database): Make open and
  open_connection async by executing SQLite code in a background thread,
  update call sites. Move job management code out of
  exec_transaction_async into a new internal add_async_job() method so it
  can be used by Connection. Add unit tests.

* src/engine/db/db-transaction-async-job.vala (TransactionAsyncJob): Add
  an optional internal connection method and make the job's cancellable
  an internal property so a Connection instance can specify itself for
  the transaction.

* src/engine/db/db-versioned-database.vala (VersionedDatabase): Remove
  open_background() hack since open() is now async. Make version upgrade
  hooks for derived classes async and update call sites. Use a
  Nonblocking.Mutex rather than GLib mutex so upgrade exclusion works
  asynchronously. Add unit tests.

* src/engine/imap-db/imap-db-database.vala (Database): Make database
  upgrade methods async and execute SQL in async instructions now that
  the bases classes support it. Add unit tests.
2018-05-08 12:24:48 +10:00

54 lines
1.6 KiB
Vala

/*
* Copyright 2018 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
class Geary.Db.VersionedDatabaseTest : TestCase {
public VersionedDatabaseTest() {
base("Geary.Db.VersionedDatabaseTest");
add_test("open_new", open_new);
}
public void open_new() throws Error {
GLib.File tmp_dir = GLib.File.new_for_path(
GLib.DirUtils.make_tmp("geary-db-database-test-XXXXXX")
);
GLib.File sql1 = tmp_dir.get_child("version-001.sql");
sql1.create(
GLib.FileCreateFlags.NONE
).write("CREATE TABLE TestTable (id INTEGER PRIMARY KEY, col TEXT);".data);
GLib.File sql2 = tmp_dir.get_child("version-002.sql");
sql2.create(
GLib.FileCreateFlags.NONE
).write("INSERT INTO TestTable (col) VALUES ('value');".data);
VersionedDatabase db = new VersionedDatabase.persistent(
tmp_dir.get_child("test.db"), tmp_dir
);
db.open.begin(
Geary.Db.DatabaseFlags.CREATE_FILE, null, null,
(obj, ret) => { async_complete(ret); }
);
db.open.end(async_result());
Geary.Db.Result result = db.query("SELECT * FROM TestTable;");
assert_false(result.finished, "Row not inserted");
assert_string("value", result.string_for("col"));
assert_false(result.next(), "Multiple rows inserted");
db.file.delete();
sql1.delete();
sql2.delete();
tmp_dir.delete();
}
}