check_thread_stress.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Check: a unit test framework for C
  3. * Copyright (C) 2001, 2002 Arien Malec
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include "../lib/libcompat.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <check.h>
  24. Suite *s;
  25. TCase *tc;
  26. SRunner *sr;
  27. #if defined (HAVE_PTHREAD) || defined (HAVE_FORK)
  28. static void *
  29. sendinfo (void *userdata CK_ATTRIBUTE_UNUSED)
  30. {
  31. unsigned int i;
  32. for (i = 0; i < 999; i++)
  33. {
  34. ck_assert_msg (1, "Shouldn't see this message");
  35. }
  36. return NULL;
  37. }
  38. #endif /* HAVE_PTHREAD || HAVE_FORK */
  39. #ifdef HAVE_PTHREAD
  40. START_TEST (test_stress_threads)
  41. {
  42. pthread_t a, b;
  43. pthread_create (&a, NULL, sendinfo, (void *) 0xa);
  44. pthread_create (&b, NULL, sendinfo, (void *) 0xb);
  45. pthread_join (a, NULL);
  46. pthread_join (b, NULL);
  47. }
  48. END_TEST
  49. #endif /* HAVE_PTHREAD */
  50. #if defined(HAVE_FORK) && HAVE_FORK==1
  51. START_TEST (test_stress_forks)
  52. {
  53. pid_t cpid = fork ();
  54. if (cpid == 0)
  55. {
  56. /* child */
  57. sendinfo ((void *) 0x1);
  58. exit (EXIT_SUCCESS);
  59. }
  60. else
  61. {
  62. /* parent */
  63. sendinfo ((void *) 0x2);
  64. }
  65. }
  66. END_TEST
  67. #endif /* HAVE_FORK */
  68. int
  69. main (void)
  70. {
  71. int number_failed;
  72. s = suite_create ("ForkThreadStress");
  73. tc = tcase_create ("ForkThreadStress");
  74. sr = srunner_create (s);
  75. suite_add_tcase (s, tc);
  76. #ifdef HAVE_PTHREAD
  77. tcase_add_loop_test (tc, test_stress_threads, 0, 100);
  78. #endif /* HAVE_PTHREAD */
  79. #if defined(HAVE_FORK) && HAVE_FORK==1
  80. tcase_add_loop_test (tc, test_stress_forks, 0, 100);
  81. #endif /* HAVE_FORK */
  82. srunner_run_all (sr, CK_VERBOSE);
  83. number_failed = srunner_ntests_failed (sr);
  84. srunner_free (sr);
  85. /* hack to give us XFAIL on non-posix platforms */
  86. #ifndef HAVE_FORK
  87. number_failed++;
  88. #endif /* !HAVE_FORK */
  89. return number_failed ? EXIT_FAILURE : EXIT_SUCCESS;
  90. }