Blame SOURCES/0005-Engine-Add-comprehensive-unit-tests-for-subscription.patch

545ecd
From 8760833820e03a21900afda2d2f5785610c59ac9 Mon Sep 17 00:00:00 2001
545ecd
From: Daniel Playfair Cal <daniel.playfair.cal@gmail.com>
545ecd
Date: Sat, 28 Jul 2018 13:38:00 +1000
545ecd
Subject: [PATCH 5/5] Engine: Add comprehensive unit tests for subscription
545ecd
 counting behaviour
545ecd
545ecd
Use g_assert_false instead of g_assert in unit tests
545ecd
---
545ecd
 tests/engine.c | 206 +++++++++++++++++++++++++++++++++++++++++++++++++
545ecd
 1 file changed, 206 insertions(+)
545ecd
545ecd
diff --git a/tests/engine.c b/tests/engine.c
545ecd
index 038c04c..f2e57b2 100644
545ecd
--- a/tests/engine.c
545ecd
+++ b/tests/engine.c
545ecd
@@ -1232,6 +1232,185 @@ test_watch_fast (void)
545ecd
   g_variant_unref (triv);
545ecd
 }
545ecd
 
545ecd
+static void
545ecd
+test_watch_fast_simultaneous_subscriptions (void)
545ecd
+{
545ecd
+  /**
545ecd
+   * Test that creating multiple subscriptions to the same path
545ecd
+   * simultaneously (before receiving replies from D-Bus) only results in
545ecd
+   * a single D-Bus match rule, and that it is removed at the right time.
545ecd
+   */
545ecd
+  DConfEngine *engine;
545ecd
+  GvdbTable *table;
545ecd
+  GVariant *triv;
545ecd
+
545ecd
+  /* Set up */
545ecd
+  table = dconf_mock_gvdb_table_new ();
545ecd
+  dconf_mock_gvdb_install ("/HOME/.config/dconf/user", table);
545ecd
+  table = dconf_mock_gvdb_table_new ();
545ecd
+  dconf_mock_gvdb_install ("/etc/dconf/db/site", table);
545ecd
+
545ecd
+  triv = g_variant_ref_sink (g_variant_new ("()"));
545ecd
+
545ecd
+  change_log = g_string_new (NULL);
545ecd
+
545ecd
+  engine = dconf_engine_new (SRCDIR "/profile/dos", NULL, NULL);
545ecd
+
545ecd
+
545ecd
+  /* Subscribe to the same path 3 times. Both AddMatch results succeed
545ecd
+   * (one for each source). There is only one for each source*path.
545ecd
+   */
545ecd
+  dconf_engine_watch_fast (engine, "/a/b/c");
545ecd
+  dconf_engine_watch_fast (engine, "/a/b/c");
545ecd
+  dconf_engine_watch_fast (engine, "/a/b/c");
545ecd
+
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_assert_no_async ();
545ecd
+
545ecd
+  /* Unsubscribe twice, after the AddMatch succeeds. There is still one
545ecd
+   * active subscription, so no RemoveMatch request is sent. */
545ecd
+  dconf_engine_unwatch_fast (engine, "/a/b/c");
545ecd
+  dconf_engine_unwatch_fast (engine, "/a/b/c");
545ecd
+
545ecd
+  dconf_mock_dbus_assert_no_async ();
545ecd
+
545ecd
+  /* Unsubscribe once more. The number of active subscriptions falls to 0
545ecd
+   * and the D-Bus match rule is removed */
545ecd
+  dconf_engine_unwatch_fast (engine, "/a/b/c");
545ecd
+
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_assert_no_async ();
545ecd
+
545ecd
+  /* The shm was not flagged at any point - so no change notifications
545ecd
+   * should not have been sent */
545ecd
+  g_assert_cmpstr (change_log->str, ==, "");
545ecd
+
545ecd
+  /* Clean up */
545ecd
+  dconf_engine_unref (engine);
545ecd
+  g_string_free (change_log, TRUE);
545ecd
+  change_log = NULL;
545ecd
+  g_variant_unref (triv);
545ecd
+}
545ecd
+
545ecd
+static void
545ecd
+test_watch_fast_successive_subscriptions (void)
545ecd
+{
545ecd
+  /**
545ecd
+   * Test that subscribing to the same path multiple times successively
545ecd
+   * (after waiting for any expected replies from D-Bus) results in only
545ecd
+   * a single D-Bus match rule being created, and that it is created and
545ecd
+   * destroyed at the right times.
545ecd
+   */
545ecd
+  DConfEngine *engine;
545ecd
+  GvdbTable *table;
545ecd
+  GVariant *triv;
545ecd
+
545ecd
+  /* Set up */
545ecd
+  table = dconf_mock_gvdb_table_new ();
545ecd
+  dconf_mock_gvdb_install ("/HOME/.config/dconf/user", table);
545ecd
+  table = dconf_mock_gvdb_table_new ();
545ecd
+  dconf_mock_gvdb_install ("/etc/dconf/db/site", table);
545ecd
+
545ecd
+  triv = g_variant_ref_sink (g_variant_new ("()"));
545ecd
+
545ecd
+  change_log = g_string_new (NULL);
545ecd
+
545ecd
+  engine = dconf_engine_new (SRCDIR "/profile/dos", NULL, NULL);
545ecd
+
545ecd
+  /* Subscribe to a path, and simulate a change to the database while the
545ecd
+   * AddMatch request is in progress */
545ecd
+  dconf_engine_watch_fast (engine, "/a/b/c");
545ecd
+  dconf_mock_shm_flag ("user");
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+
545ecd
+  /* When the AddMatch requests succeeds, expect a change notification
545ecd
+   * for the path */
545ecd
+  dconf_mock_dbus_assert_no_async ();
545ecd
+  g_assert_cmpstr (change_log->str, ==, "/a/b/c:1::nil;");
545ecd
+
545ecd
+  /* Subscribe to a path twice again, and simulate a change to the
545ecd
+   * database */
545ecd
+  dconf_engine_watch_fast (engine, "/a/b/c");
545ecd
+  dconf_engine_watch_fast (engine, "/a/b/c");
545ecd
+  dconf_mock_shm_flag ("user");
545ecd
+
545ecd
+  /* There was already a match rule in place, so there should be no D-Bus
545ecd
+   * requests and no change notifications */
545ecd
+  dconf_mock_dbus_assert_no_async ();
545ecd
+  g_assert_cmpstr (change_log->str, ==, "/a/b/c:1::nil;");
545ecd
+
545ecd
+  /* Unsubscribe */
545ecd
+  dconf_engine_unwatch_fast (engine, "/a/b/c");
545ecd
+  dconf_engine_unwatch_fast (engine, "/a/b/c");
545ecd
+  dconf_mock_dbus_assert_no_async ();
545ecd
+  dconf_engine_unwatch_fast (engine, "/a/b/c");
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_assert_no_async ();
545ecd
+
545ecd
+
545ecd
+  /* Clean up */
545ecd
+  dconf_engine_unref (engine);
545ecd
+  g_string_free (change_log, TRUE);
545ecd
+  change_log = NULL;
545ecd
+  g_variant_unref (triv);
545ecd
+}
545ecd
+
545ecd
+static void
545ecd
+test_watch_fast_short_lived_subscriptions (void)
545ecd
+{
545ecd
+  /**
545ecd
+   * Test that subscribing and then immediately unsubscribing (without
545ecd
+   * waiting for replies from D-Bus) multiple times to the same path
545ecd
+   * correctly triggers D-Bus requests and change notifications in cases
545ecd
+   * where the D-Bus match rule was not in place when the database was
545ecd
+   * changed.
545ecd
+   */
545ecd
+  DConfEngine *engine;
545ecd
+  GvdbTable *table;
545ecd
+  GVariant *triv;
545ecd
+
545ecd
+  /* Set up */
545ecd
+  table = dconf_mock_gvdb_table_new ();
545ecd
+  dconf_mock_gvdb_install ("/HOME/.config/dconf/user", table);
545ecd
+  table = dconf_mock_gvdb_table_new ();
545ecd
+  dconf_mock_gvdb_install ("/etc/dconf/db/site", table);
545ecd
+
545ecd
+  triv = g_variant_ref_sink (g_variant_new ("()"));
545ecd
+
545ecd
+  change_log = g_string_new (NULL);
545ecd
+
545ecd
+  engine = dconf_engine_new (SRCDIR "/profile/dos", NULL, NULL);
545ecd
+
545ecd
+  /* Subscribe to a path twice, and simulate a change to the database
545ecd
+   * while the AddMatch request is in progress */
545ecd
+  dconf_engine_watch_fast (engine, "/a/b/c");
545ecd
+  dconf_engine_watch_fast (engine, "/a/b/c");
545ecd
+  dconf_mock_shm_flag ("user");
545ecd
+  dconf_engine_unwatch_fast (engine, "/a/b/c");
545ecd
+  dconf_engine_unwatch_fast (engine, "/a/b/c");
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_async_reply (triv, NULL);
545ecd
+  dconf_mock_dbus_assert_no_async ();
545ecd
+
545ecd
+  /* When the AddMatch requests succeed, expect a change notification
545ecd
+   * to have been sent for the path, even though the client has since
545ecd
+   * unsubscribed. */
545ecd
+  g_assert_cmpstr (change_log->str, ==, "/a/b/c:1::nil;");
545ecd
+
545ecd
+
545ecd
+  /* Clean up */
545ecd
+  dconf_engine_unref (engine);
545ecd
+  g_string_free (change_log, TRUE);
545ecd
+  change_log = NULL;
545ecd
+  g_variant_unref (triv);
545ecd
+}
545ecd
+
545ecd
 static const gchar *match_request_type;
545ecd
 static gboolean got_match_request[5];
545ecd
 
545ecd
@@ -1270,13 +1449,37 @@ test_watch_sync (void)
545ecd
   engine = dconf_engine_new (SRCDIR "/profile/dos", NULL, NULL);
545ecd
 
545ecd
   match_request_type = "AddMatch";
545ecd
+
545ecd
+  /* A match rule should be added when the first subscription is established */
545ecd
   dconf_engine_watch_sync (engine, "/a/b/c");
545ecd
   g_assert (got_match_request[G_BUS_TYPE_SESSION]);
545ecd
   g_assert (got_match_request[G_BUS_TYPE_SYSTEM]);
545ecd
   got_match_request[G_BUS_TYPE_SESSION] = FALSE;
545ecd
   got_match_request[G_BUS_TYPE_SYSTEM] = FALSE;
545ecd
 
545ecd
+  /* The match rule is now already in place, so more are not needed */
545ecd
+  dconf_engine_watch_sync (engine, "/a/b/c");
545ecd
+  g_assert_false (got_match_request[G_BUS_TYPE_SESSION]);
545ecd
+  g_assert_false (got_match_request[G_BUS_TYPE_SYSTEM]);
545ecd
+
545ecd
+  dconf_engine_watch_sync (engine, "/a/b/c");
545ecd
+  g_assert_false (got_match_request[G_BUS_TYPE_SESSION]);
545ecd
+  g_assert_false (got_match_request[G_BUS_TYPE_SYSTEM]);
545ecd
+
545ecd
   match_request_type = "RemoveMatch";
545ecd
+
545ecd
+  /* There are 3 subscriptions, so removing 2 should not remove
545ecd
+   * the match rule */
545ecd
+  dconf_engine_unwatch_sync (engine, "/a/b/c");
545ecd
+  g_assert_false (got_match_request[G_BUS_TYPE_SESSION]);
545ecd
+  g_assert_false (got_match_request[G_BUS_TYPE_SYSTEM]);
545ecd
+
545ecd
+  dconf_engine_unwatch_sync (engine, "/a/b/c");
545ecd
+  g_assert_false (got_match_request[G_BUS_TYPE_SESSION]);
545ecd
+  g_assert_false (got_match_request[G_BUS_TYPE_SYSTEM]);
545ecd
+
545ecd
+  /* The match rule should be removed when the last subscription is
545ecd
+   * removed */
545ecd
   dconf_engine_unwatch_sync (engine, "/a/b/c");
545ecd
   g_assert (got_match_request[G_BUS_TYPE_SESSION]);
545ecd
   g_assert (got_match_request[G_BUS_TYPE_SYSTEM]);
545ecd
@@ -1773,6 +1976,9 @@ main (int argc, char **argv)
545ecd
   g_test_add_func ("/engine/sources/service", test_service_source);
545ecd
   g_test_add_func ("/engine/read", test_read);
545ecd
   g_test_add_func ("/engine/watch/fast", test_watch_fast);
545ecd
+  g_test_add_func ("/engine/watch/fast/simultaneous", test_watch_fast_simultaneous_subscriptions);
545ecd
+  g_test_add_func ("/engine/watch/fast/successive", test_watch_fast_successive_subscriptions);
545ecd
+  g_test_add_func ("/engine/watch/fast/short_lived", test_watch_fast_short_lived_subscriptions);
545ecd
   g_test_add_func ("/engine/watch/sync", test_watch_sync);
545ecd
   g_test_add_func ("/engine/change/fast", test_change_fast);
545ecd
   g_test_add_func ("/engine/change/sync", test_change_sync);
545ecd
-- 
545ecd
2.20.1
545ecd