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 */
39typedef 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#ifdef PATH_MAX
88extern char executable_path[PATH_MAX];
89#else
90extern char executable_path[4096];
91#endif
92
93/*
94 * Include platform-dependent definitions
95 */
96#ifdef _WIN32
97# include "runner-win.h"
98#else
99# include "runner-unix.h"
100#endif
101
102
103/* The array that is filled by test-list.h or benchmark-list.h */
104extern task_entry_t TASKS[];
105
106/*
107 * Run all tests.
108 */
109int run_tests(int benchmark_output);
110
111/*
112 * Run a single test. Starts up any helpers.
113 */
114int run_test(const char* test,
115 int benchmark_output,
116 int test_count);
117
118/*
119 * Run a test part, i.e. the test or one of its helpers.
120 */
121int run_test_part(const char* test, const char* part);
122
123
124/*
125 * Print tests in sorted order to `stream`. Used by `./run-tests --list`.
126 */
127void print_tests(FILE* stream);
128
129/* Print lines in |buffer| as TAP diagnostics to |stream|. */
130void print_lines(const char* buffer, size_t size, FILE* stream);
131
132/*
133 * Stuff that should be implemented by test-runner-<platform>.h
134 * All functions return 0 on success, -1 on failure, unless specified
135 * otherwise.
136 */
137
138/* Do platform-specific initialization. */
139int platform_init(int argc, char** argv);
140
141/* Invoke "argv[0] test-name [test-part]". Store process info in *p. Make sure
142 * that all stdio output of the processes is buffered up. */
143int process_start(char *name, char* part, process_info_t *p, int is_helper);
144
145/* Wait for all `n` processes in `vec` to terminate. Time out after `timeout`
146 * msec, or never if timeout == -1. Return 0 if all processes are terminated,
147 * -1 on error, -2 on timeout. */
148int process_wait(process_info_t *vec, int n, int timeout);
149
150/* Returns the number of bytes in the stdio output buffer for process `p`. */
151long int process_output_size(process_info_t *p);
152
153/* Copy the contents of the stdio output buffer to `stream`. */
154int process_copy_output(process_info_t* p, FILE* stream);
155
156/* Copy the last line of the stdio output buffer to `buffer` */
157int process_read_last_line(process_info_t *p,
158 char * buffer,
159 size_t buffer_len);
160
161/* Return the name that was specified when `p` was started by process_start */
162char* process_get_name(process_info_t *p);
163
164/* Terminate process `p`. */
165int process_terminate(process_info_t *p);
166
167/* Return the exit code of process p. On error, return -1. */
168int process_reap(process_info_t *p);
169
170/* Clean up after terminating process `p` (e.g. free the output buffer etc.). */
171void process_cleanup(process_info_t *p);
172
173/* Move the console cursor one line up and back to the first column. */
174void rewind_cursor(void);
175
176#endif /* RUNNER_H_ */
177