Changeset 134ac5d in mainline for uspace/lib/pcut/src
- Timestamp:
- 2014-06-06T07:54:24Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8db09e4
- Parents:
- eeb23f2d
- Location:
- uspace/lib/pcut/src
- Files:
-
- 1 added
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/pcut/src/internal.h
reeb23f2d r134ac5d 27 27 */ 28 28 29 /** @file 30 * Common definitions internally used in PCUT. 31 */ 32 29 33 #ifndef PCUT_INTERNAL_H_GUARD 30 34 #define PCUT_INTERNAL_H_GUARD 31 35 32 #include <pcut/ test.h>36 #include <pcut/pcut.h> 33 37 #include <stdlib.h> 34 38 … … 69 73 pcut_item_t *pcut_get_real_next(pcut_item_t *item); 70 74 pcut_item_t *pcut_get_real(pcut_item_t *item); 71 const char* pcut_run_test(pcut_test_func_t function);72 const char* pcut_run_setup_teardown(pcut_setup_func_t function);73 75 void pcut_print_tests(pcut_item_t *first); 74 76 int pcut_is_arg_with_number(const char *arg, const char *opt, int *value); … … 78 80 int pcut_run_test_single(pcut_item_t *test); 79 81 80 extern pcut_item_t *pcut_current_test; 81 extern pcut_item_t *pcut_current_suite; 82 extern int pcut_running_test_now; 83 extern int pcut_running_setup_now; 82 int pcut_get_test_timeout(pcut_item_t *test); 84 83 84 void pcut_failed_assertion(const char *message); 85 85 void pcut_print_fail_message(const char *msg); 86 86 87 /** Reporting callbacks structure. */ 87 88 typedef struct pcut_report_ops pcut_report_ops_t; 88 /** Reporting callbacks structure. */ 89 90 /** @copydoc pcut_report_ops_t */ 89 91 struct pcut_report_ops { 90 92 /** Initialize the reporting, given all tests. */ … … 116 118 void pcut_report_done(void); 117 119 120 /* OS-dependent functions. */ 118 121 122 /** Tell whether two strings start with the same prefix. 123 * 124 * @param a First string. 125 * @param b Second string. 126 * @param len Length of common prefix. 127 * @return Whether first @p len characters of @p a are the same as in @p b. 128 */ 119 129 int pcut_str_start_equals(const char *a, const char *b, int len); 130 131 /** Get size of string in bytes. 132 * 133 * @param s String in question. 134 * @return Size of @p s in bytes. 135 */ 120 136 int pcut_str_size(const char *s); 137 138 /** Convert string to integer. 139 * 140 * @param s String with integer. 141 * @return Converted integer. 142 */ 121 143 int pcut_str_to_int(const char *s); 144 145 /** Find character in a string. 146 * 147 * @param haystack Where to look for the @p needle. 148 * @param needle Character to find. 149 * @return String starting with @p needle. 150 * @retval NULL there is no @p needle in @p haystack. 151 */ 122 152 char *pcut_str_find_char(const char *haystack, const char needle); 123 153 -
uspace/lib/pcut/src/list.c
reeb23f2d r134ac5d 35 35 #include <stdlib.h> 36 36 #include "internal.h" 37 #include <pcut/ test.h>37 #include <pcut/pcut.h> 38 38 39 39 … … 118 118 } 119 119 120 /** Hide tests that are marked to be skipped. 121 * 122 * Go through all tests and those that have PCUT_EXTRA_SKIP mark 123 * as skipped with PCUT_KIND_SKIP. 124 * 125 * @param first Head of the list. 126 */ 127 static void detect_skipped_tests(pcut_item_t *first) { 128 assert(first != NULL); 129 if (first->kind == PCUT_KIND_SKIP) { 130 first = pcut_get_real_next(first); 131 } 132 for (pcut_item_t *it = first; it != NULL; it = pcut_get_real_next(it)) { 133 if (it->kind != PCUT_KIND_TEST) { 134 continue; 135 } 136 pcut_extra_t *extras = it->test.extras; 137 while (extras->type != PCUT_EXTRA_LAST) { 138 if (extras->type == PCUT_EXTRA_SKIP) { 139 it->kind = PCUT_KIND_SKIP; 140 break; 141 } 142 extras++; 143 } 144 } 145 } 146 120 147 /** Convert the static single-linked list into a flat double-linked list. 121 148 * … … 143 170 } 144 171 172 detect_skipped_tests(next); 173 145 174 set_ids(next); 146 175 -
uspace/lib/pcut/src/os/generic.c
reeb23f2d r134ac5d 76 76 static char extra_output_buffer[OUTPUT_BUFFER_SIZE]; 77 77 78 /** Prepare for a new test. */ 78 /** Prepare for a new test. 79 * 80 * @param test Test that is about to start. 81 */ 79 82 static void before_test_start(pcut_item_t *test) { 80 83 pcut_report_test_start(test); -
uspace/lib/pcut/src/os/helenos.c
reeb23f2d r134ac5d 34 34 #include <stdlib.h> 35 35 #include <str.h> 36 #include <str_error.h> 36 37 #include <unistd.h> 37 38 #include <sys/types.h> … … 41 42 #include <task.h> 42 43 #include <fcntl.h> 44 #include <fibril_synch.h> 43 45 #include "../internal.h" 44 46 … … 68 70 } 69 71 72 void pcut_str_error(int error, char *buffer, int size) { 73 const char *str = str_error(error); 74 if (str == NULL) { 75 str = "(strerror failure)"; 76 } 77 str_cpy(buffer, size, str); 78 } 79 70 80 71 81 /* Forking-mode related functions. */ … … 89 99 static char extra_output_buffer[OUTPUT_BUFFER_SIZE]; 90 100 91 /** Prepare for a new test. */ 101 /** Prepare for a new test. 102 * 103 * @param test Test that is about to be run. 104 */ 92 105 static void before_test_start(pcut_item_t *test) { 93 106 pcut_report_test_start(test); … … 95 108 memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE); 96 109 memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE); 110 } 111 112 /** Mutex guard for forced_termination_cv. */ 113 static fibril_mutex_t forced_termination_mutex 114 = FIBRIL_MUTEX_INITIALIZER(forced_termination_mutex); 115 116 /** Condition-variable for checking whether test timed-out. */ 117 static fibril_condvar_t forced_termination_cv 118 = FIBRIL_CONDVAR_INITIALIZER(forced_termination_cv); 119 120 /** Spawned task id. */ 121 static task_id_t test_task_id; 122 123 /** Flag whether test is still running. 124 * 125 * This flag is used when checking whether test timed-out. 126 */ 127 static int test_running; 128 129 /** Main fibril for checking whether test timed-out. 130 * 131 * @param arg Test that is currently running (pcut_item_t *). 132 * @return EOK Always. 133 */ 134 static int test_timeout_handler_fibril(void *arg) { 135 pcut_item_t *test = arg; 136 int timeout_sec = pcut_get_test_timeout(test); 137 suseconds_t timeout_us = (suseconds_t) timeout_sec * 1000 * 1000; 138 139 fibril_mutex_lock(&forced_termination_mutex); 140 if (!test_running) { 141 goto leave_no_kill; 142 } 143 int rc = fibril_condvar_wait_timeout(&forced_termination_cv, 144 &forced_termination_mutex, timeout_us); 145 if (rc == ETIMEOUT) { 146 task_kill(test_task_id); 147 } 148 leave_no_kill: 149 fibril_mutex_unlock(&forced_termination_mutex); 150 return EOK; 97 151 } 98 152 … … 131 185 int status = TEST_OUTCOME_PASS; 132 186 133 task_id_t task_id; 134 int rc = task_spawnvf(&task_id, self_path, arguments, files); 187 int rc = task_spawnvf(&test_task_id, self_path, arguments, files); 135 188 if (rc != EOK) { 136 189 status = TEST_OUTCOME_ERROR; … … 138 191 } 139 192 193 test_running = 1; 194 195 fid_t killer_fibril = fibril_create(test_timeout_handler_fibril, test); 196 if (killer_fibril == 0) { 197 /* FIXME: somehow announce this problem. */ 198 task_kill(test_task_id); 199 } else { 200 fibril_add_ready(killer_fibril); 201 } 202 140 203 task_exit_t task_exit; 141 204 int task_retval; 142 rc = task_wait(t ask_id, &task_exit, &task_retval);205 rc = task_wait(test_task_id, &task_exit, &task_retval); 143 206 if (rc != EOK) { 144 207 status = TEST_OUTCOME_ERROR; … … 151 214 } 152 215 216 fibril_mutex_lock(&forced_termination_mutex); 217 test_running = 0; 218 fibril_condvar_signal(&forced_termination_cv); 219 fibril_mutex_unlock(&forced_termination_mutex); 220 153 221 read_all(tempfile, extra_output_buffer, OUTPUT_BUFFER_SIZE); 154 222 -
uspace/lib/pcut/src/os/stdc.c
reeb23f2d r134ac5d 54 54 return strchr(haystack, needle); 55 55 } 56 57 void pcut_str_error(int error, char *buffer, int size) { 58 const char *str = strerror(error); 59 if (str == NULL) { 60 str = "(strerror failure)"; 61 } 62 strncpy(buffer, str, size - 1); 63 /* Ensure correct termination. */ 64 buffer[size - 1] = 0; 65 } -
uspace/lib/pcut/src/os/unix.c
reeb23f2d r134ac5d 32 32 */ 33 33 34 /** We need _POSX_SOURCE because of kill(). */ 35 #define _POSIX_SOURCE 34 36 #include <stdlib.h> 35 37 #include <unistd.h> 36 38 #include <sys/types.h> 39 #include <signal.h> 37 40 #include <errno.h> 38 41 #include <assert.h> … … 51 54 static char extra_output_buffer[OUTPUT_BUFFER_SIZE]; 52 55 53 /** Prepare for a new test. */ 56 /** Prepare for a new test. 57 * 58 * @param test Test that is about to be run. 59 */ 54 60 static void before_test_start(pcut_item_t *test) { 55 61 pcut_report_test_start(test); … … 57 63 memset(error_message_buffer, 0, OUTPUT_BUFFER_SIZE); 58 64 memset(extra_output_buffer, 0, OUTPUT_BUFFER_SIZE); 65 } 66 67 /** PID of the forked process running the actual test. */ 68 static pid_t child_pid; 69 70 /** Signal handler that kills the child. 71 * 72 * @param sig Signal number. 73 */ 74 static void kill_child_on_alarm(int sig) { 75 PCUT_UNUSED(sig); 76 kill(child_pid, SIGKILL); 59 77 } 60 78 … … 124 142 125 143 int link_stdout[2], link_stderr[2]; 126 pid_t pid;127 144 128 145 int rc = pipe(link_stdout); … … 141 158 } 142 159 143 pid = fork();144 if ( pid == (pid_t)-1) {160 child_pid = fork(); 161 if (child_pid == (pid_t)-1) { 145 162 snprintf(error_message_buffer, OUTPUT_BUFFER_SIZE - 1, 146 163 "fork() failed: %s.", strerror(rc)); … … 149 166 } 150 167 151 if ( pid == 0) {168 if (child_pid == 0) { 152 169 /* We are the child. */ 153 170 dup2(link_stdout[1], STDOUT_FILENO); … … 164 181 close(link_stderr[1]); 165 182 183 signal(SIGALRM, kill_child_on_alarm); 184 alarm(pcut_get_test_timeout(test)); 185 166 186 size_t stderr_size = read_all(link_stderr[0], extra_output_buffer, OUTPUT_BUFFER_SIZE - 1); 167 187 read_all(link_stdout[0], extra_output_buffer, OUTPUT_BUFFER_SIZE - 1 - stderr_size); … … 169 189 int status; 170 190 wait(&status); 191 alarm(0); 171 192 172 193 rc = convert_wait_status_to_outcome(status); -
uspace/lib/pcut/src/print.c
reeb23f2d r134ac5d 32 32 */ 33 33 34 #include <pcut/ test.h>34 #include <pcut/pcut.h> 35 35 #include <stdio.h> 36 36 #include <stdlib.h> -
uspace/lib/pcut/src/report/tap.c
reeb23f2d r134ac5d 48 48 static int failed_tests_in_suite; 49 49 50 /** Initialize the tap output. */ 50 /** Initialize the TAP output. 51 * 52 * @param all_items Start of the list with all items. 53 */ 51 54 static void tap_init(pcut_item_t *all_items) { 52 55 int tests_total = pcut_count_tests(all_items); … … 56 59 } 57 60 58 /** Report that a suite was started. */ 61 /** Report that a suite was started. 62 * 63 * @param suite Suite that just started. 64 */ 59 65 static void tap_suite_start(pcut_item_t *suite) { 60 66 tests_in_suite = 0; … … 64 70 } 65 71 66 /** Report that a suite was completed. */ 72 /** Report that a suite was completed. 73 * 74 * @param suite Suite that just ended. 75 */ 67 76 static void tap_suite_done(pcut_item_t *suite) { 68 77 printf("#> Finished suite %s (failed %d of %d).\n", … … 104 113 } 105 114 106 /** Report a completed test. */ 115 /** Report a completed test. 116 * 117 * @param test Test that just finished. 118 * @param outcome Outcome of the test. 119 * @param error_message Buffer with error message. 120 * @param teardown_error_message Buffer with error message from a tear-down function. 121 * @param extra_output Extra output from the test (stdout). 122 */ 107 123 static void tap_test_done(pcut_item_t *test, int outcome, 108 124 const char *error_message, const char *teardown_error_message, -
uspace/lib/pcut/src/report/xml.c
reeb23f2d r134ac5d 48 48 static int failed_tests_in_suite; 49 49 50 /** Initialize the XML output. */ 50 /** Initialize the XML output. 51 * 52 * @param all_items Start of the list with all items. 53 */ 51 54 static void xml_init(pcut_item_t *all_items) { 52 55 printf("<?xml version=\"1.0\"?>\n"); … … 58 61 } 59 62 60 /** Report that a suite was started. */ 63 /** Report that a suite was started. 64 * 65 * @param suite Suite that just started. 66 */ 61 67 static void xml_suite_start(pcut_item_t *suite) { 62 68 tests_in_suite = 0; … … 66 72 } 67 73 68 /** Report that a suite was completed. */ 74 /** Report that a suite was completed. 75 * 76 * @param suite Suite that just ended. 77 */ 69 78 static void xml_suite_done(pcut_item_t *suite) { 70 79 printf("\t</suite><!-- %s: %d / %d -->\n", suite->suite.name, … … 111 120 } 112 121 113 /** Report a completed test. */ 122 /** Report a completed test. 123 * 124 * @param test Test that just finished. 125 * @param outcome Outcome of the test. 126 * @param error_message Buffer with error message. 127 * @param teardown_error_message Buffer with error message from a tear-down function. 128 * @param extra_output Extra output from the test (stdout). 129 */ 114 130 static void xml_test_done(pcut_item_t *test, int outcome, 115 131 const char *error_message, const char *teardown_error_message, -
uspace/lib/pcut/src/run.c
reeb23f2d r134ac5d 267 267 } 268 268 269 /** Tells time-out length for a given test. 270 * 271 * @param test Test for which the time-out is questioned. 272 * @return Timeout in seconds. 273 */ 274 int pcut_get_test_timeout(pcut_item_t *test) { 275 PCUT_UNUSED(test); 276 277 int timeout = PCUT_DEFAULT_TEST_TIMEOUT; 278 279 pcut_extra_t *extras = test->test.extras; 280 while (extras->type != PCUT_EXTRA_LAST) { 281 if (extras->type == PCUT_EXTRA_TIMEOUT) { 282 timeout = extras->timeout; 283 } 284 extras++; 285 } 286 287 return timeout; 288 }
Note:
See TracChangeset
for help on using the changeset viewer.