#include <stdio.h>
#include <check.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h> /* fork() */
+#endif
+
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
TCase *tc;
SRunner *sr;
-static void *sendinfo_thread(void *userdata CK_ATTRIBUTE_UNUSED)
+static void *sendinfo(void *userdata CK_ATTRIBUTE_UNUSED)
{
unsigned int i;
- for (i=0; i < 100; i++) {
+ for (i=0; i < 999; i++) {
fail_unless(1,"Shouldn't see this message");
}
}
-static void *sendinfo_fail_thread(void *userdata CK_ATTRIBUTE_UNUSED)
-{
- unsigned int i;
- for (i=0; i < 100; i++) {
- fail("This test fails");
- }
-
- return NULL;
-}
-
-START_TEST(test_pass)
+START_TEST(test_stress_threads)
{
#ifdef HAVE_PTHREAD
pthread_t a, b;
- pthread_create(&a, NULL, sendinfo_thread, (void *) 0xa);
- pthread_create(&b, NULL, sendinfo_thread, (void *) 0xb);
+ pthread_create(&a, NULL, sendinfo, (void *) 0xa);
+ pthread_create(&b, NULL, sendinfo, (void *) 0xb);
pthread_join(a, NULL);
pthread_join(b, NULL);
#else
- sendinfo_thread((void *) 0xa);
- sendinfo_thread((void *) 0xb);
+ sendinfo((void *) 0xa);
+ sendinfo((void *) 0xb);
#endif
}
END_TEST
-START_TEST(test_fail)
+START_TEST(test_stress_forks)
{
-#ifdef HAVE_PTHREAD
- pthread_t a, b;
- pthread_create(&a, NULL, sendinfo_fail_thread, (void *) 0xa);
- pthread_create(&b, NULL, sendinfo_fail_thread, (void *) 0xb);
-
- pthread_join(a, NULL);
- pthread_join(b, NULL);
-#else
- sendinfo_fail_thread((void *) 0xa);
- sendinfo_fail_thread((void *) 0xb);
-#endif
+ pid_t cpid = fork();
+ if (cpid == 0) { /* Child */
+ sendinfo((void *) 0x1);
+ } else {
+ sendinfo((void *) 0x2);
+ }
}
END_TEST
-static void run (int num_iters)
+
+int main(void)
{
- int i;
- s = suite_create ("Stress");
- tc = tcase_create ("Stress");
+ int nf;
+ s = suite_create ("ForkThreadStress");
+ tc = tcase_create ("ForkThreadStress");
sr = srunner_create (s);
suite_add_tcase(s, tc);
- for (i = 0; i < num_iters; i++) {
- tcase_add_test (tc, test_pass);
- tcase_add_test (tc, test_fail);
- }
+ tcase_add_loop_test (tc, test_stress_threads, 0, 100);
+ tcase_add_loop_test (tc, test_stress_forks, 0, 100);
srunner_run_all(sr, CK_SILENT);
- if (srunner_ntests_failed (sr) != num_iters) {
- printf ("Error: expected %d failures, got %d\n",
- num_iters, srunner_ntests_failed(sr));
- return;
- }
+
+ nf = srunner_ntests_failed (sr);
srunner_free(sr);
-}
-
-int main(void)
-{
- int i;
- time_t t1;
- int iters[] = {1, 100, 1000, -1};
-
- for (i = 0; iters[i] != -1; i++) {
- t1 = time(NULL);
- run(iters[i]);
- printf ("%d, %d\n", iters[i], (int) difftime(time(NULL), t1));
- }
- return 0;
+ return nf ? EXIT_FAILURE : EXIT_SUCCESS;
}