1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Imendio AB
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
22 #define GLIB_DISABLE_DEPRECATION_WARNINGS
24 #include <glib-object.h>
26 static volatile int mtsafe_call_counter
= 0; /* multi thread safe call counter */
27 static int unsafe_call_counter
= 0; /* single-threaded call counter */
29 #define NUM_COUNTER_INCREMENTS 100000
32 call_counter_init (gpointer tclass
)
35 for (i
= 0; i
< NUM_COUNTER_INCREMENTS
; i
++)
37 int saved_unsafe_call_counter
= unsafe_call_counter
;
38 g_atomic_int_add (&mtsafe_call_counter
, 1); /* real call count update */
39 g_thread_yield(); /* let concurrent threads corrupt the unsafe_call_counter state */
40 unsafe_call_counter
= 1 + saved_unsafe_call_counter
; /* non-atomic counter update */
44 static void interface_per_class_init () { call_counter_init (NULL
); }
46 /* define 3 test interfaces */
47 typedef GTypeInterface MyFace0Interface
;
48 static GType
my_face0_get_type (void);
49 G_DEFINE_INTERFACE (MyFace0
, my_face0
, G_TYPE_OBJECT
);
50 static void my_face0_default_init (MyFace0Interface
*iface
) { call_counter_init (iface
); }
51 typedef GTypeInterface MyFace1Interface
;
52 static GType
my_face1_get_type (void);
53 G_DEFINE_INTERFACE (MyFace1
, my_face1
, G_TYPE_OBJECT
);
54 static void my_face1_default_init (MyFace1Interface
*iface
) { call_counter_init (iface
); }
55 typedef GTypeInterface MyFace2Interface
;
56 static GType
my_face2_get_type (void);
57 G_DEFINE_INTERFACE (MyFace2
, my_face2
, G_TYPE_OBJECT
);
58 static void my_face2_default_init (MyFace2Interface
*iface
) { call_counter_init (iface
); }
60 /* define 3 test objects, adding interfaces 0 & 1, and adding interface 2 after class initialization */
61 typedef GObject MyTester0
;
62 typedef GObjectClass MyTester0Class
;
63 static GType
my_tester0_get_type (void);
64 G_DEFINE_TYPE_WITH_CODE (MyTester0
, my_tester0
, G_TYPE_OBJECT
,
65 G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init
);
66 G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init
);
68 static void my_tester0_init (MyTester0
*t
) {}
69 static void my_tester0_class_init (MyTester0Class
*c
) { call_counter_init (c
); }
70 typedef GObject MyTester1
;
71 typedef GObjectClass MyTester1Class
;
72 static GType
my_tester1_get_type (void);
73 G_DEFINE_TYPE_WITH_CODE (MyTester1
, my_tester1
, G_TYPE_OBJECT
,
74 G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init
);
75 G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init
);
77 static void my_tester1_init (MyTester1
*t
) {}
78 static void my_tester1_class_init (MyTester1Class
*c
) { call_counter_init (c
); }
79 typedef GObject MyTester2
;
80 typedef GObjectClass MyTester2Class
;
81 static GType
my_tester2_get_type (void);
82 G_DEFINE_TYPE_WITH_CODE (MyTester2
, my_tester2
, G_TYPE_OBJECT
,
83 G_IMPLEMENT_INTERFACE (my_face0_get_type(), interface_per_class_init
);
84 G_IMPLEMENT_INTERFACE (my_face1_get_type(), interface_per_class_init
);
86 static void my_tester2_init (MyTester2
*t
) {}
87 static void my_tester2_class_init (MyTester2Class
*c
) { call_counter_init (c
); }
89 static GCond sync_cond
;
90 static GMutex sync_mutex
;
93 tester_init_thread (gpointer data
)
95 const GInterfaceInfo face2_interface_info
= { (GInterfaceInitFunc
) interface_per_class_init
, NULL
, NULL
};
97 /* first, syncronize with other threads,
98 * then run interface and class initializers,
99 * using unsafe_call_counter concurrently
101 g_mutex_lock (&sync_mutex
);
102 g_mutex_unlock (&sync_mutex
);
103 /* test default interface initialization for face0 */
104 g_type_default_interface_unref (g_type_default_interface_ref (my_face0_get_type()));
105 /* test class initialization, face0 per-class initializer, face1 default and per-class initializer */
106 klass
= g_type_class_ref ((GType
) data
);
107 /* test face2 default and per-class initializer, after class_init */
108 g_type_add_interface_static (G_TYPE_FROM_CLASS (klass
), my_face2_get_type(), &face2_interface_info
);
110 g_type_class_unref (klass
);
115 test_threaded_class_init (void)
117 /* pause newly created threads */
118 g_mutex_lock (&sync_mutex
);
120 g_thread_create (tester_init_thread
, (gpointer
) my_tester0_get_type(), TRUE
, NULL
);
121 g_thread_create (tester_init_thread
, (gpointer
) my_tester1_get_type(), TRUE
, NULL
);
122 g_thread_create (tester_init_thread
, (gpointer
) my_tester2_get_type(), TRUE
, NULL
);
123 /* execute threads */
124 g_mutex_unlock (&sync_mutex
);
125 while (g_atomic_int_get (&mtsafe_call_counter
) < (3 + 3 + 3 * 3) * NUM_COUNTER_INCREMENTS
)
127 if (g_test_verbose())
128 g_print ("Initializers counted: %u\n", g_atomic_int_get (&mtsafe_call_counter
));
129 g_usleep (50 * 1000); /* wait for threads to complete */
131 if (g_test_verbose())
132 g_print ("Total initializers: %u\n", g_atomic_int_get (&mtsafe_call_counter
));
133 /* ensure non-corrupted counter updates */
134 g_assert_cmpint (g_atomic_int_get (&mtsafe_call_counter
), ==, unsafe_call_counter
);
141 typedef GObjectClass PropTesterClass
;
142 static GType
prop_tester_get_type (void);
143 G_DEFINE_TYPE (PropTester
, prop_tester
, G_TYPE_OBJECT
);
146 prop_tester_init (PropTester
* t
)
149 ; /* neds unit test framework initialization: g_test_bug ("race initializing properties"); */
152 prop_tester_set_property (GObject
*object
,
158 prop_tester_class_init (PropTesterClass
*c
)
162 GObjectClass
*gobject_class
= G_OBJECT_CLASS (c
);
164 gobject_class
->set_property
= prop_tester_set_property
; /* silence GObject checks */
166 g_mutex_lock (&sync_mutex
);
167 g_cond_signal (&sync_cond
);
168 g_mutex_unlock (&sync_mutex
);
170 for (i
= 0; i
< 100; i
++) /* wait a bit. */
173 call_counter_init (c
);
174 param
= g_param_spec_string ("name", "name_i18n",
175 "yet-more-wasteful-i18n",
177 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_WRITABLE
|
178 G_PARAM_STATIC_NAME
| G_PARAM_STATIC_BLURB
|
179 G_PARAM_STATIC_NICK
);
180 g_object_class_install_property (gobject_class
, PROP_NAME
, param
);
184 object_create (gpointer data
)
186 GObject
*obj
= g_object_new (prop_tester_get_type(), "name", "fish", NULL
);
187 g_object_unref (obj
);
192 test_threaded_object_init (void)
195 g_mutex_lock (&sync_mutex
);
197 creator
= g_thread_create (object_create
, NULL
, TRUE
, NULL
);
198 /* really provoke the race */
199 g_cond_wait (&sync_cond
, &sync_mutex
);
201 object_create (NULL
);
202 g_mutex_unlock (&sync_mutex
);
204 g_thread_join (creator
);
213 unref_in_thread (gpointer p
)
215 UnrefInThreadData
*data
= p
;
217 g_usleep (data
->unref_delay
);
218 g_object_unref (data
->strong
);
223 /* undefine to see this test fail without GWeakRef */
224 #define HAVE_G_WEAK_REF
226 #define SLEEP_MIN_USEC 1
227 #define SLEEP_MAX_USEC 10
230 test_threaded_weak_ref (void)
233 guint get_wins
= 0, unref_wins
= 0;
236 if (g_test_thorough ())
237 n
= NUM_COUNTER_INCREMENTS
;
239 n
= NUM_COUNTER_INCREMENTS
/ 20;
241 for (i
= 0; i
< n
; i
++)
243 UnrefInThreadData data
;
244 #ifdef HAVE_G_WEAK_REF
245 /* GWeakRef<MyTester0> in C++ terms */
250 MyTester0
*strengthened
;
253 GError
*error
= NULL
;
255 if (g_test_verbose () && (i
% (n
/20)) == 0)
256 g_print ("%u%%\n", ((i
* 100) / n
));
258 /* Have an object and a weak ref to it */
259 data
.strong
= g_object_new (my_tester0_get_type (), NULL
);
261 #ifdef HAVE_G_WEAK_REF
262 g_weak_ref_init (&weak
, data
.strong
);
265 g_object_add_weak_pointer ((GObject
*) weak
, &weak
);
268 /* Delay for a random time on each side of the race, to perturb the
269 * timing. Ideally, we want each side to win half the races; on
270 * smcv's laptop, these timings are about right.
272 data
.unref_delay
= g_random_int_range (SLEEP_MIN_USEC
/ 2, SLEEP_MAX_USEC
/ 2);
273 get_delay
= g_random_int_range (SLEEP_MIN_USEC
, SLEEP_MAX_USEC
);
275 /* One half of the race is to unref the shared object */
276 thread
= g_thread_create (unref_in_thread
, &data
, TRUE
, &error
);
277 g_assert_no_error (error
);
279 /* The other half of the race is to get the object from the "global
282 g_usleep (get_delay
);
284 #ifdef HAVE_G_WEAK_REF
285 strengthened
= g_weak_ref_get (&weak
);
287 /* Spot the unsafe pointer access! In GDBusConnection this is rather
288 * better-hidden, but ends up with essentially the same thing, albeit
289 * cleared in dispose() rather than by a traditional weak pointer
293 if (strengthened
!= NULL
)
294 g_object_ref (strengthened
);
297 if (strengthened
!= NULL
)
298 g_assert (G_IS_OBJECT (strengthened
));
300 /* Wait for the thread to run */
301 g_thread_join (thread
);
303 if (strengthened
!= NULL
)
306 g_assert (G_IS_OBJECT (strengthened
));
307 g_object_unref (strengthened
);
314 #ifdef HAVE_G_WEAK_REF
315 g_weak_ref_clear (&weak
);
318 g_object_remove_weak_pointer (weak
, &weak
);
322 if (g_test_verbose ())
323 g_print ("Race won by get %u times, unref %u times\n",
324 get_wins
, unref_wins
);
331 g_test_init (&argc
, &argv
, NULL
);
334 g_test_add_func ("/GObject/threaded-class-init", test_threaded_class_init
);
335 g_test_add_func ("/GObject/threaded-object-init", test_threaded_object_init
);
336 g_test_add_func ("/GObject/threaded-weak-ref", test_threaded_weak_ref
);