/Users/deen/code/yugabyte-db/src/yb/util/test_main.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <signal.h> // For sigaction |
34 | | #include <sys/time.h> |
35 | | |
36 | | #include <gflags/gflags.h> |
37 | | #include <glog/logging.h> |
38 | | #include <gtest/gtest.h> |
39 | | |
40 | | #include "yb/util/pstack_watcher.h" |
41 | | #include "yb/util/flags.h" |
42 | | #include "yb/util/status.h" |
43 | | #include "yb/util/status_log.h" |
44 | | #include "yb/util/debug-util.h" |
45 | | |
46 | | using testing::EmptyTestEventListener; |
47 | | using testing::TestPartResult; |
48 | | using testing::UnitTest; |
49 | | |
50 | | using yb::GetStackTrace; |
51 | | using yb::StackTraceLineFormat; |
52 | | |
53 | | using std::string; |
54 | | |
55 | | DEFINE_int32(test_timeout_after, 0, |
56 | | "Maximum total seconds allowed for all unit tests in the suite. Default: disabled"); |
57 | | |
58 | | // Start timer that kills the process if --test_timeout_after is exceeded before |
59 | | // the tests complete. |
60 | | static void CreateAndStartTimer(); |
61 | | |
62 | | // Gracefully kill the process. |
63 | | static void KillTestOnTimeout(int signum); |
64 | | |
65 | | class MinimalistPrinter : public EmptyTestEventListener{ |
66 | | // Called after a failed assertion or a SUCCEED() invocation. |
67 | 7.25M | virtual void OnTestPartResult(const TestPartResult& test_part_result) { |
68 | 7.25M | if (test_part_result.failed()) { |
69 | 306 | string stack_trace = GetStackTrace(StackTraceLineFormat::CLION_CLICKABLE, 4); |
70 | | |
71 | | // Remove the common part of all Google Test tests from the stack trace. |
72 | 306 | const char* kDontShowLinesStartingWith = |
73 | 306 | "void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(" |
74 | 306 | "testing::Test*, void (testing::Test::*)(), char const*)"; |
75 | 306 | size_t cutoff_pos = stack_trace.find(kDontShowLinesStartingWith, 0); |
76 | 306 | if (cutoff_pos != string::npos) { |
77 | 0 | cutoff_pos = stack_trace.find_last_of('\n', cutoff_pos); |
78 | 0 | if (cutoff_pos != string::npos) { |
79 | 0 | stack_trace = stack_trace.substr(0, cutoff_pos); |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | 306 | std::cerr << "Test failure stack trace:\n" << stack_trace << std::endl; |
84 | 306 | } |
85 | 7.25M | } |
86 | | }; |
87 | | |
88 | 18.6k | int main(int argc, char **argv) { |
89 | 18.6k | google::InstallFailureSignalHandler(); |
90 | | // InitGoogleTest() must precede ParseCommandLineFlags(), as the former |
91 | | // removes gtest-related flags from argv that would trip up the latter. |
92 | 18.6k | ::testing::InitGoogleTest(&argc, argv); |
93 | | |
94 | 18.6k | yb::ParseCommandLineFlags(&argc, &argv, /* remove_flags */ true); |
95 | | |
96 | | // Create the test-timeout timer. |
97 | 18.6k | CreateAndStartTimer(); |
98 | | |
99 | | // Gets hold of the event listener list. |
100 | 18.6k | auto& listeners = UnitTest::GetInstance()->listeners(); |
101 | | |
102 | | // Adds a listener to the end. Google Test takes the ownership. |
103 | 18.6k | listeners.Append(new MinimalistPrinter()); |
104 | | |
105 | 18.6k | int ret = RUN_ALL_TESTS(); |
106 | | |
107 | 18.6k | return ret; |
108 | 18.6k | } |
109 | | |
110 | 2.68k | static void CreateAndStartTimer() { |
111 | 2.68k | struct sigaction action; |
112 | 2.68k | struct itimerval timer; |
113 | | |
114 | | // Create the test-timeout timer. |
115 | 2.68k | memset(&action, 0, sizeof(action)); |
116 | 2.68k | action.sa_handler = &KillTestOnTimeout; |
117 | 2.68k | CHECK_ERR(sigaction(SIGALRM, &action, nullptr)) << "Unable to set timeout action"0 ; |
118 | | |
119 | 2.68k | timer.it_interval.tv_sec = 0; // No repeat. |
120 | 2.68k | timer.it_interval.tv_usec = 0; |
121 | 2.68k | timer.it_value.tv_sec = FLAGS_test_timeout_after; // Fire in timeout seconds. |
122 | 2.68k | timer.it_value.tv_usec = 0; |
123 | | |
124 | 2.68k | CHECK_ERR(setitimer(ITIMER_REAL, &timer, nullptr)) << "Unable to set timeout timer"0 ; |
125 | 2.68k | } |
126 | | |
127 | 0 | static void KillTestOnTimeout(int signum) { |
128 | | // Dump a pstack to stdout. |
129 | 0 | WARN_NOT_OK(yb::PstackWatcher::DumpStacks(), "Unable to print pstack"); |
130 | | |
131 | | // ...and abort. |
132 | 0 | LOG(FATAL) << "Maximum unit test time exceeded (" << FLAGS_test_timeout_after << " sec)"; |
133 | 0 | } |