ea08fb51e731e39fbbb9e80e8f336b7967154d19
1 /* GStreamer
2 *
3 * unit test for GstObject
4 *
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
23 #include <gst/check/gstcheck.h>
25 /*
26 Create a fake subclass
27 */
28 typedef struct _GstFakeObjectClass GstFakeObjectClass;
29 typedef struct _GstFakeObject GstFakeObject;
31 struct _GstFakeObject
32 {
33 GstObject object;
34 };
36 struct _GstFakeObjectClass
37 {
38 GstObjectClass parent_class;
39 };
41 GType _gst_fake_object_type = 0;
43 //static GstObjectClass *parent_class = NULL;
44 //static guint gst_fake_object_signals[LAST_SIGNAL] = { 0 };
46 GType
47 gst_fake_object_get_type (void)
48 {
49 if (!_gst_fake_object_type) {
50 static const GTypeInfo fake_object_info = {
51 sizeof (GstFakeObjectClass),
52 NULL, //gst_fake_object_base_class_init,
53 NULL, //gst_fake_object_base_class_finalize,
54 NULL, //(GClassInitFunc) gst_fake_object_class_init,
55 NULL,
56 NULL,
57 sizeof (GstFakeObject),
58 0,
59 NULL, //(GInstanceInitFunc) gst_fake_object_init,
60 NULL
61 };
63 _gst_fake_object_type = g_type_register_static (GST_TYPE_OBJECT,
64 "GstFakeObject", &fake_object_info, 0);
65 }
66 return _gst_fake_object_type;
67 }
69 /* g_object_new on abstract GstObject should fail */
70 GST_START_TEST (test_fail_abstract_new)
71 {
72 GstObject *object;
74 ASSERT_CRITICAL (object = g_object_new (gst_object_get_type (), NULL));
75 fail_unless (object == NULL, "Created an instance of abstract GstObject");
76 }
78 GST_END_TEST
79 /* g_object_new on GstFakeObject should succeed */
80 GST_START_TEST (test_fake_object_new)
81 {
82 GstObject *object;
84 object = g_object_new (gst_fake_object_get_type (), NULL);
85 fail_if (object == NULL, "Failed to create instance of GstFakeObject");
86 fail_unless (GST_IS_OBJECT (object),
87 "GstFakeObject instance is not a GstObject");
88 gst_object_unref (object);
89 }
91 GST_END_TEST
92 /* GstFakeObject name tests */
93 GST_START_TEST (test_fake_object_name)
94 {
95 GstObject *object;
96 gchar *name;
97 gchar *name2;
99 object = g_object_new (gst_fake_object_get_type (), NULL);
101 name = gst_object_get_name (object);
102 fail_if (name == NULL, "Newly created object has no name");
103 fail_if (strncmp (name, "fakeobject", 10) != 0,
104 "Random name %s does not start with Gst", name);
105 g_free (name);
107 /* give a random name by setting with NULL;
108 * GstFakeObject class -> fakeobject%d */
109 gst_object_set_name (object, NULL);
110 name = gst_object_get_name (object);
111 fail_if (name == NULL, "Random name was not assigned");
112 fail_if (strncmp (name, "fakeobject", 10) != 0,
113 "Random name %s does not start with Gst", name);
114 g_free (name);
116 gst_object_set_name (object, "fake");
117 name = gst_object_get_name (object);
118 fail_if (name == NULL, "Failed to get name of GstFakeObject");
119 fail_if (strcmp (name, "fake") != 0, "Name of GstFakeObject is not 'fake'");
121 /* change the gotten name to see that it's a copy and not the original */
122 name[0] = 'm';
123 name2 = gst_object_get_name (object);
124 fail_if (strcmp (name2, "fake") != 0,
125 "Copy of object name affected actual object name");
126 g_free (name);
127 g_free (name2);
129 gst_object_unref (object);
130 }
132 GST_END_TEST
133 /* thread function for threaded name change test */
134 gpointer thread_name_object (GstObject * object)
135 {
136 gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
138 THREAD_START ();
140 /* give main thread a head start */
141 g_usleep (100000);
143 /* write our name repeatedly */
144 g_message ("THREAD %s: starting loop\n", thread_id);
145 while (THREAD_TEST_RUNNING ()) {
146 gst_object_set_name (object, thread_id);
147 /* a minimal sleep invokes a thread switch */
148 THREAD_SWITCH ();
149 }
151 /* thread is done, so let's return */
152 g_message ("THREAD %s: set name\n", thread_id);
153 g_free (thread_id);
155 return NULL;
156 }
158 /*
159 * main thread sets and gets name while other threads set the name
160 * constantly; fails because lock is released inbetween set and get
161 */
163 GST_START_TEST (test_fake_object_name_threaded_wrong)
164 {
165 GstObject *object;
166 gchar *name;
167 gint i;
168 gboolean expected_failure = FALSE;
170 g_message ("\nTEST: set/get without lock\n");
172 object = g_object_new (gst_fake_object_get_type (), NULL);
173 gst_object_set_name (object, "main");
175 MAIN_START_THREADS (5, thread_name_object, object);
177 /* start looping and set/get name repeatedly */
178 for (i = 0; i < 1000; ++i) {
179 gst_object_set_name (object, "main");
180 THREAD_SWITCH ();
181 name = gst_object_get_name (object);
182 if (strcmp (name, "main") != 0) {
183 g_message ("MAIN: expected failure during run %d\n", i);
184 expected_failure = TRUE;
185 g_free (name);
186 break;
187 }
188 g_free (name);
189 }
190 MAIN_STOP_THREADS ();
192 gst_object_unref (object);
194 fail_unless (expected_failure, "name did not get changed");
195 }
197 GST_END_TEST
198 /*
199 * main thread sets and gets name directly on struct inside the object lock
200 * succeed because lock is held during set/get, and threads are locked out
201 */
202 GST_START_TEST (test_fake_object_name_threaded_right)
203 {
204 GstObject *object;
205 gchar *name;
206 gint i;
208 g_message ("\nTEST: set/get inside lock\n");
210 object = g_object_new (gst_fake_object_get_type (), NULL);
211 gst_object_set_name (object, "main");
213 MAIN_START_THREADS (5, thread_name_object, object);
215 /* start looping and set/get name repeatedly */
216 for (i = 0; i < 1000; ++i) {
217 GST_OBJECT_LOCK (object);
218 g_free (GST_OBJECT_NAME (object));
219 GST_OBJECT_NAME (object) = g_strdup ("main");
220 THREAD_SWITCH ();
221 name = g_strdup (GST_OBJECT_NAME (object));
222 GST_OBJECT_UNLOCK (object);
224 fail_unless (strcmp (name, "main") == 0,
225 "Name got changed while lock held during run %d", i);
226 g_free (name);
227 }
228 MAIN_STOP_THREADS ();
229 gst_object_unref (object);
230 }
232 GST_END_TEST
233 /*
234 * main thread creates lots of objects
235 * child threads sets default names on objects
236 * then main thread checks uniqueness of object names
237 */
238 GList * object_list = NULL;
239 gint num_objects = 1000;
240 gint num_threads = 5;
242 /* thread function for threaded default name change test */
243 gpointer
244 thread_name_object_default (int *i)
245 {
246 int j;
248 THREAD_START ();
250 for (j = *i; j < num_objects; j += num_threads) {
251 GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
253 /* g_message ("THREAD %p: setting default name on object %d\n",
254 g_thread_self (), j); */
255 gst_object_set_name (o, NULL);
256 THREAD_SWITCH ();
257 }
259 /* thread is done, so let's return */
260 g_message ("THREAD %p: set name\n", g_thread_self ());
261 g_free (i);
263 return NULL;
264 }
266 static gint
267 gst_object_name_compare (GstObject * o, GstObject * p)
268 {
269 gint result;
271 GST_OBJECT_LOCK (o);
272 GST_OBJECT_LOCK (p);
274 if (o->name == NULL && p->name == NULL) {
275 result = 0;
276 } else if (o->name == NULL) {
277 result = -1;
278 } else if (p->name == NULL) {
279 result = 1;
280 } else {
281 result = strcmp (o->name, p->name);
282 }
284 GST_OBJECT_UNLOCK (p);
285 GST_OBJECT_UNLOCK (o);
287 return result;
288 }
290 GST_START_TEST (test_fake_object_name_threaded_unique)
291 {
292 GstObject *object;
293 gint i;
294 gint *ip;
295 gchar *name1, *name2;
296 GList *l;
298 g_message ("\nTEST: uniqueness of default names\n");
300 for (i = 0; i < num_objects; ++i) {
301 object = g_object_new (gst_fake_object_get_type (), NULL);
302 object_list = g_list_append (object_list, object);
303 }
305 MAIN_INIT ();
307 mark_point ();
308 for (i = 0; i < num_threads; ++i) {
309 ip = g_new (gint, 1);
310 *ip = i;
311 MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
312 }
314 mark_point ();
315 MAIN_SYNCHRONIZE ();
316 mark_point ();
317 MAIN_STOP_THREADS ();
319 /* sort GList based on object name */
320 /* FIXME: sort and test */
321 object_list =
322 g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
324 name1 = gst_object_get_name (GST_OBJECT (object_list->data));
325 for (l = object_list->next; l->next; l = l->next) {
326 g_message ("object with name %s\n", name1);
327 name2 = gst_object_get_name (GST_OBJECT (l->data));
328 fail_if (strcmp (name1, name2) == 0, "Two objects with name %s", name2);
329 g_free (name1);
330 name1 = name2;
331 }
332 g_free (name1);
334 /* free stuff */
335 g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
336 }
338 GST_END_TEST
339 /* parentage test on GstFakeObject */
340 GST_START_TEST (test_fake_object_parentage)
341 {
342 GstObject *object1, *object2;
343 GstObject *parent;
344 gboolean result;
346 /* create new object */
347 object1 = g_object_new (gst_fake_object_get_type (), NULL);
348 fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
349 fail_unless (GST_IS_OBJECT (object1),
350 "GstFakeObject instance is not a GstObject");
351 fail_unless (GST_OBJECT_IS_FLOATING (object1),
352 "GstFakeObject instance is not floating");
354 /* check the parent */
355 parent = gst_object_get_parent (object1);
356 fail_if (parent != NULL, "GstFakeObject has parent");
357 /* try to set a NULL parent, this should give a warning */
358 ASSERT_CRITICAL (result = gst_object_set_parent (object1, NULL));
359 fail_if (result == TRUE, "GstFakeObject accepted NULL parent");
360 /* try to set itself as parent, we expect a warning here */
361 ASSERT_CRITICAL (result = gst_object_set_parent (object1, object1));
362 fail_if (result == TRUE, "GstFakeObject accepted itself as parent");
364 /* should still be floating */
365 fail_unless (GST_OBJECT_IS_FLOATING (object1),
366 "GstFakeObject instance is not floating");
368 /* create another object */
369 object2 = g_object_new (gst_fake_object_get_type (), NULL);
370 fail_if (object2 == NULL,
371 "Failed to create another instance of GstFakeObject");
372 fail_unless (GST_IS_OBJECT (object2),
373 "second GstFakeObject instance is not a GstObject");
374 fail_unless (GST_OBJECT_IS_FLOATING (object1),
375 "GstFakeObject instance is not floating");
377 /* try to set other object as parent */
378 result = gst_object_set_parent (object1, object2);
379 fail_if (result == FALSE,
380 "GstFakeObject could not accept other object as parent");
382 /* should not be floating anymore */
383 fail_if (GST_OBJECT_IS_FLOATING (object1),
384 "GstFakeObject instance is still floating");
385 /* parent should still be floating */
386 fail_unless (GST_OBJECT_IS_FLOATING (object2),
387 "GstFakeObject instance is not floating");
389 /* check the parent */
390 parent = gst_object_get_parent (object1);
391 fail_if (parent != object2, "GstFakeObject has wrong parent");
392 gst_object_unref (parent);
393 /* try to set other object as parent again */
394 result = gst_object_set_parent (object1, object2);
395 fail_if (result == TRUE, "GstFakeObject could set parent twice");
397 /* ref before unparenting */
398 gst_object_ref (object1);
399 /* clear parent of object */
400 gst_object_unparent (object1);
402 /* check the parent */
403 parent = gst_object_get_parent (object1);
404 fail_if (parent != NULL, "GstFakeObject has parent");
406 /* object should not be floating */
407 fail_if (GST_OBJECT_IS_FLOATING (object1),
408 "GstFakeObject instance is floating again");
410 gst_object_unref (object1);
411 gst_object_unref (object2);
412 }
414 GST_END_TEST
415 /* parentage test dispose on GstFakeObject, since our testcase
416 * does not handle the parent relation completely, the parent does
417 * not hold a ref to the child, we cannot dispose the parent to
418 * dipose the child as well. This test needs to be run with DEBUG
419 * info to check if the finalize methods are called correctly. */
420 GST_START_TEST (test_fake_object_parentage_dispose)
421 {
422 GstObject *object1, *object2;
423 gboolean result;
425 object1 = g_object_new (gst_fake_object_get_type (), NULL);
426 fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
428 object2 = g_object_new (gst_fake_object_get_type (), NULL);
429 fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
431 /* try to set other object as parent */
432 result = gst_object_set_parent (object1, object2);
433 fail_if (result == FALSE,
434 "GstFakeObject could not accept other object as parent");
436 /* clear parent of object */
437 gst_object_unparent (object1);
439 /* now dispose parent */
440 gst_object_unref (object2);
441 }
443 GST_END_TEST
444 /* test: try renaming a parented object, make sure it fails */
445 Suite * gst_object_suite (void)
446 {
447 Suite *s = suite_create ("GstObject");
448 TCase *tc_chain = tcase_create ("general");
450 /* turn off timeout */
451 tcase_set_timeout (tc_chain, 60);
453 suite_add_tcase (s, tc_chain);
454 tcase_add_test (tc_chain, test_fake_object_new);
455 tcase_add_test (tc_chain, test_fake_object_name);
456 tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
457 tcase_add_test (tc_chain, test_fake_object_name_threaded_right);
458 tcase_add_test (tc_chain, test_fake_object_name_threaded_unique);
459 tcase_add_test (tc_chain, test_fake_object_parentage);
460 tcase_add_test (tc_chain, test_fake_object_parentage_dispose);
461 //tcase_add_checked_fixture (tc_chain, setup, teardown);
463 /* SEGV tests go last so we can debug the others */
464 tcase_add_test_raise_signal (tc_chain, test_fail_abstract_new, SIGSEGV);
465 return s;
466 }
468 GST_CHECK_MAIN (gst_object);