runner.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #ifndef RUNNER_H_
  22. #define RUNNER_H_
  23. #include <limits.h> /* PATH_MAX */
  24. #include <stdio.h> /* FILE */
  25. /*
  26. * The maximum number of processes (main + helpers) that a test / benchmark
  27. * can have.
  28. */
  29. #define MAX_PROCESSES 8
  30. /*
  31. * Struct to store both tests and to define helper processes for tasks.
  32. */
  33. typedef struct {
  34. char *task_name;
  35. char *process_name;
  36. int (*main)(void);
  37. int is_helper;
  38. int show_output;
  39. /*
  40. * The time in milliseconds after which a single test or benchmark times out.
  41. */
  42. int timeout;
  43. } task_entry_t, bench_entry_t;
  44. /*
  45. * Macros used by test-list.h and benchmark-list.h.
  46. */
  47. #define TASK_LIST_START \
  48. task_entry_t TASKS[] = {
  49. #define TASK_LIST_END \
  50. { 0, 0, 0, 0, 0, 0 } \
  51. };
  52. #define TEST_DECLARE(name) \
  53. int run_test_##name(void);
  54. #define TEST_ENTRY(name) \
  55. { #name, #name, &run_test_##name, 0, 0, 5000 },
  56. #define TEST_ENTRY_CUSTOM(name, is_helper, show_output, timeout) \
  57. { #name, #name, &run_test_##name, is_helper, show_output, timeout },
  58. #define BENCHMARK_DECLARE(name) \
  59. int run_benchmark_##name(void);
  60. #define BENCHMARK_ENTRY(name) \
  61. { #name, #name, &run_benchmark_##name, 0, 0, 60000 },
  62. #define HELPER_DECLARE(name) \
  63. int run_helper_##name(void);
  64. #define HELPER_ENTRY(task_name, name) \
  65. { #task_name, #name, &run_helper_##name, 1, 0, 0 },
  66. #define TEST_HELPER HELPER_ENTRY
  67. #define BENCHMARK_HELPER HELPER_ENTRY
  68. extern char executable_path[4096];
  69. /*
  70. * Include platform-dependent definitions
  71. */
  72. #ifdef _WIN32
  73. # include "runner-win.h"
  74. #else
  75. # include "runner-unix.h"
  76. #endif
  77. /* The array that is filled by test-list.h or benchmark-list.h */
  78. extern task_entry_t TASKS[];
  79. /*
  80. * Run all tests.
  81. */
  82. int run_tests(int benchmark_output);
  83. /*
  84. * Run a single test. Starts up any helpers.
  85. */
  86. int run_test(const char* test,
  87. int benchmark_output,
  88. int test_count);
  89. /*
  90. * Run a test part, i.e. the test or one of its helpers.
  91. */
  92. int run_test_part(const char* test, const char* part);
  93. /*
  94. * Print tests in sorted order to `stream`. Used by `./run-tests --list`.
  95. */
  96. void print_tests(FILE* stream);
  97. /* Print lines in |buffer| as TAP diagnostics to |stream|. */
  98. void print_lines(const char* buffer, size_t size, FILE* stream);
  99. /*
  100. * Stuff that should be implemented by test-runner-<platform>.h
  101. * All functions return 0 on success, -1 on failure, unless specified
  102. * otherwise.
  103. */
  104. /* Do platform-specific initialization. */
  105. void platform_init(int argc, char** argv);
  106. /* Invoke "argv[0] test-name [test-part]". Store process info in *p. Make sure
  107. * that all stdio output of the processes is buffered up. */
  108. int process_start(char *name, char* part, process_info_t *p, int is_helper);
  109. /* Wait for all `n` processes in `vec` to terminate. Time out after `timeout`
  110. * msec, or never if timeout == -1. Return 0 if all processes are terminated,
  111. * -1 on error, -2 on timeout. */
  112. int process_wait(process_info_t *vec, int n, int timeout);
  113. /* Returns the number of bytes in the stdio output buffer for process `p`. */
  114. long int process_output_size(process_info_t *p);
  115. /* Copy the contents of the stdio output buffer to `stream`. */
  116. int process_copy_output(process_info_t* p, FILE* stream);
  117. /* Copy the last line of the stdio output buffer to `buffer` */
  118. int process_read_last_line(process_info_t *p,
  119. char * buffer,
  120. size_t buffer_len);
  121. /* Return the name that was specified when `p` was started by process_start */
  122. char* process_get_name(process_info_t *p);
  123. /* Terminate process `p`. */
  124. int process_terminate(process_info_t *p);
  125. /* Return the exit code of process p. On error, return -1. */
  126. int process_reap(process_info_t *p);
  127. /* Clean up after terminating process `p` (e.g. free the output buffer etc.). */
  128. void process_cleanup(process_info_t *p);
  129. /* Move the console cursor one line up and back to the first column. */
  130. void rewind_cursor(void);
  131. #endif /* RUNNER_H_ */