gstobject.c 15 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* GStreamer
 *
 * unit test for GstObject
 *
 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23
24
25
26
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

27
#include <gst/check/gstcheck.h>
Wim Taymans's avatar
Wim Taymans committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

/*
  Create a fake subclass
 */
typedef struct _GstFakeObjectClass GstFakeObjectClass;
typedef struct _GstFakeObject GstFakeObject;

struct _GstFakeObject
{
  GstObject object;
};

struct _GstFakeObjectClass
{
  GstObjectClass parent_class;
};

//static GstObjectClass *parent_class = NULL;
//static guint gst_fake_object_signals[LAST_SIGNAL] = { 0 };

48
static GType
Wim Taymans's avatar
Wim Taymans committed
49
50
gst_fake_object_get_type (void)
{
51
52
53
54
  static volatile gsize fake_object_type = 0;

  if (g_once_init_enter (&fake_object_type)) {
    GType type;
Wim Taymans's avatar
Wim Taymans committed
55
56
57
58
59
60
61
62
63
64
65
66
67
    static const GTypeInfo fake_object_info = {
      sizeof (GstFakeObjectClass),
      NULL,                     //gst_fake_object_base_class_init,
      NULL,                     //gst_fake_object_base_class_finalize,
      NULL,                     //(GClassInitFunc) gst_fake_object_class_init,
      NULL,
      NULL,
      sizeof (GstFakeObject),
      0,
      NULL,                     //(GInstanceInitFunc) gst_fake_object_init,
      NULL
    };

68
    type = g_type_register_static (GST_TYPE_OBJECT,
Wim Taymans's avatar
Wim Taymans committed
69
        "GstFakeObject", &fake_object_info, 0);
70
    g_once_init_leave (&fake_object_type, type);
Wim Taymans's avatar
Wim Taymans committed
71
  }
72
  return fake_object_type;
Wim Taymans's avatar
Wim Taymans committed
73
74
75
}

/* g_object_new on GstFakeObject should succeed */
76
GST_START_TEST (test_fake_object_new)
Wim Taymans's avatar
Wim Taymans committed
77
78
79
80
81
82
83
{
  GstObject *object;

  object = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object == NULL, "Failed to create instance of GstFakeObject");
  fail_unless (GST_IS_OBJECT (object),
      "GstFakeObject instance is not a GstObject");
84
  gst_object_unref (object);
Wim Taymans's avatar
Wim Taymans committed
85
86
}

87
88
GST_END_TEST;

Wim Taymans's avatar
Wim Taymans committed
89
/* GstFakeObject name tests */
90
GST_START_TEST (test_fake_object_name)
Wim Taymans's avatar
Wim Taymans committed
91
92
93
94
95
96
97
98
99
100
101
{
  GstObject *object;
  gchar *name;
  gchar *name2;

  object = g_object_new (gst_fake_object_get_type (), NULL);

  name = gst_object_get_name (object);
  fail_if (name == NULL, "Newly created object has no name");
  fail_if (strncmp (name, "fakeobject", 10) != 0,
      "Random name %s does not start with Gst", name);
102
  g_free (name);
Wim Taymans's avatar
Wim Taymans committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

  /* give a random name by setting with NULL;
   * GstFakeObject class -> fakeobject%d */
  gst_object_set_name (object, NULL);
  name = gst_object_get_name (object);
  fail_if (name == NULL, "Random name was not assigned");
  fail_if (strncmp (name, "fakeobject", 10) != 0,
      "Random name %s does not start with Gst", name);
  g_free (name);

  gst_object_set_name (object, "fake");
  name = gst_object_get_name (object);
  fail_if (name == NULL, "Failed to get name of GstFakeObject");
  fail_if (strcmp (name, "fake") != 0, "Name of GstFakeObject is not 'fake'");

  /* change the gotten name to see that it's a copy and not the original */
  name[0] = 'm';
  name2 = gst_object_get_name (object);
  fail_if (strcmp (name2, "fake") != 0,
      "Copy of object name affected actual object name");
  g_free (name);
  g_free (name2);
125
126

  gst_object_unref (object);
Wim Taymans's avatar
Wim Taymans committed
127
128
}

129
130
GST_END_TEST;

Wim Taymans's avatar
Wim Taymans committed
131
/* thread function for threaded name change test */
132
133
static gpointer
thread_name_object (GstObject * object)
Wim Taymans's avatar
Wim Taymans committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
{
  gchar *thread_id = g_strdup_printf ("%p", g_thread_self ());

  THREAD_START ();

  /* give main thread a head start */
  g_usleep (100000);

  /* write our name repeatedly */
  g_message ("THREAD %s: starting loop\n", thread_id);
  while (THREAD_TEST_RUNNING ()) {
    gst_object_set_name (object, thread_id);
    /* a minimal sleep invokes a thread switch */
    THREAD_SWITCH ();
  }

  /* thread is done, so let's return */
  g_message ("THREAD %s: set name\n", thread_id);
  g_free (thread_id);

  return NULL;
}

157
#if 0
158
GST_START_TEST (test_fake_object_name_threaded_wrong)
Wim Taymans's avatar
Wim Taymans committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
{
  GstObject *object;
  gchar *name;
  gint i;
  gboolean expected_failure = FALSE;

  g_message ("\nTEST: set/get without lock\n");

  object = g_object_new (gst_fake_object_get_type (), NULL);
  gst_object_set_name (object, "main");

  MAIN_START_THREADS (5, thread_name_object, object);

  /* start looping and set/get name repeatedly */
  for (i = 0; i < 1000; ++i) {
    gst_object_set_name (object, "main");
    THREAD_SWITCH ();
    name = gst_object_get_name (object);
    if (strcmp (name, "main") != 0) {
      g_message ("MAIN: expected failure during run %d\n", i);
      expected_failure = TRUE;
      g_free (name);
      break;
    }
    g_free (name);
  }
  MAIN_STOP_THREADS ();

187
188
  gst_object_unref (object);

Wim Taymans's avatar
Wim Taymans committed
189
190
191
  fail_unless (expected_failure, "name did not get changed");
}

192
193
194
GST_END_TEST;
#endif

Wim Taymans's avatar
Wim Taymans committed
195
196
197
198
/*
 * main thread sets and gets name directly on struct inside the object lock
 * succeed because lock is held during set/get, and threads are locked out
 */
199
GST_START_TEST (test_fake_object_name_threaded_right)
Wim Taymans's avatar
Wim Taymans committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{
  GstObject *object;
  gchar *name;
  gint i;

  g_message ("\nTEST: set/get inside lock\n");

  object = g_object_new (gst_fake_object_get_type (), NULL);
  gst_object_set_name (object, "main");

  MAIN_START_THREADS (5, thread_name_object, object);

  /* start looping and set/get name repeatedly */
  for (i = 0; i < 1000; ++i) {
214
    GST_OBJECT_LOCK (object);
Wim Taymans's avatar
Wim Taymans committed
215
216
217
218
    g_free (GST_OBJECT_NAME (object));
    GST_OBJECT_NAME (object) = g_strdup ("main");
    THREAD_SWITCH ();
    name = g_strdup (GST_OBJECT_NAME (object));
219
    GST_OBJECT_UNLOCK (object);
Wim Taymans's avatar
Wim Taymans committed
220
221
222
223
224
225

    fail_unless (strcmp (name, "main") == 0,
        "Name got changed while lock held during run %d", i);
    g_free (name);
  }
  MAIN_STOP_THREADS ();
226
  gst_object_unref (object);
Wim Taymans's avatar
Wim Taymans committed
227
228
}

229
GST_END_TEST;
Wim Taymans's avatar
Wim Taymans committed
230
231
232
233
234
/*
 * main thread creates lots of objects
 * child threads sets default names on objects
 * then main thread checks uniqueness of object names
 */
235
236
237
238

static GList *object_list = NULL;
static gint num_objects = 1000;
static gint num_threads = 5;
Wim Taymans's avatar
Wim Taymans committed
239
240

/* thread function for threaded default name change test */
241
static gpointer
Wim Taymans's avatar
Wim Taymans committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
thread_name_object_default (int *i)
{
  int j;

  THREAD_START ();

  for (j = *i; j < num_objects; j += num_threads) {
    GstObject *o = GST_OBJECT (g_list_nth_data (object_list, j));

    /* g_message ("THREAD %p: setting default name on object %d\n",
       g_thread_self (), j); */
    gst_object_set_name (o, NULL);
    THREAD_SWITCH ();
  }

  /* thread is done, so let's return */
  g_message ("THREAD %p: set name\n", g_thread_self ());
  g_free (i);

  return NULL;
}

static gint
gst_object_name_compare (GstObject * o, GstObject * p)
{
  gint result;

269
270
  GST_OBJECT_LOCK (o);
  GST_OBJECT_LOCK (p);
Wim Taymans's avatar
Wim Taymans committed
271
272
273
274
275
276
277
278
279
280
281

  if (o->name == NULL && p->name == NULL) {
    result = 0;
  } else if (o->name == NULL) {
    result = -1;
  } else if (p->name == NULL) {
    result = 1;
  } else {
    result = strcmp (o->name, p->name);
  }

282
283
  GST_OBJECT_UNLOCK (p);
  GST_OBJECT_UNLOCK (o);
Wim Taymans's avatar
Wim Taymans committed
284
285
286
287

  return result;
}

288
GST_START_TEST (test_fake_object_name_threaded_unique)
Wim Taymans's avatar
Wim Taymans committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{
  GstObject *object;
  gint i;
  gint *ip;
  gchar *name1, *name2;
  GList *l;

  g_message ("\nTEST: uniqueness of default names\n");

  for (i = 0; i < num_objects; ++i) {
    object = g_object_new (gst_fake_object_get_type (), NULL);
    object_list = g_list_append (object_list, object);
  }

  MAIN_INIT ();

  mark_point ();
  for (i = 0; i < num_threads; ++i) {
    ip = g_new (gint, 1);
    *ip = i;
    MAIN_START_THREAD_FUNCTION (i, thread_name_object_default, ip);
  }

  mark_point ();
  MAIN_SYNCHRONIZE ();
  mark_point ();
  MAIN_STOP_THREADS ();

  /* sort GList based on object name */
  /* FIXME: sort and test */
319
320
  object_list =
      g_list_sort (object_list, (GCompareFunc) gst_object_name_compare);
Wim Taymans's avatar
Wim Taymans committed
321
322
323
324
325
326
327
328
329

  name1 = gst_object_get_name (GST_OBJECT (object_list->data));
  for (l = object_list->next; l->next; l = l->next) {
    g_message ("object with name %s\n", name1);
    name2 = gst_object_get_name (GST_OBJECT (l->data));
    fail_if (strcmp (name1, name2) == 0, "Two objects with name %s", name2);
    g_free (name1);
    name1 = name2;
  }
330
  g_free (name1);
Wim Taymans's avatar
Wim Taymans committed
331
332
333
334
335

  /* free stuff */
  g_list_foreach (object_list, (GFunc) g_object_unref, NULL);
}

336
337
GST_END_TEST;

Wim Taymans's avatar
Wim Taymans committed
338
/* parentage test on GstFakeObject */
339
GST_START_TEST (test_fake_object_parentage)
Wim Taymans's avatar
Wim Taymans committed
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
{
  GstObject *object1, *object2;
  GstObject *parent;
  gboolean result;

  /* create new object */
  object1 = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");
  fail_unless (GST_IS_OBJECT (object1),
      "GstFakeObject instance is not a GstObject");
  fail_unless (GST_OBJECT_IS_FLOATING (object1),
      "GstFakeObject instance is not floating");

  /* check the parent */
  parent = gst_object_get_parent (object1);
  fail_if (parent != NULL, "GstFakeObject has parent");
  /* try to set a NULL parent, this should give a warning */
  ASSERT_CRITICAL (result = gst_object_set_parent (object1, NULL));
  fail_if (result == TRUE, "GstFakeObject accepted NULL parent");
  /* try to set itself as parent, we expect a warning here */
  ASSERT_CRITICAL (result = gst_object_set_parent (object1, object1));
  fail_if (result == TRUE, "GstFakeObject accepted itself as parent");

  /* should still be floating */
  fail_unless (GST_OBJECT_IS_FLOATING (object1),
      "GstFakeObject instance is not floating");

  /* create another object */
  object2 = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object2 == NULL,
      "Failed to create another instance of GstFakeObject");
  fail_unless (GST_IS_OBJECT (object2),
      "second GstFakeObject instance is not a GstObject");
  fail_unless (GST_OBJECT_IS_FLOATING (object1),
      "GstFakeObject instance is not floating");

  /* try to set other object as parent */
  result = gst_object_set_parent (object1, object2);
  fail_if (result == FALSE,
      "GstFakeObject could not accept other object as parent");

  /* should not be floating anymore */
  fail_if (GST_OBJECT_IS_FLOATING (object1),
      "GstFakeObject instance is still floating");
  /* parent should still be floating */
  fail_unless (GST_OBJECT_IS_FLOATING (object2),
      "GstFakeObject instance is not floating");

  /* check the parent */
  parent = gst_object_get_parent (object1);
  fail_if (parent != object2, "GstFakeObject has wrong parent");
  gst_object_unref (parent);
  /* try to set other object as parent again */
  result = gst_object_set_parent (object1, object2);
  fail_if (result == TRUE, "GstFakeObject could set parent twice");

  /* ref before unparenting */
  gst_object_ref (object1);
  /* clear parent of object */
  gst_object_unparent (object1);

  /* check the parent */
  parent = gst_object_get_parent (object1);
  fail_if (parent != NULL, "GstFakeObject has parent");

  /* object should not be floating */
  fail_if (GST_OBJECT_IS_FLOATING (object1),
      "GstFakeObject instance is floating again");

  gst_object_unref (object1);
  gst_object_unref (object2);
}

413
414
GST_END_TEST;

Wim Taymans's avatar
Wim Taymans committed
415
416
417
418
419
/* parentage test dispose on GstFakeObject, since our testcase
 * does not handle the parent relation completely, the parent does
 * not hold a ref to the child, we cannot dispose the parent to
 * dipose the child as well. This test needs to be run with DEBUG
 * info to check if the finalize methods are called correctly. */
420
GST_START_TEST (test_fake_object_parentage_dispose)
Wim Taymans's avatar
Wim Taymans committed
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
{
  GstObject *object1, *object2;
  gboolean result;

  object1 = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");

  object2 = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");

  /* try to set other object as parent */
  result = gst_object_set_parent (object1, object2);
  fail_if (result == FALSE,
      "GstFakeObject could not accept other object as parent");

  /* clear parent of object */
  gst_object_unparent (object1);

  /* now dispose parent */
  gst_object_unref (object2);
}

443
444
GST_END_TEST;

445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
GST_START_TEST (test_fake_object_has_ancestor)
{
  GstObject *object1, *object2, *object3, *object4;
  gboolean result;

  object1 = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object1 == NULL, "Failed to create instance of GstFakeObject");

  object2 = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object2 == NULL, "Failed to create instance of GstFakeObject");

  object3 = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object3 == NULL, "Failed to create instance of GstFakeObject");

  object4 = g_object_new (gst_fake_object_get_type (), NULL);
  fail_if (object4 == NULL, "Failed to create instance of GstFakeObject");

  /* try to set other object as parent */
  result = gst_object_set_parent (object1, object3);
  fail_if (result == FALSE,
      "GstFakeObject could not accept other object as parent");
  result = gst_object_set_parent (object2, object3);
  fail_if (result == FALSE,
      "GstFakeObject could not accept other object as parent");
  result = gst_object_set_parent (object3, object4);
  fail_if (result == FALSE,
      "GstFakeObject could not accept other object as parent");

  fail_unless (gst_object_has_ancestor (object1, object1));
  fail_if (gst_object_has_ancestor (object1, object2));
  fail_unless (gst_object_has_ancestor (object1, object3));
  fail_unless (gst_object_has_ancestor (object1, object4));
  fail_if (gst_object_has_ancestor (object3, object1));
  fail_if (gst_object_has_ancestor (object4, object1));
  fail_unless (gst_object_has_ancestor (object3, object4));
  fail_if (gst_object_has_ancestor (object4, object3));
  fail_unless (gst_object_has_ancestor (object4, object4));

  /* unparent everything */
  gst_object_unparent (object3);
  gst_object_unparent (object2);
  gst_object_unparent (object1);

  /* now dispose objects */
  gst_object_unref (object4);
}

GST_END_TEST;

Wim Taymans's avatar
Wim Taymans committed
494
/* test: try renaming a parented object, make sure it fails */
495
496
497

static Suite *
gst_object_suite (void)
Wim Taymans's avatar
Wim Taymans committed
498
499
500
501
{
  Suite *s = suite_create ("GstObject");
  TCase *tc_chain = tcase_create ("general");

502
503
504
  /* turn off timeout */
  tcase_set_timeout (tc_chain, 60);

Wim Taymans's avatar
Wim Taymans committed
505
506
507
  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_fake_object_new);
  tcase_add_test (tc_chain, test_fake_object_name);
508
#if 0
Wim Taymans's avatar
Wim Taymans committed
509
  tcase_add_test (tc_chain, test_fake_object_name_threaded_wrong);
510
#endif
Wim Taymans's avatar
Wim Taymans committed
511
512
  tcase_add_test (tc_chain, test_fake_object_name_threaded_right);
  tcase_add_test (tc_chain, test_fake_object_name_threaded_unique);
Wim Taymans's avatar
Wim Taymans committed
513
514
  tcase_add_test (tc_chain, test_fake_object_parentage);
  tcase_add_test (tc_chain, test_fake_object_parentage_dispose);
515
516

  tcase_add_test (tc_chain, test_fake_object_has_ancestor);
Wim Taymans's avatar
Wim Taymans committed
517
518
519
520
521
  //tcase_add_checked_fixture (tc_chain, setup, teardown);

  return s;
}

522
GST_CHECK_MAIN (gst_object);