[dconf/wip/reorg] tests/: implement more realistic shm mocking



commit dd608d06c0868b3dc82105e4b7826ead74510d01
Author: Ryan Lortie <desrt desrt ca>
Date:   Thu Jul 12 01:33:15 2012 -0400

    tests/: implement more realistic shm mocking
    
    Add an implementation of the mock shm based on a hash table.  We can
    use this to check that all shm handles have been properly closed after
    each test.
    
    Soon will come support for flagging the shm regions, by name.

 tests/dconf-mock-shm.c |   73 ++++++++++++++++++++++++++++++++++++++++++++++-
 tests/dconf-mock.h     |    6 ++++
 tests/engine.c         |    5 +++
 3 files changed, 82 insertions(+), 2 deletions(-)
---
diff --git a/tests/dconf-mock-shm.c b/tests/dconf-mock-shm.c
index e9edb1c..3e9c38d 100644
--- a/tests/dconf-mock-shm.c
+++ b/tests/dconf-mock-shm.c
@@ -1,13 +1,82 @@
 #include "../shm/dconf-shm.h"
 
+#include "dconf-mock.h"
+
+typedef struct
+{
+  guint8 flagged;
+  gint   ref_count;
+} DConfMockShm;
+
+static GHashTable *dconf_mock_shm_table;
+static GMutex      dconf_mock_shm_lock;
+
+static void
+dconf_mock_shm_unref (gpointer data)
+{
+  DConfMockShm *shm = data;
+
+  if (g_atomic_int_dec_and_test (&shm->ref_count))
+    g_slice_free (DConfMockShm, shm);
+}
+
+static DConfMockShm *
+dconf_mock_shm_ref (DConfMockShm *shm)
+{
+  g_atomic_int_inc (&shm->ref_count);
+
+  return shm;
+}
+
 guint8 *
 dconf_shm_open (const gchar *name)
 {
-  return g_malloc0 (1);
+  DConfMockShm *shm;
+
+  g_mutex_lock (&dconf_mock_shm_lock);
+
+  if G_UNLIKELY (dconf_mock_shm_table == NULL)
+    dconf_mock_shm_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, dconf_mock_shm_unref);
+
+  shm = g_hash_table_lookup (dconf_mock_shm_table, name);
+  if (shm == NULL)
+    {
+      shm = g_slice_new0 (DConfMockShm);
+      g_hash_table_insert (dconf_mock_shm_table, g_strdup (name), dconf_mock_shm_ref (shm));
+    }
+
+  /* before unlocking... */
+  dconf_mock_shm_ref (shm);
+
+  g_mutex_unlock (&dconf_mock_shm_lock);
+
+  return &shm->flagged;
 }
 
 void
 dconf_shm_close (guint8 *shm)
 {
-  g_free (shm);
+  if (shm)
+    dconf_mock_shm_unref (shm);
+}
+
+void
+dconf_mock_shm_reset (void)
+{
+  g_mutex_lock (&dconf_mock_shm_lock);
+  if (dconf_mock_shm_table != NULL)
+    {
+      GHashTableIter iter;
+      gpointer value;
+
+      g_hash_table_iter_init (&iter, dconf_mock_shm_table);
+      while (g_hash_table_iter_next (&iter, NULL, &value))
+        {
+          DConfMockShm *shm = value;
+
+          g_assert_cmpint (shm->ref_count, ==, 1);
+          g_hash_table_iter_remove (&iter);
+        }
+    }
+  g_mutex_unlock (&dconf_mock_shm_lock);
 }
diff --git a/tests/dconf-mock.h b/tests/dconf-mock.h
new file mode 100644
index 0000000..8ce7f1f
--- /dev/null
+++ b/tests/dconf-mock.h
@@ -0,0 +1,6 @@
+#ifndef __dconf_mock_h__
+#define __dconf_mock_h__
+
+void                    dconf_mock_shm_reset                            (void);
+
+#endif
diff --git a/tests/engine.c b/tests/engine.c
index 50d8362..ea29e37 100644
--- a/tests/engine.c
+++ b/tests/engine.c
@@ -2,6 +2,7 @@
 
 #include "../engine/dconf-engine.h"
 #include "../engine/dconf-engine-profile.h"
+#include "dconf-mock.h"
 #include <stdlib.h>
 #include <stdio.h>
 #include <dlfcn.h>
@@ -176,6 +177,8 @@ test_profile_parser (void)
   g_assert_cmpstr (sources[0]->name, ==, "user");
   dconf_engine_source_free (sources[0]);
   g_free (sources);
+
+  dconf_mock_shm_reset ();
 }
 
 static gpointer
@@ -217,6 +220,8 @@ test_signal_threadsafety (void)
                                      "/ca/desrt/dconf/Writer/user",
                                      "Notify", parameters);
   g_variant_unref (parameters);
+
+  dconf_mock_shm_reset ();
 }
 
 int



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]