1 /*
2  * Copyright (C) 2017 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 #pragma once
18 
19 #include <poll.h>
20 #include <stdint.h>
21 #include <sys/types.h>
22 
23 #include <map>
24 #include <memory>
25 #include <stack>
26 #include <string>
27 #include <tuple>
28 #include <unordered_map>
29 #include <vector>
30 
31 #include "Color.h"
32 #include "Options.h"
33 #include "Test.h"
34 
35 namespace android {
36 namespace gtest_extras {
37 
38 class Isolate {
39  public:
Isolate(const Options & options,const std::vector<char * > & child_args)40   Isolate(const Options& options, const std::vector<char*>& child_args)
41       : options_(options), child_args_(child_args) {}
42 
43   void EnumerateTests();
44 
45   int Run();
46 
47  private:
48   struct ResultsType {
49     const char* color;
50     const char* prefix;
51     const char* list_desc;
52     const char* title;
53     bool (*match_func)(const Test&);
54     void (*print_func)(const Options&, const Test&);
55   };
56 
57   size_t CheckTestsFinished();
58 
59   void CheckTestsTimeout();
60 
61   int ChildProcessFn(const std::tuple<std::string, std::string>& test);
62 
63   void HandleSignals();
64 
65   void LaunchTests();
66 
67   void ReadTestsOutput();
68 
69   void RunAllTests();
70 
71   void PrintFooter(uint64_t elapsed_time_ns);
72 
73   void PrintResults(size_t total, const ResultsType& results, std::string* footer);
74 
75   void WriteXmlResults(uint64_t elapsed_time_ns, time_t start_time);
76 
GetTestName(const std::tuple<std::string,std::string> & test)77   static std::string GetTestName(const std::tuple<std::string, std::string>& test) {
78     return std::get<0>(test) + std::get<1>(test);
79   }
80 
81   const Options& options_;
82   const std::vector<char*>& child_args_;
83 
84   size_t total_suites_ = 0;
85   size_t total_tests_ = 0;
86   size_t total_disable_tests_ = 0;
87   size_t total_pass_tests_;
88   size_t total_xpass_tests_;
89   size_t total_fail_tests_;
90   size_t total_xfail_tests_;
91   size_t total_timeout_tests_;
92   size_t total_slow_tests_;
93   size_t total_skipped_tests_;
94   size_t cur_test_index_ = 0;
95 
96   uint64_t slow_threshold_ns_;
97   uint64_t deadline_threshold_ns_;
98   std::vector<std::tuple<std::string, std::string>> tests_;
99 
100   std::vector<Test*> running_;
101   std::vector<pollfd> running_pollfds_;
102   std::vector<size_t> running_indices_;
103   std::unordered_map<pid_t, std::unique_ptr<Test>> running_by_pid_;
104   std::map<size_t, Test*> running_by_test_index_;
105 
106   std::map<size_t, std::unique_ptr<Test>> finished_;
107 
108   static constexpr useconds_t MIN_USECONDS_WAIT = 1000;
109 
110   static ResultsType SlowResults;
111   static ResultsType XpassFailResults;
112   static ResultsType FailResults;
113   static ResultsType TimeoutResults;
114   static ResultsType SkippedResults;
115 };
116 
117 }  // namespace gtest_extras
118 }  // namespace android
119