Debian Codegraph @

Xpjproject/pjlib/src/pj/unittest.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2024 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <pj/unittest.h>
19 #include <pj/assert.h>
20 #include <pj/errno.h>
21 #include <pj/fifobuf.h>
22 #include <pj/os.h>
23 #include <pj/rand.h>
24 #include <pj/string.h>
25
26 #define THIS_FILE "unittest.c"
27 #define INVALID_TLS_ID -1
28
29 static long tls_id = INVALID_TLS_ID;
30
31 /* When basic runner is used, current test is saved in this global var */
32 static pj_test_case *tc_main_thread;
33
34 /* Is unit test running */
35 static pj_bool_t is_under_test;
36
37 /* Forward decls. */
38 static void unittest_log_callback(int level, const char *data, int len);
39 static int get_completion_line( const pj_test_case *tc, const char *end_line,
40 char *log_buf, unsigned buf_size);
41
42 /* atexit() callback to free TLS */
43 static void unittest_shutdown(void)
44 {
45 if (tls_id != INVALID_TLS_ID) {
46 pj_thread_local_free(tls_id);
47 tls_id = INVALID_TLS_ID;
48 }
49 }
50
51 /* initialize unittest subsystem. can be called many times. */
52 static pj_status_t unittest_init(void)
53 {
54 #if PJ_HAS_THREADS
55 if (tls_id == INVALID_TLS_ID) {
56 pj_status_t status;
57 status = pj_thread_local_alloc(&tls_id);
58 if (status != PJ_SUCCESS) {
59 tls_id = INVALID_TLS_ID;
60 return status;
61 }
62
63 pj_atexit(&unittest_shutdown);
64 }
65 #endif
66 return PJ_SUCCESS;
67 }
68
69 /* Initialize param with default values */
70 PJ_DEF(void) pj_test_case_param_default( pj_test_case_param *prm)
71 {
72 pj_bzero(prm, sizeof(*prm));
73 prm->log_level = 6;
74 }
75
76 /* Initialize test case */
77 PJ_DEF(void) pj_test_case_init( pj_test_case *tc,
78 const char *obj_name,
79 unsigned flags,
80 int (*test_func)(void*),
81 void *arg,
82 void *fifobuf_buf,
83 unsigned buf_size,
84 const pj_test_case_param *prm)
85 {
86 PJ_ASSERT_ON_FAIL( ((flags & (PJ_TEST_KEEP_FIRST|PJ_TEST_KEEP_LAST)) !=
87 (PJ_TEST_KEEP_FIRST|PJ_TEST_KEEP_LAST)), {} );
88 pj_bzero(tc, sizeof(*tc));
89
90 /* Parameters */
91 if (prm) {
92 pj_memcpy(&tc->prm, prm, sizeof(*prm));
93 } else {
94 pj_test_case_param_default(&tc->prm);
95 }
96 pj_ansi_strxcpy(tc->obj_name, obj_name, sizeof(tc->obj_name));
97 tc->flags = flags;
98 tc->test_func = test_func;
99 tc->arg = arg;
100 pj_fifobuf_init(&tc->fb, fifobuf_buf, buf_size);
101
102 /* Run-time state */
103 tc->result = PJ_EPENDING;
104 pj_list_init(&tc->logs);
105 }
106
107 /* Init test suite */
108 PJ_DEF(void) pj_test_suite_init(pj_test_suite *suite)
109 {
110 pj_bzero(suite, sizeof(*suite));
111 pj_list_init(&suite->tests);
112 }
113
114 /* Add test case */
115 PJ_DEF(void) pj_test_suite_add_case(pj_test_suite *suite, pj_test_case *tc)
116 {
117 pj_list_push_back(&suite->tests, tc);
118 }
119
120 /* Own PRNG using Linear congruential generator because rand() yields
121 * difference sequence on different machines even with the same seed.
122 */
123 static pj_uint32_t rand_int(pj_uint32_t seed)
124 {
125 #define M ((pj_uint32_t)(1<<31))
126 #define A ((pj_uint32_t)1103515245)
127 #define C ((pj_uint32_t)12345)
128
129 return (pj_uint32_t)(((A*seed) + C) % M);
130
131 #undef M
132 #undef A
133 #undef C
134 }
135
136 /* Shuffle */
137 PJ_DEF(void) pj_test_suite_shuffle(pj_test_suite *suite, int seed)
138 {
139 pj_test_case src, *tc;
140 pj_uint32_t rand;
141 unsigned total, movable;
142
143 /* although pj_rand() is not used here, still call pj_srand() to make
144 * RNG used by other parts of the program repeatable. This should be
145 * the only call to pj_srand() in the whole program.
146 */
147 pj_srand(seed);
148 rand = (pj_uint32_t)((seed >= 0) ? seed : pj_rand());
149
150 /* Move tests to new list */
151 pj_list_init(&src);
152 pj_list_merge_last(&src, &suite->tests);
153
154 /* Move KEEP_FIRST tests first */
155 for (tc=src.next; tc!=&src; ) {
156 pj_test_case *next = tc->next;
157 if (tc->flags & PJ_TEST_KEEP_FIRST) {
158 pj_list_erase(tc);
159 pj_list_push_back(&suite->tests, tc);
160 }
161
162 tc = next;
163 }
164
165 /* Count non-KEEP_LAST tests */
166 for (total=0, movable=0, tc=src.next; tc!=&src; tc=tc->next) {
167 ++total;
168 if ((tc->flags & PJ_TEST_KEEP_LAST)==0)
169 ++movable;
170 }
171
172 /* Shuffle non KEEP_LAST tests */
173 while (movable > 0) {
174 unsigned step;
175
176 rand = rand_int(rand);
177 step = rand % total;
178
179 for (tc=src.next; step>0; tc=tc->next, --step)
180 ;
181
182 pj_assert(tc!=&src);
183 if (tc->flags & PJ_TEST_KEEP_LAST)
184 continue;
185
186 pj_list_erase(tc);
187 pj_list_push_back(&suite->tests, tc);
188 --movable;
189 --total;
190 }
191
192 /* Move KEEP_LAST tests */
193 for (tc=src.next; tc!=&src; ) {
194 pj_test_case *next = tc->next;
195 pj_assert(tc->flags & PJ_TEST_KEEP_LAST);
196 pj_list_erase(tc);
197 pj_list_push_back(&suite->tests, tc);
198 tc = next;
199 }
200 }
201
202 /* Initialize text runner param with default values */
203 PJ_DEF(void) pj_test_runner_param_default(pj_test_runner_param *prm)
204 {
205 pj_bzero(prm, sizeof(*prm));
206 #if PJ_HAS_THREADS
207 prm->nthreads = 1;
208 #endif
209 }
210
211 /* Main API to start running a test runner */
212 PJ_DEF(void) pj_test_run(pj_test_runner *runner, pj_test_suite *suite)
213 {
214 pj_test_case *tc;
215
216 /* Redirect logging to our custom callback */
217 runner->orig_log_writer = pj_log_get_log_func();
218 pj_log_set_log_func(&unittest_log_callback);
219
220 /* Initialize suite and test cases */
221 runner->suite = suite;
222 runner->ntests = (unsigned)pj_list_size(&suite->tests);
223 runner->nruns = 0;
224
225 for (tc=suite->tests.next; tc!=&suite->tests;
226 tc=tc->next)
227 {
228 tc->result = PJ_EPENDING;
229 tc->runner = NULL; /* WIll be assigned runner when is run */
230 }
231
232 /* Call the run method to perform runner specific loop */
233 is_under_test = PJ_TRUE;
234 pj_get_timestamp(&suite->start_time);
235 runner->main(runner);
236 pj_get_timestamp(&suite->end_time);
237 is_under_test = PJ_FALSE;
238
239 /* Restore logging */
240 pj_log_set_log_func(runner->orig_log_writer);
241 }
242
243 /* Check if we are under test */
244 PJ_DEF(pj_bool_t) pj_test_is_under_test(void)
245 {
246 // Cannot use pj_log_get_log_func() when PJ_LOG_MAX_LEVEL is 0.
247 //return pj_log_get_log_func()==&unittest_log_callback;
248
249 return is_under_test;
250 }
251
252 /* Calculate statistics */
253 PJ_DEF(void) pj_test_get_stat( const pj_test_suite *suite, pj_test_stat *stat)
254 {
255 const pj_test_case *tc;
256
257 pj_bzero(stat, sizeof(*stat));
258 stat->duration = pj_elapsed_time(&suite->start_time, &suite->end_time);
259 stat->ntests = (unsigned)pj_list_size(&suite->tests);
260
261 for (tc=suite->tests.next; tc!=&suite->tests; tc=tc->next) {
262 if (tc->result != PJ_EPENDING) {
263 stat->nruns++;
264 if (tc->result != PJ_SUCCESS) {
265 if (stat->nfailed < PJ_ARRAY_SIZE(stat->failed_names)) {
266 stat->failed_names[stat->nfailed] = tc->obj_name;
267 }
268 stat->nfailed++;
269 }
270 }
271 }
272 }
273
274 /* Display statistics */
275 PJ_DEF(void) pj_test_display_stat(const pj_test_stat *stat,
276 const char *test_name,
277 const char *log_sender)
278 {
279 PJ_LOG(3,(log_sender, "Unit test statistics for %s:", test_name));
280 PJ_LOG(3,(log_sender, " Total number of tests: %d", stat->ntests));
281 PJ_LOG(3,(log_sender, " Number of test run: %d", stat->nruns));
282 PJ_LOG(3,(log_sender, " Number of failed test: %d", stat->nfailed));
283 PJ_LOG(3,(log_sender, " Total duration: %dm%d.%03ds",
284 (int)stat->duration.sec/60, (int)stat->duration.sec%60,
285 (int)stat->duration.msec));
286 }
287
288 /* Get name with argument info if any, e.g. "ice_test (arg: 1)" */
289 static const char *get_test_case_info(const pj_test_case *tc,
290 char *buf, unsigned size)
291 {
292 char arg_info[64];
293 if (tc->flags & PJ_TEST_FUNC_NO_ARG) {
294 arg_info[0] = '\0';
295 } else {
296 char arg_val[40];
297 /* treat argument as integer */
298 pj_ansi_snprintf(arg_val, sizeof(arg_val), "%ld",
299 (long)(intptr_t)tc->arg);
300
301 /* if arg value is too long (e.g. it's a pointer!), then just show
302 * a portion of it */
303 if (pj_ansi_strlen(arg_val) > 6) {
304 pj_ansi_strxcat(arg_val+6, "...", sizeof(arg_val));
305 }
306
307 pj_ansi_snprintf(arg_info, sizeof(arg_info), " (arg: %s)", arg_val);
308 }
309 pj_ansi_snprintf(buf, size, "%s%s", tc->obj_name, arg_info);
310 return buf;
311 }
312
313 /* Dump previously saved log messages */
314 PJ_DEF(void) pj_test_display_log_messages(const pj_test_suite *suite,
315 unsigned flags)
316 {
317 const pj_test_case *tc = suite->tests.next;
318 pj_log_func *log_writer = pj_log_get_log_func();
319 char tcname[64];
320 const char *title;
321
322 if ((flags & PJ_TEST_ALL_TESTS)==PJ_TEST_ALL_TESTS)
323 title = "all";
324 else if ((flags & PJ_TEST_ALL_TESTS)==PJ_TEST_FAILED_TESTS)
325 title = "failed";
326 else if ((flags & PJ_TEST_ALL_TESTS)==PJ_TEST_SUCCESSFUL_TESTS)
327 title = "successful";
328 else
329 title = "unknown";
330
331 while (tc != &suite->tests) {
332 const pj_test_log_item *log_item = tc->logs.next;
333
334 if ((tc->result == PJ_EPENDING) ||
335 ((flags & PJ_TEST_ALL_TESTS)==PJ_TEST_FAILED_TESTS &&
336 tc->result==0) ||
337 ((flags & PJ_TEST_ALL_TESTS)==PJ_TEST_SUCCESSFUL_TESTS &&
338 tc->result!=0))
339 {
340 /* Test doesn't meet criteria */
341 tc = tc->next;
342 continue;
343 }
344
345 if (log_item != &tc->logs) {
346 if (title && (flags & PJ_TEST_NO_HEADER_FOOTER)==0) {
347 PJ_LOG(3,(THIS_FILE,
348 "------------ Displaying %s test logs: ------------",
349 title));
350 title = NULL;
351 }
352
353 PJ_LOG(3,(THIS_FILE, "------------ Logs for %s [rc:%d]: ------------",
354 get_test_case_info(tc, tcname, sizeof(tcname)),
355 tc->result));
356
357 do {
358 log_writer(log_item->level, log_item->msg, log_item->len);
359 log_item = log_item->next;
360 } while (log_item != &tc->logs);
361 }
362 tc = tc->next;
363 }
364
365 if (!title) {
366 PJ_LOG(3,(THIS_FILE,
367 "--------------------------------------------------------"));
368 }
369 }
370
371 /* Destroy runner */
372 PJ_DEF(void) pj_test_runner_destroy(pj_test_runner *runner)
373 {
374 runner->destroy(runner);
375 }
376
377 /**************************** Common for runners ****************************/
378
379 /* Set the current test case being run by a thread. The logging callback
380 * needs this info.
381 */
382 static void set_current_test_case(pj_test_case *tc)
383 {
384 if (tls_id == INVALID_TLS_ID)
385 tc_main_thread = tc;
386 else
387 pj_thread_local_set(tls_id, tc);
388 }
389
390
391 /* Get the current test case being run by a thread. The logging callback
392 * needs this info.
393 */
394 static pj_test_case *get_current_test_case()
395 {
396 if (tls_id == INVALID_TLS_ID)
397 return tc_main_thread;
398 else
399 return (pj_test_case*) pj_thread_local_get(tls_id);
400 }
401
402 /* Logging callback */
403 static void unittest_log_callback(int level, const char *data, int len)
404 {
405 pj_test_case *tc = get_current_test_case();
406 unsigned req_size, free_size;
407 pj_bool_t truncated;
408 pj_test_log_item *log_item;
409
410 if (len < 1)
411 return;
412
413 if (tc==NULL) {
414 /* We are being called by thread that is not part of unit-test.
415 * Call the original log writer, hoping that the thread did not
416 * change the writer before this.. (note: this can only be solved
417 * by setting pj_log_set/get_log_func() to be thread specific.)
418 */
419 pj_log_write(level, data, len);
420 return;
421 }
422
423 /* Filter out unwanted log */
424 if (level > tc->prm.log_level)
425 return;
426
427 /* If the test case wants to display the original log as they are called,
428 * then write it using the original logging writer now.
429 */
430 if (tc->flags & PJ_TEST_LOG_NO_CACHE) {
431 tc->runner->orig_log_writer(level, data, len);
432 return;
433 }
434
435 /* If fifobuf is not configured on this test case, there's nothing
436 * we can do. We assume tester doesn't want logging.
437 */
438 if (pj_fifobuf_capacity(&tc->fb)==0)
439 return;
440
441 /* Required size is the message length plus sizeof(pj_test_log_item).
442 * This should be enough to save the message INCLUDING the null
443 * character (because of msg[1] in pj_test_log_item)
444 */
445 req_size = len + sizeof(pj_test_log_item);
446
447 /* Free the buffer until it's enough to save the message. */
448 while ((free_size = pj_fifobuf_available_size(&tc->fb)) < req_size &&
449 !pj_list_empty(&tc->logs))
450 {
451 pj_test_log_item *first = tc->logs.next;
452
453 /* Free the oldest */
454 pj_list_erase(first);
455 pj_fifobuf_free(&tc->fb, first);
456 }
457
458 if (free_size < sizeof(pj_test_log_item) + 10) {
459 /* Tester has set the fifobuf's size too small */
460 return;
461 }
462
463 if (free_size < req_size) {
464 /* Truncate message */
465 len = free_size - sizeof(pj_test_log_item);
466 req_size = free_size;
467 truncated = PJ_TRUE;
468 } else {
469 truncated = PJ_FALSE;
470 }
471
472 log_item = (pj_test_log_item*)pj_fifobuf_alloc(&tc->fb, req_size);
473 PJ_ASSERT_ON_FAIL(log_item, return);
474 log_item->level = level;
475 log_item->len = len;
476 pj_memcpy(log_item->msg, data, len+1);
477 if (truncated)
478 log_item->msg[len-1] = '\n';
479 pj_list_push_back(&tc->logs, log_item);
480 }
481
482 /* Create test case completion line, i.e. the one that looks like:
483 * [2/24] pool_test [OK]
484 */
485 static int get_completion_line( const pj_test_case *tc, const char *end_line,
486 char *log_buf, unsigned buf_size)
487 {
488 char tcname[64];
489 char res_buf[64];
490 pj_time_val elapsed;
491 int log_len;
492
493 elapsed = pj_elapsed_time(&tc->start_time, &tc->end_time);
494
495 if (tc->result==0) {
496 pj_ansi_snprintf(res_buf, sizeof(res_buf), "[OK] [%d.%03ds]",
497 (int)elapsed.sec, (int)elapsed.msec);
498 } else if (tc->result==PJ_EPENDING) {
499 pj_ansi_strxcpy(res_buf, "pending", sizeof(res_buf));
500 } else {
501 pj_ansi_snprintf(res_buf, sizeof(res_buf), "[Err: %d] [%d.%03ds]",
502 tc->result, (int)elapsed.sec, (int)elapsed.msec);
503 }
504
505 log_len = pj_ansi_snprintf(log_buf, buf_size, "%-32s %s%s\n",
506 get_test_case_info(tc, tcname, sizeof(tcname)),
507 res_buf, end_line);
508
509 if (log_len < 1 || log_len >= sizeof(log_buf))
510 log_len = (int)pj_ansi_strlen(log_buf);
511
512 return log_len;
513 }
514
515 /* This is the main function to run a single test case. It may
516 * be used by the basic runner, which has no threads (=no TLS),
517 * no fifobuf, no pool, or by multiple threads.
518 */
519 static void run_test_case(pj_test_runner *runner, int tid, pj_test_case *tc)
520 {
521 char tcname[64];
522
523 if (runner->prm.verbosity >= 1) {
524 const char *exclusivity = (tc->flags & PJ_TEST_EXCLUSIVE) ?
525 " (exclusive)" : "";
526 get_test_case_info(tc, tcname, sizeof(tcname));
527 PJ_LOG(3,(THIS_FILE, "Thread %d starts running %s%s",
528 tid, tcname, exclusivity));
529 }
530
531 /* Set the test case being worked on by this thread */
532 set_current_test_case(tc);
533
534 tc->runner = runner;
535 pj_get_timestamp(&tc->start_time);
536
537 /* Call the test case's function */
538 if (tc->flags & PJ_TEST_FUNC_NO_ARG) {
539 /* Function without argument */
540 typedef int (*func_t)(void);
541 func_t func = (func_t)tc->test_func;
542 tc->result = func();
543 } else {
544 tc->result = tc->test_func(tc->arg);
545 }
546
547 if (tc->result == PJ_EPENDING)
548 tc->result = -12345;
549
550 if (tc->result && runner->prm.stop_on_error)
551 runner->stopping = PJ_TRUE;
552
553 pj_get_timestamp(&tc->end_time);
554 runner->on_test_complete(runner, tc);
555
556 /* Reset the test case being worked on by this thread */
557 set_current_test_case(NULL);
558
559 if (runner->prm.verbosity >= 1) {
560 PJ_LOG(3,(THIS_FILE, "Thread %d done running %s (rc: %d)",
561 tid, tcname, tc->result));
562 }
563 }
564
565 static pj_test_case *get_first_running(pj_test_case *tests)
566 {
567 pj_test_case *tc;
568 for (tc=tests->next; tc!=tests; tc=tc->next) {
569 if (tc->runner && tc->result==PJ_EPENDING)
570 return tc;
571 }
572 return NULL;
573 }
574
575 static pj_test_case *get_next_to_run(pj_test_case *tests)
576 {
577 pj_test_case *tc;
578 for (tc=tests->next; tc!=tests; tc=tc->next) {
579 if (tc->runner==NULL) {
580 assert(tc->result==PJ_EPENDING);
581 return tc;
582 }
583 }
584 return NULL;
585 }
586
587 /******************************* Basic Runner *******************************/
588
589 /* This is the "main()" function for basic runner. It just runs the tests
590 * sequentially
591 */
592 static void basic_runner_main(pj_test_runner *runner)
593 {
594 pj_test_case *tc;
595 for (tc = runner->suite->tests.next;
596 tc != &runner->suite->tests && !runner->stopping;
597 tc = tc->next)
598 {
599 run_test_case(runner, 0, tc);
600 }
601 }
602
603 /* Basic runner's callback when a test case completes. */
604 static void basic_on_test_complete(pj_test_runner *runner, pj_test_case *tc)
605 {
606 char line[80];
607 int len;
608
609 runner->nruns++;
610
611 len = pj_ansi_snprintf( line, sizeof(line), "[%2d/%d] ",
612 runner->nruns, runner->ntests);
613 if (len < 1 || len >= sizeof(line))
614 len = (int)pj_ansi_strlen(line);
615
616 len += get_completion_line(tc, "", line+len, sizeof(line)-len);
617 tc->runner->orig_log_writer(3, line, len);
618 }
619
620 /* Destroy for basic runner */
621 static void basic_runner_destroy(pj_test_runner *runner)
622 {
623 /* Nothing to do for basic runner */
624 PJ_UNUSED_ARG(runner);
625 }
626
627 /* Initialize a basic runner. */
628 PJ_DEF(void) pj_test_init_basic_runner(pj_test_runner *runner,
629 const pj_test_runner_param *prm)
630 {
631 pj_bzero(runner, sizeof(*runner));
632 if (prm)
633 pj_memcpy(&runner->prm, prm, sizeof(*prm));
634 else
635 pj_test_runner_param_default(&runner->prm);
636 runner->main = &basic_runner_main;
637 runner->destroy = &basic_runner_destroy;
638 runner->on_test_complete = &basic_on_test_complete;
639 }
640
641 /******************************* Text Runner *******************************/
642
643 typedef struct text_runner_t
644 {
645 pj_test_runner base;
646 pj_mutex_t *mutex;
647 pj_thread_t **threads;
648 } text_runner_t;
649
650 /* This is called by thread(s) to get the next test case to run.
651 *
652 * Returns:
653 * - PJ_SUCCESS if we successfully returns next test case (tc)
654 * - PJ_EPENDING if there is tc left but we must wait for completion of
655 * exclusive tc
656 * - PJ_ENOTFOUND if there is no further tests, which in this case the
657 * thread can exit.
658 * - other error is not anticipated, and will cause thread to exit.
659 */
660 static pj_status_t text_runner_get_next_test_case(text_runner_t *runner,
661 pj_test_case **p_test_case)
662 {
663 pj_test_suite *suite;
664 pj_test_case *cur, *next;
665 pj_status_t status;
666
667 *p_test_case = NULL;
668 pj_mutex_lock(runner->mutex);
669
670 suite = runner->base.suite;
671
672 if (runner->base.stopping) {
673 pj_mutex_unlock(runner->mutex);
674 return PJ_ENOTFOUND;
675 }
676
677 cur = get_first_running(&suite->tests);
678 next = get_next_to_run(&suite->tests);
679
680 if (cur == NULL) {
681 if (next==NULL) {
682 status = PJ_ENOTFOUND;
683 } else {
684 *p_test_case = next;
685 /* Mark as running so it won't get picked up by other threads
686 * when we exit this function
687 */
688 next->runner = &runner->base;
689 status = PJ_SUCCESS;
690 }
691 } else {
692 /* Test is still running. */
693 if ((cur->flags & PJ_TEST_EXCLUSIVE)==0 && next != NULL &&
694 (next->flags & PJ_TEST_EXCLUSIVE)==0)
695 {
696 /* Allowed other test to run because test also allows
697 * parallel test
698 */
699 *p_test_case = next;
700 /* Mark as running so it won't get picked up by other threads
701 * when we exit this function
702 */
703 next->runner = &runner->base;
704 status = PJ_SUCCESS;
705 } else {
706 if (next==NULL) {
707 /* The current case is the last one. The calling thread
708 * can quit now.
709 */
710 status = PJ_ENOTFOUND;
711 } else {
712 /* Current test case or next test do not allow parallel run */
713 status = PJ_EPENDING;
714 }
715 }
716 }
717
718 pj_mutex_unlock(runner->mutex);
719 return status;
720 }
721
722 typedef struct thread_param_t
723 {
724 text_runner_t *runner;
725 unsigned tid;
726 } thread_param_t;
727
728 /* Thread loop */
729 static int text_runner_thread_proc(void *arg)
730 {
731 thread_param_t *prm = (thread_param_t*)arg;
732 text_runner_t *runner = prm->runner;
733 unsigned tid = prm->tid;
734
735 for (;;) {
736 pj_test_case *tc;
737 pj_status_t status;
738
739 status = text_runner_get_next_test_case(runner, &tc);
740 if (status==PJ_SUCCESS) {
741 run_test_case(&runner->base, tid, tc);
742 } else if (status==PJ_EPENDING) {
743 /* Yeah sleep, but the "correct" solution is probably an order of
744 * magnitute more complicated, so this is good I think.
745 */
746 pj_thread_sleep(PJ_TEST_THREAD_WAIT_MSEC);
747 } else {
748 break;
749 }
750 }
751
752 return 0;
753 }
754
755 /* This is the "main()" function for text runner. */
756 static void text_runner_main(pj_test_runner *base)
757 {
758 text_runner_t *runner = (text_runner_t*)base;
759 thread_param_t tprm ;
760 unsigned i;
761
762 tprm.runner = runner;
763 tprm.tid = 0;
764
765 for (i=0; i<base->prm.nthreads; ++i) {
766 pj_thread_resume(runner->threads[i]);
767 }
768
769 /* The main thread behaves like another worker thread */
770 text_runner_thread_proc(&tprm);
771
772 for (i=0; i<base->prm.nthreads; ++i) {
773 pj_thread_join(runner->threads[i]);
774 }
775 }
776
777 /* text runner's callback when a test case completes. */
778 static void text_runner_on_test_complete(pj_test_runner *base,
779 pj_test_case *tc)
780 {
781 text_runner_t *runner = (text_runner_t*)base;
782 pj_mutex_lock(runner->mutex);
783 basic_on_test_complete(base, tc);
784 pj_mutex_unlock(runner->mutex);
785 }
786
787 /* text runner destructor */
788 static void text_runner_destroy(pj_test_runner *base)
789 {
790 text_runner_t *runner = (text_runner_t*)base;
791 unsigned i;
792
793 for (i=0; i<base->prm.nthreads; ++i) {
794 pj_thread_destroy(runner->threads[i]);
795 }
796 if (runner->mutex)
797 pj_mutex_destroy(runner->mutex);
798
799 }
800
801 /* Create text runner */
802 PJ_DEF(pj_status_t) pj_test_create_text_runner(
803 pj_pool_t *pool,
804 const pj_test_runner_param *prm,
805 pj_test_runner **p_runner)
806 {
807 text_runner_t *runner;
808 unsigned i;
809 pj_status_t status;
810 unsigned nthreads = (PJ_HAS_THREADS? 1: 0);
811
812 *p_runner = NULL;
813
814 status = unittest_init();
815 if (status != PJ_SUCCESS)
816 return status;
817
818 runner = PJ_POOL_ZALLOC_T(pool, text_runner_t);
819 runner->base.main = text_runner_main;
820 runner->base.destroy = text_runner_destroy;
821 runner->base.on_test_complete = &text_runner_on_test_complete;
822
823 status = pj_mutex_create(pool, "unittest%p", PJ_MUTEX_RECURSE,
824 &runner->mutex);
825 if (status != PJ_SUCCESS)
826 goto on_error;
827
828 if (prm) {
829 pj_memcpy(&runner->base.prm, prm, sizeof(*prm));
830 nthreads = prm->nthreads;
831 } else {
832 pj_test_runner_param_default(&runner->base.prm);
833 }
834 runner->base.prm.nthreads = 0;
835 runner->threads = (pj_thread_t**) pj_pool_calloc(pool, nthreads,
836 sizeof(pj_thread_t*));
837 for (i=0; i<nthreads; ++i) {
838 thread_param_t *tprm = PJ_POOL_ZALLOC_T(pool, thread_param_t);
839 tprm->runner = runner;
840 tprm->tid = i+1;
841 status = pj_thread_create(pool, "unittest%p",
842 text_runner_thread_proc, tprm,
843 0, PJ_THREAD_SUSPENDED,
844 &runner->threads[i]);
845 if (status != PJ_SUCCESS)
846 goto on_error;
847 runner->base.prm.nthreads++;
848 }
849
850 *p_runner = (pj_test_runner*)runner;
851 return PJ_SUCCESS;
852
853 on_error:
854 text_runner_destroy(&runner->base);
855 return status;
856 }
857