Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | |
| 19 | #include <errno.h> |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 20 | #include <fcntl.h> |
| 21 | #include <inttypes.h> |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 22 | #include <stdarg.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <time.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include <string> |
| 30 | #include <tuple> |
| 31 | #include <utility> |
| 32 | #include <vector> |
| 33 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 34 | #include "BionicDeathTest.h" // For selftest. |
| 35 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 36 | namespace testing { |
| 37 | namespace internal { |
| 38 | |
| 39 | // Reuse of testing::internal::ColoredPrintf in gtest. |
| 40 | enum GTestColor { |
| 41 | COLOR_DEFAULT, |
| 42 | COLOR_RED, |
| 43 | COLOR_GREEN, |
| 44 | COLOR_YELLOW |
| 45 | }; |
| 46 | |
| 47 | void ColoredPrintf(GTestColor color, const char* fmt, ...); |
| 48 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 49 | } // namespace internal |
| 50 | } // namespace testing |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 51 | |
| 52 | using testing::internal::GTestColor; |
| 53 | using testing::internal::COLOR_DEFAULT; |
| 54 | using testing::internal::COLOR_RED; |
| 55 | using testing::internal::COLOR_GREEN; |
| 56 | using testing::internal::COLOR_YELLOW; |
| 57 | using testing::internal::ColoredPrintf; |
| 58 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 59 | constexpr int DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS = 60000; |
| 60 | constexpr int DEFAULT_GLOBAL_TEST_RUN_WARNLINE_MS = 2000; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 61 | |
| 62 | // The time each test can run before killed for the reason of timeout. |
| 63 | // It takes effect only with --isolate option. |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 64 | static int global_test_run_deadline_ms = DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 65 | |
| 66 | // The time each test can run before be warned for too much running time. |
| 67 | // It takes effect only with --isolate option. |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 68 | static int global_test_run_warnline_ms = DEFAULT_GLOBAL_TEST_RUN_WARNLINE_MS; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 69 | |
| 70 | // Return deadline duration for a test, in ms. |
| 71 | static int GetDeadlineInfo(const std::string& /*test_name*/) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 72 | return global_test_run_deadline_ms; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Return warnline duration for a test, in ms. |
| 76 | static int GetWarnlineInfo(const std::string& /*test_name*/) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 77 | return global_test_run_warnline_ms; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 80 | static void PrintHelpInfo() { |
| 81 | printf("Bionic Unit Test Options:\n" |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 82 | " -j [JOB_COUNT] or -j[JOB_COUNT]\n" |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 83 | " Run up to JOB_COUNT tests in parallel.\n" |
| 84 | " Use isolation mode, Run each test in a separate process.\n" |
| 85 | " If JOB_COUNT is not given, it is set to the count of available processors.\n" |
| 86 | " --no-isolate\n" |
| 87 | " Don't use isolation mode, run all tests in a single process.\n" |
| 88 | " --deadline=[TIME_IN_MS]\n" |
| 89 | " Run each test in no longer than [TIME_IN_MS] time.\n" |
| 90 | " It takes effect only in isolation mode. Deafult deadline is 60000 ms.\n" |
| 91 | " --warnline=[TIME_IN_MS]\n" |
| 92 | " Test running longer than [TIME_IN_MS] will be warned.\n" |
| 93 | " It takes effect only in isolation mode. Default warnline is 2000 ms.\n" |
| 94 | "\nDefault bionic unit test option is -j.\n" |
| 95 | "\n"); |
| 96 | } |
| 97 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 98 | enum TestResult { |
| 99 | TEST_SUCCESS = 0, |
| 100 | TEST_FAILED, |
| 101 | TEST_TIMEOUT |
| 102 | }; |
| 103 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 104 | class Test { |
| 105 | public: |
| 106 | Test() {} // For std::vector<Test>. |
| 107 | explicit Test(const char* name) : name_(name) {} |
| 108 | |
| 109 | const std::string& GetName() const { return name_; } |
| 110 | |
| 111 | void SetResult(TestResult result) { result_ = result; } |
| 112 | |
| 113 | TestResult GetResult() const { return result_; } |
| 114 | |
| 115 | void SetTestTime(int64_t elapsed_time_ns) { elapsed_time_ns_ = elapsed_time_ns; } |
| 116 | |
| 117 | int64_t GetTestTime() const { return elapsed_time_ns_; } |
| 118 | |
| 119 | void AppendFailureMessage(const std::string& s) { failure_message_ += s; } |
| 120 | |
| 121 | const std::string& GetFailureMessage() const { return failure_message_; } |
| 122 | |
| 123 | private: |
| 124 | const std::string name_; |
| 125 | TestResult result_; |
| 126 | int64_t elapsed_time_ns_; |
| 127 | std::string failure_message_; |
| 128 | }; |
| 129 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 130 | class TestCase { |
| 131 | public: |
| 132 | TestCase() {} // For std::vector<TestCase>. |
| 133 | explicit TestCase(const char* name) : name_(name) {} |
| 134 | |
| 135 | const std::string& GetName() const { return name_; } |
| 136 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 137 | void AppendTest(const char* test_name) { |
| 138 | test_list_.push_back(Test(test_name)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 141 | size_t TestCount() const { return test_list_.size(); } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 142 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 143 | std::string GetTestName(size_t test_id) const { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 144 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 145 | return name_ + "." + test_list_[test_id].GetName(); |
| 146 | } |
| 147 | |
| 148 | Test& GetTest(size_t test_id) { |
| 149 | VerifyTestId(test_id); |
| 150 | return test_list_[test_id]; |
| 151 | } |
| 152 | |
| 153 | const Test& GetTest(size_t test_id) const { |
| 154 | VerifyTestId(test_id); |
| 155 | return test_list_[test_id]; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 158 | void SetTestResult(size_t test_id, TestResult result) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 159 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 160 | test_list_[test_id].SetResult(result); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 163 | TestResult GetTestResult(size_t test_id) const { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 164 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 165 | return test_list_[test_id].GetResult(); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 168 | void SetTestTime(size_t test_id, int64_t elapsed_time_ns) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 169 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 170 | test_list_[test_id].SetTestTime(elapsed_time_ns); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 173 | int64_t GetTestTime(size_t test_id) const { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 174 | VerifyTestId(test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 175 | return test_list_[test_id].GetTestTime(); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | private: |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 179 | void VerifyTestId(size_t test_id) const { |
| 180 | if(test_id >= test_list_.size()) { |
| 181 | fprintf(stderr, "test_id %zu out of range [0, %zu)\n", test_id, test_list_.size()); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 182 | exit(1); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | const std::string name_; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 188 | std::vector<Test> test_list_; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 189 | }; |
| 190 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 191 | // This is the file descriptor used by the child process to write failure message. |
| 192 | // The parent process will collect the information and dump to stdout / xml file. |
| 193 | static int child_output_fd; |
| 194 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 195 | class TestResultPrinter : public testing::EmptyTestEventListener { |
| 196 | public: |
| 197 | TestResultPrinter() : pinfo_(NULL) {} |
| 198 | virtual void OnTestStart(const testing::TestInfo& test_info) { |
| 199 | pinfo_ = &test_info; // Record test_info for use in OnTestPartResult. |
| 200 | } |
| 201 | virtual void OnTestPartResult(const testing::TestPartResult& result); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 202 | |
| 203 | private: |
| 204 | const testing::TestInfo* pinfo_; |
| 205 | }; |
| 206 | |
| 207 | // Called after an assertion failure. |
| 208 | void TestResultPrinter::OnTestPartResult(const testing::TestPartResult& result) { |
| 209 | // If the test part succeeded, we don't need to do anything. |
| 210 | if (result.type() == testing::TestPartResult::kSuccess) |
| 211 | return; |
| 212 | |
| 213 | // Print failure message from the assertion (e.g. expected this and got that). |
| 214 | char buf[1024]; |
| 215 | snprintf(buf, sizeof(buf), "%s:(%d) Failure in test %s.%s\n%s\n", result.file_name(), |
| 216 | result.line_number(), |
| 217 | pinfo_->test_case_name(), |
| 218 | pinfo_->name(), |
| 219 | result.message()); |
| 220 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 221 | int towrite = strlen(buf); |
| 222 | char* p = buf; |
| 223 | while (towrite > 0) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 224 | ssize_t write_count = TEMP_FAILURE_RETRY(write(child_output_fd, p, towrite)); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 225 | if (write_count == -1) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 226 | fprintf(stderr, "failed to write child_output_fd: %s\n", strerror(errno)); |
| 227 | exit(1); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 228 | } else { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 229 | towrite -= write_count; |
| 230 | p += write_count; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 235 | static int64_t NanoTime() { |
| 236 | struct timespec t; |
| 237 | t.tv_sec = t.tv_nsec = 0; |
| 238 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 239 | return static_cast<int64_t>(t.tv_sec) * 1000000000LL + t.tv_nsec; |
| 240 | } |
| 241 | |
| 242 | static bool EnumerateTests(int argc, char** argv, std::vector<TestCase>& testcase_list) { |
| 243 | std::string command; |
| 244 | for (int i = 0; i < argc; ++i) { |
| 245 | command += argv[i]; |
| 246 | command += " "; |
| 247 | } |
| 248 | command += "--gtest_list_tests"; |
| 249 | FILE* fp = popen(command.c_str(), "r"); |
| 250 | if (fp == NULL) { |
| 251 | perror("popen"); |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | char buf[200]; |
| 256 | while (fgets(buf, sizeof(buf), fp) != NULL) { |
| 257 | char* p = buf; |
| 258 | |
| 259 | while (*p != '\0' && isspace(*p)) { |
| 260 | ++p; |
| 261 | } |
| 262 | if (*p == '\0') continue; |
| 263 | char* start = p; |
| 264 | while (*p != '\0' && !isspace(*p)) { |
| 265 | ++p; |
| 266 | } |
| 267 | char* end = p; |
| 268 | while (*p != '\0' && isspace(*p)) { |
| 269 | ++p; |
| 270 | } |
| 271 | if (*p != '\0') { |
| 272 | // This is not we want, gtest must meet with some error when parsing the arguments. |
| 273 | fprintf(stderr, "argument error, check with --help\n"); |
| 274 | return false; |
| 275 | } |
| 276 | *end = '\0'; |
| 277 | if (*(end - 1) == '.') { |
| 278 | *(end - 1) = '\0'; |
| 279 | testcase_list.push_back(TestCase(start)); |
| 280 | } else { |
| 281 | testcase_list.back().AppendTest(start); |
| 282 | } |
| 283 | } |
| 284 | int result = pclose(fp); |
| 285 | return (result != -1 && WEXITSTATUS(result) == 0); |
| 286 | } |
| 287 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 288 | // Part of the following *Print functions are copied from external/gtest/src/gtest.cc: |
| 289 | // PrettyUnitTestResultPrinter. The reason for copy is that PrettyUnitTestResultPrinter |
| 290 | // is defined and used in gtest.cc, which is hard to reuse. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 291 | static void OnTestIterationStartPrint(const std::vector<TestCase>& testcase_list, size_t iteration, |
| 292 | size_t iteration_count) { |
| 293 | if (iteration_count > 1) { |
| 294 | printf("\nRepeating all tests (iteration %zu) . . .\n\n", iteration); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 295 | } |
| 296 | ColoredPrintf(COLOR_GREEN, "[==========] "); |
| 297 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 298 | size_t testcase_count = testcase_list.size(); |
| 299 | size_t test_count = 0; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 300 | for (const auto& testcase : testcase_list) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 301 | test_count += testcase.TestCount(); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 304 | printf("Running %zu %s from %zu %s.\n", |
| 305 | test_count, (test_count == 1) ? "test" : "tests", |
| 306 | testcase_count, (testcase_count == 1) ? "test case" : "test cases"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 307 | fflush(stdout); |
| 308 | } |
| 309 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 310 | static void OnTestEndPrint(const TestCase& testcase, size_t test_id) { |
| 311 | TestResult result = testcase.GetTestResult(test_id); |
| 312 | if (result == TEST_SUCCESS) { |
| 313 | ColoredPrintf(COLOR_GREEN, "[ OK ] "); |
| 314 | } else if (result == TEST_FAILED) { |
| 315 | ColoredPrintf(COLOR_RED, "[ FAILED ] "); |
| 316 | } else if (result == TEST_TIMEOUT) { |
| 317 | ColoredPrintf(COLOR_RED, "[ TIMEOUT ] "); |
| 318 | } |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 319 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 320 | printf("%s", testcase.GetTestName(test_id).c_str()); |
| 321 | if (testing::GTEST_FLAG(print_time)) { |
| 322 | printf(" (%" PRId64 " ms)\n", testcase.GetTestTime(test_id) / 1000000); |
| 323 | } else { |
| 324 | printf("\n"); |
| 325 | } |
| 326 | |
| 327 | const std::string& failure_message = testcase.GetTest(test_id).GetFailureMessage(); |
| 328 | printf("%s", failure_message.c_str()); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 329 | fflush(stdout); |
| 330 | } |
| 331 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 332 | static void OnTestIterationEndPrint(const std::vector<TestCase>& testcase_list, size_t /*iteration*/, |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 333 | int64_t elapsed_time_ns) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 334 | |
| 335 | std::vector<std::string> fail_test_name_list; |
| 336 | std::vector<std::pair<std::string, int64_t>> timeout_test_list; |
| 337 | |
| 338 | // For tests run exceed warnline but not timeout. |
| 339 | std::vector<std::tuple<std::string, int64_t, int>> timewarn_test_list; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 340 | size_t testcase_count = testcase_list.size(); |
| 341 | size_t test_count = 0; |
| 342 | size_t success_test_count = 0; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 343 | |
| 344 | for (const auto& testcase : testcase_list) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 345 | test_count += testcase.TestCount(); |
| 346 | for (size_t i = 0; i < testcase.TestCount(); ++i) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 347 | TestResult result = testcase.GetTestResult(i); |
| 348 | if (result == TEST_SUCCESS) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 349 | ++success_test_count; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 350 | } else if (result == TEST_FAILED) { |
| 351 | fail_test_name_list.push_back(testcase.GetTestName(i)); |
| 352 | } else if (result == TEST_TIMEOUT) { |
| 353 | timeout_test_list.push_back(std::make_pair(testcase.GetTestName(i), |
| 354 | testcase.GetTestTime(i))); |
| 355 | } |
| 356 | if (result != TEST_TIMEOUT && |
| 357 | testcase.GetTestTime(i) / 1000000 >= GetWarnlineInfo(testcase.GetTestName(i))) { |
| 358 | timewarn_test_list.push_back(std::make_tuple(testcase.GetTestName(i), |
| 359 | testcase.GetTestTime(i), |
| 360 | GetWarnlineInfo(testcase.GetTestName(i)))); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 365 | ColoredPrintf(COLOR_GREEN, "[==========] "); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 366 | printf("%zu %s from %zu %s ran.", test_count, (test_count == 1) ? "test" : "tests", |
| 367 | testcase_count, (testcase_count == 1) ? "test case" : "test cases"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 368 | if (testing::GTEST_FLAG(print_time)) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 369 | printf(" (%" PRId64 " ms total)", elapsed_time_ns / 1000000); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 370 | } |
| 371 | printf("\n"); |
| 372 | ColoredPrintf(COLOR_GREEN, "[ PASSED ] "); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 373 | printf("%zu %s.\n", success_test_count, (success_test_count == 1) ? "test" : "tests"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 374 | |
| 375 | // Print tests failed. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 376 | size_t fail_test_count = fail_test_name_list.size(); |
| 377 | if (fail_test_count > 0) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 378 | ColoredPrintf(COLOR_RED, "[ FAILED ] "); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 379 | printf("%zu %s, listed below:\n", fail_test_count, (fail_test_count == 1) ? "test" : "tests"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 380 | for (const auto& name : fail_test_name_list) { |
| 381 | ColoredPrintf(COLOR_RED, "[ FAILED ] "); |
| 382 | printf("%s\n", name.c_str()); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // Print tests run timeout. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 387 | size_t timeout_test_count = timeout_test_list.size(); |
| 388 | if (timeout_test_count > 0) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 389 | ColoredPrintf(COLOR_RED, "[ TIMEOUT ] "); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 390 | printf("%zu %s, listed below:\n", timeout_test_count, (timeout_test_count == 1) ? "test" : "tests"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 391 | for (const auto& timeout_pair : timeout_test_list) { |
| 392 | ColoredPrintf(COLOR_RED, "[ TIMEOUT ] "); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 393 | printf("%s (stopped at %" PRId64 " ms)\n", timeout_pair.first.c_str(), |
| 394 | timeout_pair.second / 1000000); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | // Print tests run exceed warnline. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 399 | size_t timewarn_test_count = timewarn_test_list.size(); |
| 400 | if (timewarn_test_count > 0) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 401 | ColoredPrintf(COLOR_YELLOW, "[ TIMEWARN ] "); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 402 | printf("%zu %s, listed below:\n", timewarn_test_count, (timewarn_test_count == 1) ? "test" : "tests"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 403 | for (const auto& timewarn_tuple : timewarn_test_list) { |
| 404 | ColoredPrintf(COLOR_YELLOW, "[ TIMEWARN ] "); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 405 | printf("%s (%" PRId64 " ms, exceed warnline %d ms)\n", std::get<0>(timewarn_tuple).c_str(), |
| 406 | std::get<1>(timewarn_tuple) / 1000000, std::get<2>(timewarn_tuple)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 410 | if (fail_test_count > 0) { |
| 411 | printf("\n%2zu FAILED %s\n", fail_test_count, (fail_test_count == 1) ? "TEST" : "TESTS"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 412 | } |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 413 | if (timeout_test_count > 0) { |
| 414 | printf("%2zu TIMEOUT %s\n", timeout_test_count, (timeout_test_count == 1) ? "TEST" : "TESTS"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 415 | } |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 416 | if (timewarn_test_count > 0) { |
| 417 | printf("%2zu TIMEWARN %s\n", timewarn_test_count, (timewarn_test_count == 1) ? "TEST" : "TESTS"); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 418 | } |
| 419 | fflush(stdout); |
| 420 | } |
| 421 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 422 | // Output xml file when --gtest_output is used, write this function as we can't reuse |
| 423 | // gtest.cc:XmlUnitTestResultPrinter. The reason is XmlUnitTestResultPrinter is totally |
| 424 | // defined in gtest.cc and not expose to outside. What's more, as we don't run gtest in |
| 425 | // the parent process, we don't have gtest classes which are needed by XmlUnitTestResultPrinter. |
| 426 | void OnTestIterationEndXmlPrint(const std::string& xml_output_filename, |
| 427 | const std::vector<TestCase>& testcase_list, |
| 428 | time_t epoch_iteration_start_time, |
| 429 | int64_t elapsed_time_ns) { |
| 430 | FILE* fp = fopen(xml_output_filename.c_str(), "w"); |
| 431 | if (fp == NULL) { |
| 432 | fprintf(stderr, "failed to open '%s': %s\n", xml_output_filename.c_str(), strerror(errno)); |
| 433 | exit(1); |
| 434 | } |
| 435 | |
| 436 | size_t total_test_count = 0; |
| 437 | size_t total_failed_count = 0; |
| 438 | std::vector<size_t> failed_count_list(testcase_list.size(), 0); |
| 439 | std::vector<int64_t> elapsed_time_list(testcase_list.size(), 0); |
| 440 | for (size_t i = 0; i < testcase_list.size(); ++i) { |
| 441 | auto& testcase = testcase_list[i]; |
| 442 | total_test_count += testcase.TestCount(); |
| 443 | for (size_t j = 0; j < testcase.TestCount(); ++j) { |
| 444 | if (testcase.GetTestResult(j) != TEST_SUCCESS) { |
| 445 | ++failed_count_list[i]; |
| 446 | } |
| 447 | elapsed_time_list[i] += testcase.GetTestTime(j); |
| 448 | } |
| 449 | total_failed_count += failed_count_list[i]; |
| 450 | } |
| 451 | |
| 452 | const tm* time_struct = localtime(&epoch_iteration_start_time); |
| 453 | char timestamp[40]; |
| 454 | snprintf(timestamp, sizeof(timestamp), "%4d-%02d-%02dT%02d:%02d:%02d", |
| 455 | time_struct->tm_year + 1900, time_struct->tm_mon + 1, time_struct->tm_mday, |
| 456 | time_struct->tm_hour, time_struct->tm_min, time_struct->tm_sec); |
| 457 | |
| 458 | fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n", fp); |
| 459 | fprintf(fp, "<testsuites tests=\"%zu\" failures=\"%zu\" disabled=\"0\" errors=\"0\"", |
| 460 | total_test_count, total_failed_count); |
| 461 | fprintf(fp, " timestamp=\"%s\" time=\"%.3lf\" name=\"AllTests\">\n", timestamp, elapsed_time_ns / 1e9); |
| 462 | for (size_t i = 0; i < testcase_list.size(); ++i) { |
| 463 | auto& testcase = testcase_list[i]; |
| 464 | fprintf(fp, " <testsuite name=\"%s\" tests=\"%zu\" failures=\"%zu\" disabled=\"0\" errors=\"0\"", |
| 465 | testcase.GetName().c_str(), testcase.TestCount(), failed_count_list[i]); |
| 466 | fprintf(fp, " time=\"%.3lf\">\n", elapsed_time_list[i] / 1e9); |
| 467 | |
| 468 | for (size_t j = 0; j < testcase.TestCount(); ++j) { |
| 469 | fprintf(fp, " <testcase name=\"%s\" status=\"run\" time=\"%.3lf\" classname=\"%s\"", |
| 470 | testcase.GetTest(j).GetName().c_str(), testcase.GetTestTime(j) / 1e9, |
| 471 | testcase.GetName().c_str()); |
| 472 | if (testcase.GetTestResult(j) == TEST_SUCCESS) { |
| 473 | fputs(" />\n", fp); |
| 474 | } else { |
| 475 | fputs(">\n", fp); |
| 476 | const std::string& failure_message = testcase.GetTest(j).GetFailureMessage(); |
| 477 | fprintf(fp, " <failure message=\"%s\" type=\"\">\n", failure_message.c_str()); |
| 478 | fputs(" </failure>\n", fp); |
| 479 | fputs(" </testcase>\n", fp); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | fputs(" </testsuite>\n", fp); |
| 484 | } |
| 485 | fputs("</testsuites>\n", fp); |
| 486 | fclose(fp); |
| 487 | } |
| 488 | |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 489 | // Forked Child process, run the single test. |
| 490 | static void ChildProcessFn(int argc, char** argv, const std::string& test_name) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 491 | char** new_argv = new char*[argc + 2]; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 492 | memcpy(new_argv, argv, sizeof(char*) * argc); |
| 493 | |
| 494 | char* filter_arg = new char [test_name.size() + 20]; |
| 495 | strcpy(filter_arg, "--gtest_filter="); |
| 496 | strcat(filter_arg, test_name.c_str()); |
| 497 | new_argv[argc] = filter_arg; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 498 | new_argv[argc + 1] = NULL; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 499 | |
| 500 | int new_argc = argc + 1; |
| 501 | testing::InitGoogleTest(&new_argc, new_argv); |
| 502 | int result = RUN_ALL_TESTS(); |
| 503 | exit(result); |
| 504 | } |
| 505 | |
| 506 | struct ChildProcInfo { |
| 507 | pid_t pid; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 508 | int64_t start_time_ns; |
| 509 | int64_t deadline_time_ns; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 510 | size_t testcase_id, test_id; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 511 | bool done_flag; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 512 | bool timeout_flag; |
| 513 | int exit_status; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 514 | int child_read_fd; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 515 | ChildProcInfo() : pid(0) {} |
| 516 | }; |
| 517 | |
| 518 | static void WaitChildProcs(std::vector<ChildProcInfo>& child_proc_list) { |
| 519 | pid_t result; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 520 | int status; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 521 | bool loop_flag = true; |
| 522 | |
| 523 | while (true) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 524 | while ((result = waitpid(-1, &status, WNOHANG)) == -1) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 525 | if (errno != EINTR) { |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if (result == -1) { |
| 531 | perror("waitpid"); |
| 532 | exit(1); |
| 533 | } else if (result == 0) { |
| 534 | // Check child timeout. |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 535 | int64_t current_time_ns = NanoTime(); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 536 | for (size_t i = 0; i < child_proc_list.size(); ++i) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 537 | if (child_proc_list[i].deadline_time_ns <= current_time_ns) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 538 | child_proc_list[i].done_flag = true; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 539 | child_proc_list[i].timeout_flag = true; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 540 | loop_flag = false; |
| 541 | } |
| 542 | } |
| 543 | } else { |
| 544 | // Check child finish. |
| 545 | for (size_t i = 0; i < child_proc_list.size(); ++i) { |
| 546 | if (child_proc_list[i].pid == result) { |
| 547 | child_proc_list[i].done_flag = true; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 548 | child_proc_list[i].timeout_flag = false; |
| 549 | child_proc_list[i].exit_status = status; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 550 | loop_flag = false; |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | if (!loop_flag) break; |
| 557 | // sleep 1 ms to avoid busy looping. |
| 558 | timespec sleep_time; |
| 559 | sleep_time.tv_sec = 0; |
| 560 | sleep_time.tv_nsec = 1000000; |
| 561 | nanosleep(&sleep_time, NULL); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | static TestResult WaitChildProc(pid_t pid) { |
| 566 | pid_t result; |
| 567 | int exit_status; |
| 568 | |
| 569 | while ((result = waitpid(pid, &exit_status, 0)) == -1) { |
| 570 | if (errno != EINTR) { |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | TestResult test_result = TEST_SUCCESS; |
| 576 | if (result != pid || WEXITSTATUS(exit_status) != 0) { |
| 577 | test_result = TEST_FAILED; |
| 578 | } |
| 579 | return test_result; |
| 580 | } |
| 581 | |
| 582 | // We choose to use multi-fork and multi-wait here instead of multi-thread, because it always |
| 583 | // makes deadlock to use fork in multi-thread. |
| 584 | static void RunTestInSeparateProc(int argc, char** argv, std::vector<TestCase>& testcase_list, |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 585 | size_t iteration_count, size_t job_count, |
| 586 | const std::string& xml_output_filename) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 587 | // Stop default result printer to avoid environment setup/teardown information for each test. |
| 588 | testing::UnitTest::GetInstance()->listeners().Release( |
| 589 | testing::UnitTest::GetInstance()->listeners().default_result_printer()); |
| 590 | testing::UnitTest::GetInstance()->listeners().Append(new TestResultPrinter); |
| 591 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 592 | for (size_t iteration = 1; iteration <= iteration_count; ++iteration) { |
| 593 | OnTestIterationStartPrint(testcase_list, iteration, iteration_count); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 594 | int64_t iteration_start_time_ns = NanoTime(); |
| 595 | time_t epoch_iteration_start_time = time(NULL); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 596 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 597 | // Run up to job_count tests in parallel, each test in a child process. |
| 598 | std::vector<ChildProcInfo> child_proc_list(job_count); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 599 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 600 | // Next test to run is [next_testcase_id:next_test_id]. |
| 601 | size_t next_testcase_id = 0; |
| 602 | size_t next_test_id = 0; |
| 603 | |
| 604 | // Record how many tests are finished. |
| 605 | std::vector<size_t> finished_test_count_list(testcase_list.size(), 0); |
| 606 | size_t finished_testcase_count = 0; |
| 607 | |
| 608 | while (finished_testcase_count < testcase_list.size()) { |
| 609 | // Fork up to job_count child processes. |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 610 | for (auto& child_proc : child_proc_list) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 611 | if (child_proc.pid == 0 && next_testcase_id < testcase_list.size()) { |
| 612 | std::string test_name = testcase_list[next_testcase_id].GetTestName(next_test_id); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 613 | int pipefd[2]; |
| 614 | int ret = pipe(pipefd); |
| 615 | if (ret == -1) { |
| 616 | perror("pipe2 in RunTestInSeparateProc"); |
| 617 | exit(1); |
| 618 | } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 619 | pid_t pid = fork(); |
| 620 | if (pid == -1) { |
| 621 | perror("fork in RunTestInSeparateProc"); |
| 622 | exit(1); |
| 623 | } else if (pid == 0) { |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 624 | close(pipefd[0]); |
| 625 | child_output_fd = pipefd[1]; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 626 | // Run child process test, never return. |
| 627 | ChildProcessFn(argc, argv, test_name); |
| 628 | } |
| 629 | // Parent process |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 630 | close(pipefd[1]); |
| 631 | child_proc.child_read_fd = pipefd[0]; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 632 | child_proc.pid = pid; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 633 | child_proc.start_time_ns = NanoTime(); |
| 634 | child_proc.deadline_time_ns = child_proc.start_time_ns + |
| 635 | GetDeadlineInfo(test_name) * 1000000LL; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 636 | child_proc.testcase_id = next_testcase_id; |
| 637 | child_proc.test_id = next_test_id; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 638 | child_proc.done_flag = false; |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 639 | if (++next_test_id == testcase_list[next_testcase_id].TestCount()) { |
| 640 | next_test_id = 0; |
| 641 | ++next_testcase_id; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // Wait for any child proc finish or timeout. |
| 647 | WaitChildProcs(child_proc_list); |
| 648 | |
| 649 | // Collect result. |
| 650 | for (auto& child_proc : child_proc_list) { |
| 651 | if (child_proc.pid != 0 && child_proc.done_flag == true) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 652 | size_t testcase_id = child_proc.testcase_id; |
| 653 | size_t test_id = child_proc.test_id; |
| 654 | TestCase& testcase = testcase_list[testcase_id]; |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 655 | testcase.SetTestTime(test_id, NanoTime() - child_proc.start_time_ns); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 656 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 657 | // Kill and wait the timeout child process before we read failure message. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 658 | if (child_proc.timeout_flag) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 659 | kill(child_proc.pid, SIGKILL); |
| 660 | WaitChildProc(child_proc.pid); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 661 | } |
| 662 | |
| 663 | while (true) { |
| 664 | char buf[1024]; |
| 665 | int ret = TEMP_FAILURE_RETRY(read(child_proc.child_read_fd, buf, sizeof(buf) - 1)); |
| 666 | if (ret > 0) { |
| 667 | buf[ret] = '\0'; |
| 668 | testcase.GetTest(test_id).AppendFailureMessage(buf); |
| 669 | } else if (ret == 0) { |
| 670 | break; // Read end. |
| 671 | } else { |
| 672 | perror("read child_read_fd in RunTestInSeparateProc"); |
| 673 | exit(1); |
| 674 | } |
| 675 | } |
| 676 | close(child_proc.child_read_fd); |
| 677 | |
| 678 | if (child_proc.timeout_flag) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 679 | testcase.SetTestResult(test_id, TEST_TIMEOUT); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 680 | char buf[1024]; |
| 681 | snprintf(buf, sizeof(buf), "%s killed because of timeout at %" PRId64 " ms.\n", |
| 682 | testcase.GetTestName(test_id).c_str(), |
| 683 | testcase.GetTestTime(test_id) / 1000000); |
| 684 | testcase.GetTest(test_id).AppendFailureMessage(buf); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 685 | |
| 686 | } else if (WIFSIGNALED(child_proc.exit_status)) { |
| 687 | // Record signal terminated test as failed. |
| 688 | testcase.SetTestResult(test_id, TEST_FAILED); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 689 | char buf[1024]; |
| 690 | snprintf(buf, sizeof(buf), "%s terminated by signal: %s.\n", |
| 691 | testcase.GetTestName(test_id).c_str(), |
| 692 | strsignal(WTERMSIG(child_proc.exit_status))); |
| 693 | testcase.GetTest(test_id).AppendFailureMessage(buf); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 694 | |
| 695 | } else { |
| 696 | testcase.SetTestResult(test_id, WEXITSTATUS(child_proc.exit_status) == 0 ? |
| 697 | TEST_SUCCESS : TEST_FAILED); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 698 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 699 | OnTestEndPrint(testcase, test_id); |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 700 | |
| 701 | if (++finished_test_count_list[testcase_id] == testcase.TestCount()) { |
| 702 | ++finished_testcase_count; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 703 | } |
| 704 | child_proc.pid = 0; |
| 705 | child_proc.done_flag = false; |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 710 | int64_t elapsed_time_ns = NanoTime() - iteration_start_time_ns; |
| 711 | OnTestIterationEndPrint(testcase_list, iteration, elapsed_time_ns); |
| 712 | if (!xml_output_filename.empty()) { |
| 713 | OnTestIterationEndXmlPrint(xml_output_filename, testcase_list, epoch_iteration_start_time, |
| 714 | elapsed_time_ns); |
| 715 | } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 719 | static size_t GetProcessorCount() { |
| 720 | return static_cast<size_t>(sysconf(_SC_NPROCESSORS_ONLN)); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 723 | struct IsolationTestOptions { |
| 724 | bool isolate; |
| 725 | size_t job_count; |
| 726 | int test_deadline_ms; |
| 727 | int test_warnline_ms; |
| 728 | std::string gtest_color; |
| 729 | bool gtest_print_time; |
| 730 | size_t gtest_repeat; |
| 731 | std::string gtest_output; |
| 732 | }; |
| 733 | |
| 734 | // Pick options not for gtest: There are two parts in args, one part is used in isolation test mode |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 735 | // as described in PrintHelpInfo(), the other part is handled by testing::InitGoogleTest() in |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 736 | // gtest. PickOptions() picks the first part into IsolationTestOptions structure, leaving the second |
| 737 | // part in args. |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 738 | // Arguments: |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 739 | // args is used to pass in all command arguments, and pass out only the part of options for gtest. |
| 740 | // options is used to pass out test options in isolation mode. |
| 741 | // Return false if there is error in arguments. |
| 742 | static bool PickOptions(std::vector<char*>& args, IsolationTestOptions& options) { |
| 743 | for (size_t i = 1; i < args.size(); ++i) { |
| 744 | if (strcmp(args[i], "--help") == 0 || strcmp(args[i], "-h") == 0) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 745 | PrintHelpInfo(); |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 746 | options.isolate = false; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 747 | return true; |
| 748 | } |
| 749 | } |
| 750 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 751 | // if --bionic-selftest argument is used, only enable self tests, otherwise remove self tests. |
| 752 | bool enable_selftest = false; |
| 753 | for (size_t i = 1; i < args.size(); ++i) { |
| 754 | if (strcmp(args[i], "--bionic-selftest") == 0) { |
| 755 | // This argument is to enable "bionic_selftest*" for self test, and is not shown in help info. |
| 756 | // Don't remove this option from arguments. |
| 757 | enable_selftest = true; |
| 758 | } |
| 759 | } |
| 760 | std::string gtest_filter_str; |
| 761 | for (size_t i = args.size() - 1; i >= 1; --i) { |
| 762 | if (strncmp(args[i], "--gtest_filter=", strlen("--gtest_filter=")) == 0) { |
| 763 | gtest_filter_str = std::string(args[i]); |
| 764 | args.erase(args.begin() + i); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 765 | break; |
| 766 | } |
| 767 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 768 | if (enable_selftest == true) { |
| 769 | args.push_back(strdup("--gtest_filter=bionic_selftest*")); |
| 770 | } else { |
| 771 | if (gtest_filter_str == "") { |
| 772 | gtest_filter_str = "--gtest_filter=-bionic_selftest*"; |
| 773 | } else { |
| 774 | gtest_filter_str += ":-bionic_selftest*"; |
| 775 | } |
| 776 | args.push_back(strdup(gtest_filter_str.c_str())); |
| 777 | } |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 778 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 779 | options.isolate = true; |
| 780 | // Parse arguments that make us can't run in isolation mode. |
| 781 | for (size_t i = 1; i < args.size(); ++i) { |
| 782 | if (strcmp(args[i], "--no-isolate") == 0) { |
| 783 | options.isolate = false; |
| 784 | } else if (strcmp(args[i], "--gtest_list_tests") == 0) { |
| 785 | options.isolate = false; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 789 | // Stop parsing if we will not run in isolation mode. |
| 790 | if (options.isolate == false) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 791 | return true; |
| 792 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 793 | |
| 794 | // Init default isolation test options. |
| 795 | options.job_count = GetProcessorCount(); |
| 796 | options.test_deadline_ms = DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS; |
| 797 | options.test_warnline_ms = DEFAULT_GLOBAL_TEST_RUN_WARNLINE_MS; |
| 798 | options.gtest_color = testing::GTEST_FLAG(color); |
| 799 | options.gtest_print_time = testing::GTEST_FLAG(print_time); |
| 800 | options.gtest_repeat = testing::GTEST_FLAG(repeat); |
| 801 | options.gtest_output = testing::GTEST_FLAG(output); |
| 802 | |
| 803 | // Parse arguments speficied for isolation mode. |
| 804 | for (size_t i = 1; i < args.size(); ++i) { |
| 805 | if (strncmp(args[i], "-j", strlen("-j")) == 0) { |
| 806 | char* p = args[i] + strlen("-j"); |
| 807 | int count = 0; |
| 808 | if (*p != '\0') { |
| 809 | // Argument like -j5. |
| 810 | count = atoi(p); |
| 811 | } else if (args.size() > i + 1) { |
| 812 | // Arguments like -j 5. |
| 813 | count = atoi(args[i + 1]); |
| 814 | ++i; |
| 815 | } |
| 816 | if (count <= 0) { |
| 817 | fprintf(stderr, "invalid job count: %d\n", count); |
| 818 | return false; |
| 819 | } |
| 820 | options.job_count = static_cast<size_t>(count); |
| 821 | } else if (strncmp(args[i], "--deadline=", strlen("--deadline=")) == 0) { |
| 822 | int time_ms = atoi(args[i] + strlen("--deadline=")); |
| 823 | if (time_ms <= 0) { |
| 824 | fprintf(stderr, "invalid deadline: %d\n", time_ms); |
| 825 | return false; |
| 826 | } |
| 827 | options.test_deadline_ms = time_ms; |
| 828 | } else if (strncmp(args[i], "--warnline=", strlen("--warnline=")) == 0) { |
| 829 | int time_ms = atoi(args[i] + strlen("--warnline=")); |
| 830 | if (time_ms <= 0) { |
| 831 | fprintf(stderr, "invalid warnline: %d\n", time_ms); |
| 832 | return false; |
| 833 | } |
| 834 | options.test_warnline_ms = time_ms; |
| 835 | } else if (strncmp(args[i], "--gtest_color=", strlen("--gtest_color=")) == 0) { |
| 836 | options.gtest_color = args[i] + strlen("--gtest_color="); |
| 837 | } else if (strcmp(args[i], "--gtest_print_time=0") == 0) { |
| 838 | options.gtest_print_time = false; |
| 839 | } else if (strncmp(args[i], "--gtest_repeat=", strlen("--gtest_repeat=")) == 0) { |
| 840 | int repeat = atoi(args[i] + strlen("--gtest_repeat=")); |
| 841 | if (repeat < 0) { |
| 842 | fprintf(stderr, "invalid gtest_repeat count: %d\n", repeat); |
| 843 | return false; |
| 844 | } |
| 845 | options.gtest_repeat = repeat; |
| 846 | // Remove --gtest_repeat=xx from arguments, so child process only run one iteration for a single test. |
| 847 | args.erase(args.begin() + i); |
| 848 | --i; |
| 849 | } else if (strncmp(args[i], "--gtest_output=", strlen("--gtest_output=")) == 0) { |
| 850 | std::string output = args[i] + strlen("--gtest_output="); |
| 851 | // generate output xml file path according to the strategy in gtest. |
| 852 | bool success = true; |
| 853 | if (strncmp(output.c_str(), "xml:", strlen("xml:")) == 0) { |
| 854 | output = output.substr(strlen("xml:")); |
| 855 | if (output.size() == 0) { |
| 856 | success = false; |
| 857 | } |
| 858 | // Make absolute path. |
| 859 | if (success && output[0] != '/') { |
| 860 | char* cwd = getcwd(NULL, 0); |
| 861 | if (cwd != NULL) { |
| 862 | output = std::string(cwd) + "/" + output; |
| 863 | free(cwd); |
| 864 | } else { |
| 865 | success = false; |
| 866 | } |
| 867 | } |
| 868 | // Add file name if output is a directory. |
| 869 | if (success && output.back() == '/') { |
| 870 | output += "test_details.xml"; |
| 871 | } |
| 872 | } |
| 873 | if (success) { |
| 874 | options.gtest_output = output; |
| 875 | } else { |
| 876 | fprintf(stderr, "invalid gtest_output file: %s\n", args[i]); |
| 877 | return false; |
| 878 | } |
| 879 | |
| 880 | // Remove --gtest_output=xxx from arguments, so child process will not write xml file. |
| 881 | args.erase(args.begin() + i); |
| 882 | --i; |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | // Add --no-isolate in args to prevent child process from running in isolation mode again. |
| 887 | // As DeathTest will try to call execve(), this argument should always be added. |
| 888 | args.insert(args.begin() + 1, strdup("--no-isolate")); |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 889 | return true; |
| 890 | } |
| 891 | |
| 892 | int main(int argc, char** argv) { |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 893 | std::vector<char*> arg_list; |
| 894 | for (int i = 0; i < argc; ++i) { |
| 895 | arg_list.push_back(argv[i]); |
| 896 | } |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 897 | |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 898 | IsolationTestOptions options; |
| 899 | if (PickOptions(arg_list, options) == false) { |
| 900 | return 1; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 901 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 902 | |
| 903 | if (options.isolate == true) { |
| 904 | // Set global variables. |
| 905 | global_test_run_deadline_ms = options.test_deadline_ms; |
| 906 | global_test_run_warnline_ms = options.test_warnline_ms; |
| 907 | testing::GTEST_FLAG(color) = options.gtest_color.c_str(); |
| 908 | testing::GTEST_FLAG(print_time) = options.gtest_print_time; |
| 909 | std::vector<TestCase> testcase_list; |
| 910 | |
| 911 | argc = static_cast<int>(arg_list.size()); |
| 912 | arg_list.push_back(NULL); |
| 913 | if (EnumerateTests(argc, arg_list.data(), testcase_list) == false) { |
| 914 | return 1; |
| 915 | } |
| 916 | RunTestInSeparateProc(argc, arg_list.data(), testcase_list, options.gtest_repeat, |
| 917 | options.job_count, options.gtest_output); |
| 918 | } else { |
| 919 | argc = static_cast<int>(arg_list.size()); |
| 920 | arg_list.push_back(NULL); |
| 921 | testing::InitGoogleTest(&argc, arg_list.data()); |
| 922 | return RUN_ALL_TESTS(); |
| 923 | } |
| 924 | return 0; |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | //################################################################################ |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 928 | // Bionic Gtest self test, run this by --bionic-selftest option. |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 929 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 930 | TEST(bionic_selftest, test_success) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 931 | ASSERT_EQ(1, 1); |
| 932 | } |
| 933 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 934 | TEST(bionic_selftest, test_fail) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 935 | ASSERT_EQ(0, 1); |
| 936 | } |
| 937 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 938 | TEST(bionic_selftest, test_time_warn) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 939 | sleep(4); |
| 940 | } |
| 941 | |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 942 | TEST(bionic_selftest, test_timeout) { |
Yabin Cui | 294d1e2 | 2014-12-07 20:43:37 -0800 | [diff] [blame] | 943 | while (1) {} |
| 944 | } |
Yabin Cui | be83736 | 2015-01-02 18:45:37 -0800 | [diff] [blame] | 945 | |
| 946 | TEST(bionic_selftest, test_signal_SEGV_terminated) { |
| 947 | char* p = reinterpret_cast<char*>(static_cast<intptr_t>(atoi("0"))); |
| 948 | *p = 3; |
| 949 | } |
Yabin Cui | 657b1f9 | 2015-01-22 19:26:12 -0800 | [diff] [blame^] | 950 | |
| 951 | class bionic_selftest_DeathTest : public BionicDeathTest {}; |
| 952 | |
| 953 | static void deathtest_helper_success() { |
| 954 | ASSERT_EQ(1, 1); |
| 955 | exit(0); |
| 956 | } |
| 957 | |
| 958 | TEST_F(bionic_selftest_DeathTest, success) { |
| 959 | ASSERT_EXIT(deathtest_helper_success(), ::testing::ExitedWithCode(0), ""); |
| 960 | } |
| 961 | |
| 962 | static void deathtest_helper_fail() { |
| 963 | ASSERT_EQ(1, 0); |
| 964 | } |
| 965 | |
| 966 | TEST_F(bionic_selftest_DeathTest, fail) { |
| 967 | ASSERT_EXIT(deathtest_helper_fail(), ::testing::ExitedWithCode(0), ""); |
| 968 | } |