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

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