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