Bug 1792124 - Test for shared calendar database connections. r=aleca
authorGeoff Lankow <geoff@darktrojan.net>
Tue, 27 Sep 2022 21:46:33 +0000
changeset 36809 e04f31ba752e5e24d8d82117d6e4f4380c4e5fd7
parent 36808 cdba13c93725315b5eb513bb9f800bac2ecc6502
child 36810 f2ab1e7387d2a1420096618a62676d4377f411e6
push id20331
push usergeoff@darktrojan.net
push dateTue, 27 Sep 2022 21:50:05 +0000
treeherdercomm-central@e04f31ba752e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaleca
bugs1792124
Bug 1792124 - Test for shared calendar database connections. r=aleca Differential Revision: https://phabricator.services.mozilla.com/D158184
calendar/test/unit/test_storage_connection.js
calendar/test/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/calendar/test/unit/test_storage_connection.js
@@ -0,0 +1,124 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, you can obtain one at http://mozilla.org/MPL/2.0/. */
+
+add_setup(async function() {
+  do_get_profile();
+  await new Promise(resolve => cal.manager.startup({ onResult: resolve }));
+});
+
+/**
+ * Tests that local storage calendars share a database connection.
+ */
+add_task(async function testLocal() {
+  let localCalendarA = cal.manager.createCalendar(
+    "storage",
+    Services.io.newURI(`moz-storage-calendar://`)
+  );
+  localCalendarA.id = cal.getUUID();
+  let dbA = localCalendarA.wrappedJSObject.mStorageDb.db;
+
+  let localCalendarB = cal.manager.createCalendar(
+    "storage",
+    Services.io.newURI(`moz-storage-calendar://`)
+  );
+  localCalendarB.id = cal.getUUID();
+  let dbB = localCalendarB.wrappedJSObject.mStorageDb.db;
+
+  Assert.equal(
+    dbA.databaseFile.path,
+    PathUtils.join(PathUtils.profileDir, "calendar-data", "local.sqlite"),
+    "local calendar A uses the right database file"
+  );
+  Assert.equal(
+    dbB.databaseFile.path,
+    PathUtils.join(PathUtils.profileDir, "calendar-data", "local.sqlite"),
+    "local calendar two uses the right database file"
+  );
+  Assert.equal(dbA, dbB, "local calendars share a database connection");
+});
+
+/**
+ * Tests that local storage calendars using the same specified database file share a connection,
+ * and that local storage calendars with a different specified database file do not.
+ */
+add_task(async function testLocalFile() {
+  let testFileA = new FileUtils.File(PathUtils.join(PathUtils.osTempDir, "file-a.sqlite"));
+  let testFileB = new FileUtils.File(PathUtils.join(PathUtils.osTempDir, "file-two.sqlite"));
+
+  let fileCalendarA = cal.manager.createCalendar("storage", Services.io.newFileURI(testFileA));
+  fileCalendarA.id = cal.getUUID();
+  let dbA = fileCalendarA.wrappedJSObject.mStorageDb.db;
+
+  let fileCalendarB = cal.manager.createCalendar("storage", Services.io.newFileURI(testFileB));
+  fileCalendarB.id = cal.getUUID();
+  let dbB = fileCalendarB.wrappedJSObject.mStorageDb.db;
+
+  let fileCalendarC = cal.manager.createCalendar("storage", Services.io.newFileURI(testFileA));
+  fileCalendarC.id = cal.getUUID();
+  let dbC = fileCalendarC.wrappedJSObject.mStorageDb.db;
+
+  Assert.equal(
+    dbA.databaseFile.path,
+    testFileA.path,
+    "local calendar A uses the right database file"
+  );
+  Assert.equal(
+    dbB.databaseFile.path,
+    testFileB.path,
+    "local calendar two uses the right database file"
+  );
+  Assert.equal(
+    dbC.databaseFile.path,
+    testFileA.path,
+    "local calendar three uses the right database file"
+  );
+  Assert.notEqual(
+    dbA,
+    dbB,
+    "calendars with different file URLs do not share a database connection"
+  );
+  Assert.notEqual(
+    dbB,
+    dbC,
+    "calendars with different file URLs do not share a database connection"
+  );
+  Assert.equal(dbA, dbC, "calendars with matching file URLs share a database connection");
+});
+
+/**
+ * Tests that cached network calendars share a database connection.
+ */
+add_task(async function testNetwork() {
+  let networkCalendarA = cal.manager.createCalendar(
+    "ics",
+    Services.io.newURI("http://localhost/ics")
+  );
+  networkCalendarA.id = cal.getUUID();
+  networkCalendarA.setProperty("cache.enabled", true);
+  cal.manager.registerCalendar(networkCalendarA);
+  let dbA = cal.manager.getCalendarById(networkCalendarA.id).wrappedJSObject.mCachedCalendar
+    .wrappedJSObject.mStorageDb.db;
+
+  let networkCalendarB = cal.manager.createCalendar(
+    "caldav",
+    Services.io.newURI("http://localhost/caldav")
+  );
+  networkCalendarB.id = cal.getUUID();
+  networkCalendarB.setProperty("cache.enabled", true);
+  cal.manager.registerCalendar(networkCalendarB);
+  let dbB = cal.manager.getCalendarById(networkCalendarB.id).wrappedJSObject.mCachedCalendar
+    .wrappedJSObject.mStorageDb.db;
+
+  Assert.equal(
+    dbA.databaseFile.path,
+    PathUtils.join(PathUtils.profileDir, "calendar-data", "cache.sqlite"),
+    "network calendar A uses the right database file"
+  );
+  Assert.equal(
+    dbB.databaseFile.path,
+    PathUtils.join(PathUtils.profileDir, "calendar-data", "cache.sqlite"),
+    "network calendar two uses the right database file"
+  );
+  Assert.equal(dbA, dbB, "network calendars share a database connection");
+});
--- a/calendar/test/unit/xpcshell.ini
+++ b/calendar/test/unit/xpcshell.ini
@@ -22,54 +22,55 @@ tags = calendar
 [test_bug356207.js]
 [test_bug485571.js]
 [test_bug486186.js]
 [test_bug494140.js]
 [test_bug523860.js]
 [test_bug653924.js]
 [test_bug668222.js]
 [test_bug759324.js]
+[test_caldav_requests.js]
 [test_CalendarFileImporter.js]
+[test_calIteratorUtils.js]
 [test_calmgr.js]
-[test_caldav_requests.js]
 [test_calreadablestreamfactory.js]
-[test_itip_utils.js]
 [test_data_bags.js]
 [test_datetime.js]
 [test_datetime_before_1970.js]
 [test_datetimeformatter.js]
 [test_deleted_items.js]
 [test_duration.js]
+[test_email_utils.js]
 [test_extract.js]
 [test_extract_parser.js]
 [test_extract_parser_parse.js]
 [test_extract_parser_service.js]
 [test_extract_parser_tokenize.js]
-[test_email_utils.js]
 [test_filter.js]
 [test_filter_mixin.js]
 [test_freebusy.js]
 [test_freebusy_service.js]
 [test_hashedarray.js]
 [test_ics.js]
 [test_ics_parser.js]
 [test_ics_service.js]
 [test_imip.js]
 [test_invitationutils.js]
 [test_items.js]
-[test_calIteratorUtils.js]
 [test_itip_message_sender.js]
+[test_itip_utils.js]
 [test_l10n_utils.js]
 [test_providers.js]
 [test_recur.js]
 [test_recurrence_utils.js]
 [test_relation.js]
 [test_rfc3339_parser.js]
 [test_startup_service.js]
 [test_storage.js]
+[test_storage_connection.js]
 [test_storage_get_items.js]
 [test_timezone.js]
 [test_timezone_changes.js]
 [test_timezone_definition.js]
 [test_transaction_manager.js]
 [test_unifinder_utils.js]
 [test_utils.js]
 [test_view_utils.js]