runner.h 5.0 KB

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