geary/test/engine/db/db-database-test.vala

170 lines
5.3 KiB
Vala
Raw Normal View History

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 09:18:06 +09:30
/*
* 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.DatabaseTest : TestCase {
public DatabaseTest() {
base("Geary.Db.DatabaseTest");
add_test("transient_open", transient_open);
add_test("open_existing", open_existing);
add_test("open_create_file", open_create_file);
add_test("open_create_dir", open_create_dir);
add_test("open_create_dir_existing", open_create_dir_existing);
add_test("open_check_corruption", open_check_corruption);
add_test("open_create_check", open_create_check);
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 09:18:06 +09:30
}
public void transient_open() throws Error {
Database db = new Geary.Db.Database.transient();
db.open.begin(Geary.Db.DatabaseFlags.NONE, null, this.async_completion);
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 09:18:06 +09:30
db.open.end(async_result());
// Need to get a connection since the database doesn't
// actually get created until then
db.get_primary_connection();
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 09:18:06 +09:30
}
public void open_existing() throws Error {
GLib.FileIOStream stream;
GLib.File tmp_file = GLib.File.new_tmp(
"geary-db-database-test-XXXXXX", out stream
);
Database db = new Geary.Db.Database.persistent(tmp_file);
db.open.begin(Geary.Db.DatabaseFlags.NONE, null, this.async_completion);
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 09:18:06 +09:30
db.open.end(async_result());
// Need to get a connection since the database doesn't
// actually get created until then
db.get_primary_connection();
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 09:18:06 +09:30
tmp_file.delete();
}
public void open_create_file() throws Error {
GLib.File tmp_dir = GLib.File.new_for_path(
GLib.DirUtils.make_tmp("geary-db-database-test-XXXXXX")
);
Database db = new Geary.Db.Database.persistent(
tmp_dir.get_child("test.db")
);
db.open.begin(
Geary.Db.DatabaseFlags.CREATE_FILE, null, this.async_completion
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 09:18:06 +09:30
);
db.open.end(async_result());
// Need to get a connection since the database doesn't
// actually get created until then
db.get_primary_connection();
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 09:18:06 +09:30
db.file.delete();
tmp_dir.delete();
}
public void open_create_dir() throws Error {
GLib.File tmp_dir = GLib.File.new_for_path(
GLib.DirUtils.make_tmp("geary-db-database-test-XXXXXX")
);
Database db = new Geary.Db.Database.persistent(
tmp_dir.get_child("nonexistent").get_child("test.db")
);
db.open.begin(
Geary.Db.DatabaseFlags.CREATE_DIRECTORY |
Geary.Db.DatabaseFlags.CREATE_FILE,
null,
this.async_completion
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 09:18:06 +09:30
);
db.open.end(async_result());
// Need to get a connection since the database doesn't
// actually get created until then
db.get_primary_connection();
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 09:18:06 +09:30
db.file.delete();
db.file.get_parent().delete();
tmp_dir.delete();
}
public void open_create_dir_existing() throws Error {
GLib.File tmp_dir = GLib.File.new_for_path(
GLib.DirUtils.make_tmp("geary-db-database-test-XXXXXX")
);
Database db = new Geary.Db.Database.persistent(
tmp_dir.get_child("test.db")
);
db.open.begin(
Geary.Db.DatabaseFlags.CREATE_DIRECTORY |
Geary.Db.DatabaseFlags.CREATE_FILE,
null,
this.async_completion
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 09:18:06 +09:30
);
db.open.end(async_result());
// Need to get a connection since the database doesn't
// actually get created until then
db.get_primary_connection();
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 09:18:06 +09:30
db.file.delete();
tmp_dir.delete();
}
public void open_check_corruption() throws Error {
GLib.File tmp_dir = GLib.File.new_for_path(
GLib.DirUtils.make_tmp("geary-db-database-test-XXXXXX")
);
Database db = new Geary.Db.Database.persistent(
tmp_dir.get_child("test.db")
);
db.open.begin(
Geary.Db.DatabaseFlags.CREATE_FILE |
Geary.Db.DatabaseFlags.CHECK_CORRUPTION,
null,
this.async_completion
);
db.open.end(async_result());
// Need to get a connection since the database doesn't
// actually get created until then
db.get_primary_connection();
db.file.delete();
tmp_dir.delete();
}
public void open_create_check() throws Error {
GLib.File tmp_dir = GLib.File.new_for_path(
GLib.DirUtils.make_tmp("geary-db-database-test-XXXXXX")
);
Database db = new Geary.Db.Database.persistent(
tmp_dir.get_child("nonexistent").get_child("test.db")
);
db.open.begin(
Geary.Db.DatabaseFlags.CREATE_DIRECTORY |
Geary.Db.DatabaseFlags.CREATE_FILE |
Geary.Db.DatabaseFlags.CHECK_CORRUPTION,
null,
this.async_completion
);
db.open.end(async_result());
// Need to get a connection since the database doesn't
// actually get created until then
db.get_primary_connection();
db.file.delete();
db.file.get_parent().delete();
tmp_dir.delete();
}
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 09:18:06 +09:30
}