xref: /libuv/test/runner.h (revision 0ed9692a)
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to
5  * deal in the Software without restriction, including without limitation the
6  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7  * sell copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19  * IN THE SOFTWARE.
20  */
21 
22 #ifndef RUNNER_H_
23 #define RUNNER_H_
24 
25 #include <limits.h> /* PATH_MAX */
26 #include <stdio.h> /* FILE */
27 
28 
29 /*
30  * The maximum number of processes (main + helpers) that a test / benchmark
31  * can have.
32  */
33 #define MAX_PROCESSES 8
34 
35 
36 /*
37  * Struct to store both tests and to define helper processes for tasks.
38  */
39 typedef struct {
40   char *task_name;
41   char *process_name;
42   int (*main)(void);
43   int is_helper;
44   int show_output;
45 
46   /*
47    * The time in milliseconds after which a single test or benchmark times out.
48    */
49   int timeout;
50 } task_entry_t, bench_entry_t;
51 
52 
53 /*
54  * Macros used by test-list.h and benchmark-list.h.
55  */
56 #define TASK_LIST_START                             \
57   task_entry_t TASKS[] = {
58 
59 #define TASK_LIST_END                               \
60     { 0, 0, 0, 0, 0, 0 }                               \
61   };
62 
63 #define TEST_DECLARE(name)                          \
64   int run_test_##name(void);
65 
66 #define TEST_ENTRY(name)                            \
67     { #name, #name, &run_test_##name, 0, 0, 5000 },
68 
69 #define TEST_ENTRY_CUSTOM(name, is_helper, show_output, timeout) \
70     { #name, #name, &run_test_##name, is_helper, show_output, timeout },
71 
72 #define BENCHMARK_DECLARE(name)                     \
73   int run_benchmark_##name(void);
74 
75 #define BENCHMARK_ENTRY(name)                       \
76     { #name, #name, &run_benchmark_##name, 0, 0, 60000 },
77 
78 #define HELPER_DECLARE(name)                        \
79   int run_helper_##name(void);
80 
81 #define HELPER_ENTRY(task_name, name)               \
82     { #task_name, #name, &run_helper_##name, 1, 0, 0 },
83 
84 #define TEST_HELPER       HELPER_ENTRY
85 #define BENCHMARK_HELPER  HELPER_ENTRY
86 
87 extern char executable_path[4096];
88 
89 /*
90  * Include platform-dependent definitions
91  */
92 #ifdef _WIN32
93 # include "runner-win.h"
94 #else
95 # include "runner-unix.h"
96 #endif
97 
98 
99 /* The array that is filled by test-list.h or benchmark-list.h */
100 extern task_entry_t TASKS[];
101 
102 /*
103  * Run all tests.
104  */
105 int run_tests(int benchmark_output);
106 
107 /*
108  * Run a single test. Starts up any helpers.
109  */
110 int run_test(const char* test,
111              int benchmark_output,
112              int test_count);
113 
114 /*
115  * Run a test part, i.e. the test or one of its helpers.
116  */
117 int run_test_part(const char* test, const char* part);
118 
119 
120 /*
121  * Print tests in sorted order to `stream`. Used by `./run-tests --list`.
122  */
123 void print_tests(FILE* stream);
124 
125 /* Print lines in |buffer| as TAP diagnostics to |stream|. */
126 void print_lines(const char* buffer, size_t size, FILE* stream);
127 
128 /*
129  * Stuff that should be implemented by test-runner-<platform>.h
130  * All functions return 0 on success, -1 on failure, unless specified
131  * otherwise.
132  */
133 
134 /* Do platform-specific initialization. */
135 void platform_init(int argc, char** argv);
136 
137 /* Invoke "argv[0] test-name [test-part]". Store process info in *p. Make sure
138  * that all stdio output of the processes is buffered up. */
139 int process_start(char *name, char* part, process_info_t *p, int is_helper);
140 
141 /* Wait for all `n` processes in `vec` to terminate. Time out after `timeout`
142  * msec, or never if timeout == -1. Return 0 if all processes are terminated,
143  * -1 on error, -2 on timeout. */
144 int process_wait(process_info_t *vec, int n, int timeout);
145 
146 /* Returns the number of bytes in the stdio output buffer for process `p`. */
147 long int process_output_size(process_info_t *p);
148 
149 /* Copy the contents of the stdio output buffer to `stream`. */
150 int process_copy_output(process_info_t* p, FILE* stream);
151 
152 /* Copy the last line of the stdio output buffer to `buffer` */
153 int process_read_last_line(process_info_t *p,
154                            char * buffer,
155                            size_t buffer_len);
156 
157 /* Return the name that was specified when `p` was started by process_start */
158 char* process_get_name(process_info_t *p);
159 
160 /* Terminate process `p`. */
161 int process_terminate(process_info_t *p);
162 
163 /* Return the exit code of process p. On error, return -1. */
164 int process_reap(process_info_t *p);
165 
166 /* Clean up after terminating process `p` (e.g. free the output buffer etc.). */
167 void process_cleanup(process_info_t *p);
168 
169 /* Move the console cursor one line up and back to the first column. */
170 void rewind_cursor(void);
171 
172 #endif /* RUNNER_H_ */
173