3 * unit test for GstObject
5 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
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.
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.
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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
27 #include <gst/check/gstcheck.h>
30 Create a fake subclass
32 typedef struct _GstFakeObjectClass GstFakeObjectClass;
33 typedef struct _GstFakeObject GstFakeObject;
40 struct _GstFakeObjectClass
42 GstObjectClass parent_class;
45 //static GstObjectClass *parent_class = NULL;
46 //static guint gst_fake_object_signals[LAST_SIGNAL] = { 0 };
49 gst_fake_object_get_type (void)
51 static volatile gsize fake_object_type = 0;
53 if (g_once_init_enter (&fake_object_type)) {
55 static const GTypeInfo fake_object_info = {
56 sizeof (GstFakeObjectClass),
57 NULL, //gst_fake_object_base_class_init,
58 NULL, //gst_fake_object_base_class_finalize,
59 NULL, //(GClassInitFunc) gst_fake_object_class_init,
62 sizeof (GstFakeObject),
64 NULL, //(GInstanceInitFunc) gst_fake_object_init,
68 type = g_type_register_static (GST_TYPE_OBJECT,
69 "GstFakeObject", &fake_object_info, 0);
70 g_once_init_leave (&fake_object_type, type);
72 return fake_object_type;
75 /* g_object_new on GstFakeObject should succeed */
76 GST_START_TEST (test_fake_object_new)
80 object = g_object_new (gst_fake_object_get_type (), NULL);
81 fail_if (object == NULL, "Failed to create instance of GstFakeObject");
82 fail_unless (GST_IS_OBJECT (object),
83 "GstFakeObject instance is not a GstObject");
84 gst_object_unref (object);
90 notify_name (GObject * object, GParamSpec * pspec, gint * out_count)
95 /* GstFakeObject name tests */
96 GST_START_TEST (test_fake_object_name)
98 GstObject *object, *parent;
103 object = g_object_new (gst_fake_object_get_type (), NULL);
104 g_signal_connect (object, "notify::name", G_CALLBACK (notify_name), &count);
106 name = gst_object_get_name (object);
107 fail_if (name == NULL, "Newly created object has no name");
108 fail_if (strncmp (name, "fakeobject", 10) != 0,
109 "Random name %s does not start with Gst", name);
112 /* give a random name by setting with NULL;
113 * GstFakeObject class -> fakeobject%d */
114 fail_unless (gst_object_set_name (object, NULL), "Could not set name");
115 name = gst_object_get_name (object);
116 fail_if (name == NULL, "Random name was not assigned");
117 fail_if (strncmp (name, "fakeobject", 10) != 0,
118 "Random name %s does not start with Gst", name);
120 fail_unless (count == 1, "Name change was not notified");
122 /* also test the property code path */
123 g_object_set (object, "name", "fake", NULL);
124 name = gst_object_get_name (object);
125 fail_if (name == NULL, "Failed to get name of GstFakeObject");
126 fail_if (strcmp (name, "fake") != 0, "Name of GstFakeObject is not 'fake'");
127 fail_if (count > 2, "Name change was notified multiple time");
128 fail_unless (count == 2, "Name change was not notified");
130 /* change the gotten name to see that it's a copy and not the original */
132 name2 = gst_object_get_name (object);
133 fail_if (strcmp (name2, "fake") != 0,
134 "Copy of object name affected actual object name");
138 /* add a parent and ensure name cannot be changed */
139 parent = g_object_new (gst_fake_object_get_type (), NULL);
140 gst_object_set_parent (object, parent);
141 fail_if (gst_object_set_name (object, "broken"),
142 "Could set name on parented object");
144 gst_object_unref (parent);
149 /* thread function for threaded name change test */
151 thread_name_object (GstObject * object)
153 gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());
157 /* give main thread a head start */
160 /* write our name repeatedly */
161 g_message ("THREAD %s: starting loop", thread_id);
162 while (THREAD_TEST_RUNNING ()) {
163 gst_object_set_name (object, thread_id);
164 /* a minimal sleep invokes a thread switch */
168 /* thread is done, so let's return */
169 g_message ("THREAD %s: set name", thread_id);
176 GST_START_TEST (test_fake_object_name_threaded_wrong)
181 gboolean expected_failure = FALSE;
183 g_message ("\nTEST: set/get without lock");
185 object = g_object_new (gst_fake_object_get_type (), NULL);
186 gst_object_set_name (object, "main");
188 MAIN_START_THREADS (5, thread_name_object, object);
190 /* start looping and set/get name repeatedly */
191 for (i = 0; i < 1000; ++i) {
192 gst_object_set_name (object, "main");
194 name = gst_object_get_name (object);
195 if (strcmp (name, "main") != 0) {
196 g_message ("MAIN: expected failure during run %d", i);
197 expected_failure = TRUE;
203 MAIN_STOP_THREADS ();
205 gst_object_unref (object);
207 fail_unless (expected_failure, "name did not get changed");
214 * main thread sets and gets name directly on struct inside the object lock
215 * succeed because lock is held during set/get, and threads are locked out
217 GST_START_TEST (test_fake_object_name_threaded_right)
223 g_message ("\nTEST: set/get inside lock");
225 object = g_object_new (gst_fake_object_get_type (), NULL);
226 gst_object_set_name (object, "main");
228 MAIN_START_THREADS (5, thread_name_object, object);
230 /* start looping and set/get name repeatedly */
231 for (i = 0; i < 1000; ++i) {
232 GST_OBJECT_LOCK (object);
233 g_free (GST_OBJECT_NAME (object));
234 GST_OBJECT_NAME (object) = g_strdup ("main");
236 name = g_strdup (GST_OBJECT_NAME (object));
237 GST_OBJECT_UNLOCK (object);
239 fail_unless (strcmp (name, "main") == 0,
240 "Name got changed while lock held during run %d", i);
243 MAIN_STOP_THREADS ();
244 gst_object_unref (object);
249 * main thread creates lots of objects
250 * child threads sets default names on objects
251 * then main thread checks uniqueness of object names
254 static GList *object_list = NULL;
255 static gint num_objects = 1000;
256 static gint num_threads = 5;
258 /* thread function for threaded default name change test */
260 thread_name_object_default (int *i)
266 for (j = *i; j < num_objects; j += num_threads) {
267 GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));
269 /* g_message ("THREAD %p: setting default name on object %d",
270 g_thread_self (), j); */
271 gst_object_set_name (o, NULL);
275 /* thread is done, so let's return */
276 g_message ("THREAD %p: set name", g_thread_self ());
283 gst_object_name_compare (GstObject * o, GstObject * p)
290 if (o->name == NULL && p->name == NULL) {
292 } else if (o->name == NULL) {
294 } else if (p->name == NULL) {
297 result = strcmp (o->name, p->name);
300 GST_OBJECT_UNLOCK (p);
301 GST_OBJECT_UNLOCK (o);
306 GST_START_TEST (test_fake_object_name_threaded_unique)
311 gchar *name1, *name2;
314 g_message ("\nTEST: uniqueness of default names");
316 for (i = 0; i < num_objects; ++i) {
317 object = g_object_new (gst_fake_object_get_type (), NULL);
318 object_list = g_list_append (object_list, object);
324 for (i = 0; i < num_threads; ++i) {
325 ip = g_new (gint, 1);
327 MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
333 MAIN_STOP_THREADS ();
335 /* sort GList based on object name */
336 /* FIXME: sort and test */
338 g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
340 name1 = gst_object_get_name (GST_OBJECT (object_list->data));
341 for (l = object_list->next; l->next; l = l->next) {
342 g_message ("object with name %s", name1);
343 name2 = gst_object_get_name (GST_OBJECT (l->data));
344 fail_if (strcmp (name1, name2) == 0, "Two objects with name %s", name2);
351 g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
356 /* parentage test on GstFakeObject */
357 GST_START_TEST (test_fake_object_parentage)
359 GstObject *object1, *object2;
363 /* create new object */
364 object1 = g_object_new (gst_fake_object_get_type (), NULL);
365 fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
366 fail_unless (GST_IS_OBJECT (object1),
367 "GstFakeObject instance is not a GstObject");
368 fail_unless (g_object_is_floating (object1),
369 "GstFakeObject instance is not floating");
371 /* check the parent */
372 parent = gst_object_get_parent (object1);
373 fail_if (parent != NULL, "GstFakeObject has parent");
374 /* try to set a NULL parent, this should give a warning */
375 ASSERT_CRITICAL (result = gst_object_set_parent (object1, NULL));
376 fail_if (result, "GstFakeObject accepted NULL parent");
377 /* try to set itself as parent, we expect a warning here */
378 ASSERT_CRITICAL (result = gst_object_set_parent (object1, object1));
379 fail_if (result, "GstFakeObject accepted itself as parent");
381 /* _has_parent always returns FALSE if there is no parent */
382 fail_if (gst_object_has_as_parent (object1, NULL));
383 fail_if (gst_object_has_as_parent (NULL, object1));
384 fail_if (gst_object_has_as_parent (object1, object1));
386 /* should still be floating */
387 fail_unless (g_object_is_floating (object1),
388 "GstFakeObject instance is not floating");
390 /* create another object */
391 object2 = g_object_new (gst_fake_object_get_type (), NULL);
392 fail_if (object2 == NULL,
393 "Failed to create another instance of GstFakeObject");
394 fail_unless (GST_IS_OBJECT (object2),
395 "second GstFakeObject instance is not a GstObject");
396 fail_unless (g_object_is_floating (object1),
397 "GstFakeObject instance is not floating");
399 result = gst_object_has_as_parent (object1, object2);
400 fail_if (result, "GstFakeObject has a parent");
402 /* try to set other object as parent */
403 result = gst_object_set_parent (object1, object2);
404 fail_unless (result, "GstFakeObject could not accept other object as parent");
406 /* should not be floating anymore */
407 fail_if (g_object_is_floating (object1),
408 "GstFakeObject instance is still floating");
409 /* parent should still be floating */
410 fail_unless (g_object_is_floating (object2),
411 "GstFakeObject instance is not floating");
413 /* check the parent */
414 fail_unless (gst_object_has_as_parent (object1, object2));
416 /* any other combination is invalid */
417 fail_if (gst_object_has_as_parent (object2, object1));
418 fail_if (gst_object_has_as_parent (object1, NULL));
419 fail_if (gst_object_has_as_parent (object2, NULL));
420 fail_if (gst_object_has_as_parent (NULL, object1));
421 fail_if (gst_object_has_as_parent (NULL, object2));
422 fail_if (gst_object_has_as_parent (object1, object1));
423 fail_if (gst_object_has_as_parent (object2, object2));
425 /* try to set other object as parent again */
426 result = gst_object_set_parent (object1, object2);
427 fail_if (result, "GstFakeObject could set parent twice");
429 /* ref before unparenting */
430 gst_object_ref (object1);
431 /* clear parent of object */
432 gst_object_unparent (object1);
434 /* check the parent */
435 parent = gst_object_get_parent (object1);
436 fail_if (parent != NULL, "GstFakeObject has parent");
438 /* object should not be floating */
439 fail_if (g_object_is_floating (object1),
440 "GstFakeObject instance is floating again");
442 gst_object_unref (object1);
443 gst_object_unref (object2);
448 /* parentage test dispose on GstFakeObject, since our testcase
449 * does not handle the parent relation completely, the parent does
450 * not hold a ref to the child, we cannot dispose the parent to
451 * dipose the child as well. This test needs to be run with DEBUG
452 * info to check if the finalize methods are called correctly. */
453 GST_START_TEST (test_fake_object_parentage_dispose)
455 GstObject *object1, *object2;
458 object1 = g_object_new (gst_fake_object_get_type (), NULL);
459 fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
461 object2 = g_object_new (gst_fake_object_get_type (), NULL);
462 fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
464 /* try to set other object as parent */
465 result = gst_object_set_parent (object1, object2);
466 fail_unless (result, "GstFakeObject could not accept other object as parent");
468 /* clear parent of object */
469 gst_object_unparent (object1);
471 /* now dispose parent */
472 gst_object_unref (object2);
477 GST_START_TEST (test_fake_object_has_as_ancestor)
479 GstObject *object1, *object2, *object3, *object4;
482 object1 = g_object_new (gst_fake_object_get_type (), NULL);
483 fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
485 object2 = g_object_new (gst_fake_object_get_type (), NULL);
486 fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");
488 object3 = g_object_new (gst_fake_object_get_type (), NULL);
489 fail_if (object3 == NULL, "Failed to create instance of GstFakeObject");
491 object4 = g_object_new (gst_fake_object_get_type (), NULL);
492 fail_if (object4 == NULL, "Failed to create instance of GstFakeObject");
494 /* try to set other object as parent */
495 result = gst_object_set_parent (object1, object3);
496 fail_unless (result, "GstFakeObject could not accept other object as parent");
497 result = gst_object_set_parent (object2, object3);
498 fail_unless (result, "GstFakeObject could not accept other object as parent");
499 result = gst_object_set_parent (object3, object4);
500 fail_unless (result, "GstFakeObject could not accept other object as parent");
509 /* An object isn't its own parent, but it is its own ancestor */
510 fail_if (gst_object_has_as_parent (object1, object1));
511 fail_unless (gst_object_has_as_ancestor (object1, object1));
513 fail_if (gst_object_has_as_parent (object4, object4));
514 fail_unless (gst_object_has_as_ancestor (object4, object4));
517 fail_unless (gst_object_has_as_parent (object1, object3));
518 fail_unless (gst_object_has_as_ancestor (object1, object3));
520 fail_unless (gst_object_has_as_parent (object2, object3));
521 fail_unless (gst_object_has_as_ancestor (object2, object3));
523 fail_unless (gst_object_has_as_parent (object3, object4));
524 fail_unless (gst_object_has_as_ancestor (object3, object4));
527 fail_if (gst_object_has_as_parent (object1, object4));
528 fail_unless (gst_object_has_as_ancestor (object1, object4));
530 fail_if (gst_object_has_as_parent (object2, object4));
531 fail_unless (gst_object_has_as_ancestor (object2, object4));
534 fail_if (gst_object_has_as_parent (object1, object2));
535 fail_if (gst_object_has_as_ancestor (object1, object2));
537 fail_if (gst_object_has_as_parent (object3, object1));
538 fail_if (gst_object_has_as_ancestor (object3, object1));
540 fail_if (gst_object_has_as_parent (object4, object1));
541 fail_if (gst_object_has_as_ancestor (object4, object1));
543 fail_if (gst_object_has_as_parent (object4, object3));
544 fail_if (gst_object_has_as_ancestor (object4, object3));
546 /* unparent everything */
547 gst_object_unparent (object3);
548 gst_object_unparent (object2);
549 gst_object_unparent (object1);
551 /* now dispose objects */
552 gst_object_unref (object4);
557 /* test: try renaming a parented object, make sure it fails */
560 gst_object_suite (void)
562 Suite *s = suite_create ("GstObject");
563 TCase *tc_chain = tcase_create ("general");
565 /* turn off timeout */
566 tcase_set_timeout (tc_chain, 60);
568 suite_add_tcase (s, tc_chain);
569 tcase_add_test (tc_chain, test_fake_object_new);
570 tcase_add_test (tc_chain, test_fake_object_name);
572 tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
574 tcase_add_test (tc_chain, test_fake_object_name_threaded_right);
575 tcase_add_test (tc_chain, test_fake_object_name_threaded_unique);
576 tcase_add_test (tc_chain, test_fake_object_parentage);
577 tcase_add_test (tc_chain, test_fake_object_parentage_dispose);
579 tcase_add_test (tc_chain, test_fake_object_has_as_ancestor);
580 //tcase_add_checked_fixture (tc_chain, setup, teardown);
585 GST_CHECK_MAIN (gst_object);