tests: fix a small lie
[dconf.git] / tests / dconf-mock-shm.c
blob588667e102fbecf0e35b6623309d947b5f4ffd56
1 #include "../shm/dconf-shm.h"
3 #include "dconf-mock.h"
5 typedef struct
7 guint8 flagged;
8 gint ref_count;
9 } DConfMockShm;
11 static GHashTable *dconf_mock_shm_table;
12 static GMutex dconf_mock_shm_lock;
13 static GString *dconf_mock_shm_log;
15 static void
16 dconf_mock_shm_unref (gpointer data)
18 DConfMockShm *shm = data;
20 if (g_atomic_int_dec_and_test (&shm->ref_count))
21 g_slice_free (DConfMockShm, shm);
24 static DConfMockShm *
25 dconf_mock_shm_ref (DConfMockShm *shm)
27 g_atomic_int_inc (&shm->ref_count);
29 return shm;
32 guint8 *
33 dconf_shm_open (const gchar *name)
35 DConfMockShm *shm;
37 g_mutex_lock (&dconf_mock_shm_lock);
39 if G_UNLIKELY (dconf_mock_shm_table == NULL)
41 dconf_mock_shm_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, dconf_mock_shm_unref);
42 dconf_mock_shm_log = g_string_new (NULL);
45 shm = g_hash_table_lookup (dconf_mock_shm_table, name);
46 if (shm == NULL)
48 shm = g_slice_new0 (DConfMockShm);
49 g_hash_table_insert (dconf_mock_shm_table, g_strdup (name), dconf_mock_shm_ref (shm));
52 /* before unlocking... */
53 dconf_mock_shm_ref (shm);
55 g_string_append_printf (dconf_mock_shm_log, "open %s;", name);
57 g_mutex_unlock (&dconf_mock_shm_lock);
59 return &shm->flagged;
62 void
63 dconf_shm_close (guint8 *shm)
65 if (shm)
67 g_mutex_lock (&dconf_mock_shm_lock);
68 g_string_append (dconf_mock_shm_log, "close;");
69 g_mutex_unlock (&dconf_mock_shm_lock);
71 dconf_mock_shm_unref (shm);
75 gint
76 dconf_mock_shm_flag (const gchar *name)
78 DConfMockShm *shm;
79 gint count = 0;
81 g_mutex_lock (&dconf_mock_shm_lock);
82 shm = g_hash_table_lookup (dconf_mock_shm_table, name);
83 if (shm)
85 shm->flagged = 1;
86 count = shm->ref_count;
87 g_hash_table_remove (dconf_mock_shm_table, name);
89 g_mutex_unlock (&dconf_mock_shm_lock);
91 return count;
94 void
95 dconf_mock_shm_reset (void)
97 g_mutex_lock (&dconf_mock_shm_lock);
98 if (dconf_mock_shm_table != NULL)
100 GHashTableIter iter;
101 gpointer value;
103 g_hash_table_iter_init (&iter, dconf_mock_shm_table);
104 while (g_hash_table_iter_next (&iter, NULL, &value))
106 DConfMockShm *shm = value;
108 g_assert_cmpint (shm->ref_count, ==, 1);
109 g_hash_table_iter_remove (&iter);
112 g_string_truncate (dconf_mock_shm_log, 0);
114 g_mutex_unlock (&dconf_mock_shm_lock);
117 void
118 dconf_mock_shm_assert_log (const gchar *expected_log)
120 g_mutex_lock (&dconf_mock_shm_lock);
121 g_assert_cmpstr (dconf_mock_shm_log->str, ==, expected_log);
122 g_string_truncate (dconf_mock_shm_log, 0);
123 g_mutex_unlock (&dconf_mock_shm_lock);