Blame tests/check_thread_stress.c

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include "../lib/libcompat.h"
Packit 0b5880
Packit 0b5880
#include <stdlib.h>
Packit 0b5880
#include <stdio.h>
Packit 0b5880
#include <check.h>
Packit 0b5880
Packit 0b5880
Suite *s;
Packit 0b5880
TCase *tc;
Packit 0b5880
SRunner *sr;
Packit 0b5880
Packit 0b5880
#if defined (HAVE_PTHREAD) || defined (HAVE_FORK)
Packit 0b5880
static void *
Packit 0b5880
sendinfo (void *userdata CK_ATTRIBUTE_UNUSED)
Packit 0b5880
{
Packit 0b5880
  unsigned int i;
Packit 0b5880
  for (i = 0; i < 999; i++)
Packit 0b5880
    {
Packit 0b5880
      ck_assert_msg (1, "Shouldn't see this message");
Packit 0b5880
    }
Packit 0b5880
  return NULL;
Packit 0b5880
}
Packit 0b5880
#endif /* HAVE_PTHREAD || HAVE_FORK */
Packit 0b5880
Packit 0b5880
#ifdef HAVE_PTHREAD
Packit 0b5880
START_TEST (test_stress_threads)
Packit 0b5880
{
Packit 0b5880
  pthread_t a, b;
Packit 0b5880
  pthread_create (&a, NULL, sendinfo, (void *) 0xa);
Packit 0b5880
  pthread_create (&b, NULL, sendinfo, (void *) 0xb);
Packit 0b5880
Packit 0b5880
  pthread_join (a, NULL);
Packit 0b5880
  pthread_join (b, NULL);
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
#endif /* HAVE_PTHREAD */
Packit 0b5880
Packit 0b5880
#if defined(HAVE_FORK) && HAVE_FORK==1
Packit 0b5880
START_TEST (test_stress_forks)
Packit 0b5880
{
Packit 0b5880
  pid_t cpid = fork ();
Packit 0b5880
  if (cpid == 0)
Packit 0b5880
    {
Packit 0b5880
      /* child */
Packit 0b5880
      sendinfo ((void *) 0x1);
Packit 0b5880
      exit (EXIT_SUCCESS);
Packit 0b5880
    }
Packit 0b5880
  else
Packit 0b5880
    {
Packit 0b5880
      /* parent */
Packit 0b5880
      sendinfo ((void *) 0x2);
Packit 0b5880
    }
Packit 0b5880
}
Packit 0b5880
END_TEST
Packit 0b5880
#endif /* HAVE_FORK */
Packit 0b5880
Packit 0b5880
int
Packit 0b5880
main (void)
Packit 0b5880
{
Packit 0b5880
  int nf;
Packit 0b5880
  s = suite_create ("ForkThreadStress");
Packit 0b5880
  tc = tcase_create ("ForkThreadStress");
Packit 0b5880
  sr = srunner_create (s);
Packit 0b5880
  suite_add_tcase (s, tc);
Packit 0b5880
Packit 0b5880
#ifdef HAVE_PTHREAD
Packit 0b5880
  tcase_add_loop_test (tc, test_stress_threads, 0, 100);
Packit 0b5880
#endif /* HAVE_PTHREAD */
Packit 0b5880
Packit 0b5880
#if defined(HAVE_FORK) && HAVE_FORK==1
Packit 0b5880
  tcase_add_loop_test (tc, test_stress_forks, 0, 100);
Packit 0b5880
#endif /* HAVE_FORK */
Packit 0b5880
Packit 0b5880
  srunner_run_all (sr, CK_VERBOSE);
Packit 0b5880
  nf = srunner_ntests_failed (sr);
Packit 0b5880
  srunner_free (sr);
Packit 0b5880
Packit 0b5880
  /* hack to give us XFAIL on non-posix platforms */
Packit 0b5880
#ifndef HAVE_FORK
Packit 0b5880
  nf++;
Packit 0b5880
#endif /* !HAVE_FORK */
Packit 0b5880
Packit 0b5880
  return nf ? EXIT_FAILURE : EXIT_SUCCESS;
Packit 0b5880
}