Add unit test for thread IDs.

This commit is contained in:
Nick Mathewson 2013-09-27 23:20:22 -04:00
parent 7a63005220
commit 81354b081b

View File

@ -21,6 +21,10 @@ static char *thread1_name_ = NULL;
/** The name of thread2 for the thread test */ /** The name of thread2 for the thread test */
static char *thread2_name_ = NULL; static char *thread2_name_ = NULL;
static int thread_fns_failed = 0;
static unsigned long thread_fn_tid1, thread_fn_tid2;
static void thread_test_func_(void* _s) ATTR_NORETURN; static void thread_test_func_(void* _s) ATTR_NORETURN;
/** How many iterations have the threads in the unit test run? */ /** How many iterations have the threads in the unit test run? */
@ -42,10 +46,12 @@ thread_test_func_(void* _s)
m = thread_test_start1_; m = thread_test_start1_;
cp = &thread1_name_; cp = &thread1_name_;
count = &t1_count; count = &t1_count;
thread_fn_tid1 = tor_get_thread_id();
} else { } else {
m = thread_test_start2_; m = thread_test_start2_;
cp = &thread2_name_; cp = &thread2_name_;
count = &t2_count; count = &t2_count;
thread_fn_tid2 = tor_get_thread_id();
} }
tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id()); tor_snprintf(buf, sizeof(buf), "%lu", tor_get_thread_id());
@ -61,6 +67,8 @@ thread_test_func_(void* _s)
} }
tor_mutex_acquire(thread_test_mutex_); tor_mutex_acquire(thread_test_mutex_);
strmap_set(thread_test_strmap_, s, *cp); strmap_set(thread_test_strmap_, s, *cp);
if (in_main_thread())
++thread_fns_failed;
tor_mutex_release(thread_test_mutex_); tor_mutex_release(thread_test_mutex_);
tor_mutex_release(m); tor_mutex_release(m);
@ -80,7 +88,10 @@ test_threads_basic(void *arg)
tv.tv_sec=0; tv.tv_sec=0;
tv.tv_usec=100*1000; tv.tv_usec=100*1000;
#endif #endif
(void)arg; (void) arg;
set_main_thread();
thread_test_mutex_ = tor_mutex_new(); thread_test_mutex_ = tor_mutex_new();
thread_test_start1_ = tor_mutex_new(); thread_test_start1_ = tor_mutex_new();
thread_test_start2_ = tor_mutex_new(); thread_test_start2_ = tor_mutex_new();
@ -131,6 +142,9 @@ test_threads_basic(void *arg)
!strcmp(strmap_get(thread_test_strmap_, "thread 2"), !strcmp(strmap_get(thread_test_strmap_, "thread 2"),
strmap_get(thread_test_strmap_, "last to run"))); strmap_get(thread_test_strmap_, "last to run")));
tt_int_op(thread_fns_failed, ==, 0);
tt_int_op(thread_fn_tid1, !=, thread_fn_tid2);
done: done:
tor_free(s1); tor_free(s1);
tor_free(s2); tor_free(s2);
@ -299,4 +313,3 @@ struct testcase_t thread_tests[] = {
&passthrough_setup, (void*)"tv" }, &passthrough_setup, (void*)"tv" },
END_OF_TESTCASES END_OF_TESTCASES
}; };