Home | History | Annotate | Download | only in launcher
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
      6 #define BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
      7 
      8 #include <set>
      9 #include <string>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback_forward.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/test/launcher/test_result.h"
     15 #include "base/test/launcher/test_results_tracker.h"
     16 #include "base/time/time.h"
     17 #include "base/timer/timer.h"
     18 
     19 namespace testing {
     20 class TestCase;
     21 class TestInfo;
     22 }
     23 
     24 namespace base {
     25 
     26 class CommandLine;
     27 struct LaunchOptions;
     28 class SequencedWorkerPoolOwner;
     29 class TestLauncher;
     30 
     31 // Constants for GTest command-line flags.
     32 extern const char kGTestFilterFlag[];
     33 extern const char kGTestHelpFlag[];
     34 extern const char kGTestListTestsFlag[];
     35 extern const char kGTestRepeatFlag[];
     36 extern const char kGTestRunDisabledTestsFlag[];
     37 extern const char kGTestOutputFlag[];
     38 
     39 // Interface for use with LaunchTests that abstracts away exact details
     40 // which tests and how are run.
     41 class TestLauncherDelegate {
     42  public:
     43   // Called before a test is considered for running. If it returns false,
     44   // the test is not run. If it returns true, the test will be run provided
     45   // it is part of the current shard.
     46   virtual bool ShouldRunTest(const testing::TestCase* test_case,
     47                              const testing::TestInfo* test_info) = 0;
     48 
     49   // Called to make the delegate run the specified tests. The delegate must
     50   // return the number of actual tests it's going to run (can be smaller,
     51   // equal to, or larger than size of |test_names|). It must also call
     52   // |test_launcher|'s OnTestFinished method once per every run test,
     53   // regardless of its success.
     54   virtual size_t RunTests(TestLauncher* test_launcher,
     55                           const std::vector<std::string>& test_names) = 0;
     56 
     57   // Called to make the delegate retry the specified tests. The delegate must
     58   // return the number of actual tests it's going to retry (can be smaller,
     59   // equal to, or larger than size of |test_names|). It must also call
     60   // |test_launcher|'s OnTestFinished method once per every retried test,
     61   // regardless of its success.
     62   virtual size_t RetryTests(TestLauncher* test_launcher,
     63                             const std::vector<std::string>& test_names) = 0;
     64 
     65  protected:
     66   virtual ~TestLauncherDelegate();
     67 };
     68 
     69 // Launches tests using a TestLauncherDelegate.
     70 class TestLauncher {
     71  public:
     72   // Constructor. |parallel_jobs| is the limit of simultaneous parallel test
     73   // jobs.
     74   TestLauncher(TestLauncherDelegate* launcher_delegate, size_t parallel_jobs);
     75   ~TestLauncher();
     76 
     77   // Runs the launcher. Must be called at most once.
     78   bool Run() WARN_UNUSED_RESULT;
     79 
     80   // Callback called after a child process finishes. First argument is the exit
     81   // code, second one is child process elapsed time, third one is true if
     82   // the child process was terminated because of a timeout, and fourth one
     83   // contains output of the child (stdout and stderr together).
     84   typedef Callback<void(int, const TimeDelta&, bool, const std::string&)>
     85       LaunchChildGTestProcessCallback;
     86 
     87   // Launches a child process (assumed to be gtest-based binary) using
     88   // |command_line|. If |wrapper| is not empty, it is prepended to the final
     89   // command line. If the child process is still running after |timeout|, it
     90   // is terminated. |use_job_objects| determines whether job objects are used
     91   // on Windows (if unsure pass true). After the child process finishes
     92   // |callback| is called on the same thread this method was called.
     93   void LaunchChildGTestProcess(const CommandLine& command_line,
     94                                const std::string& wrapper,
     95                                base::TimeDelta timeout,
     96                                bool use_job_objects,
     97                                const LaunchChildGTestProcessCallback& callback);
     98 
     99   // Called when a test has finished running.
    100   void OnTestFinished(const TestResult& result);
    101 
    102  private:
    103   bool Init() WARN_UNUSED_RESULT;
    104 
    105   // Runs all tests in current iteration. Uses callbacks to communicate success.
    106   void RunTests();
    107 
    108   void RunTestIteration();
    109 
    110   // Saves test results summary as JSON if requested from command line.
    111   void MaybeSaveSummaryAsJSON();
    112 
    113   // Called on a worker thread after a child process finishes.
    114   void OnLaunchTestProcessFinished(
    115       const LaunchChildGTestProcessCallback& callback,
    116       int exit_code,
    117       const TimeDelta& elapsed_time,
    118       bool was_timeout,
    119       const std::string& output);
    120 
    121   // Called when a test iteration is finished.
    122   void OnTestIterationFinished();
    123 
    124   // Called by the delay timer when no output was made for a while.
    125   void OnOutputTimeout();
    126 
    127   // Make sure we don't accidentally call the wrong methods e.g. on the worker
    128   // pool thread. With lots of callbacks used this is non-trivial.
    129   // Should be the first member so that it's destroyed last: when destroying
    130   // other members, especially the worker pool, we may check the code is running
    131   // on the correct thread.
    132   ThreadChecker thread_checker_;
    133 
    134   TestLauncherDelegate* launcher_delegate_;
    135 
    136   // Support for outer sharding, just like gtest does.
    137   int32 total_shards_;  // Total number of outer shards, at least one.
    138   int32 shard_index_;   // Index of shard the launcher is to run.
    139 
    140   int cycles_;  // Number of remaining test itreations, or -1 for infinite.
    141 
    142   // Test filters (empty means no filter).
    143   std::vector<std::string> positive_test_filter_;
    144   std::vector<std::string> negative_test_filter_;
    145 
    146   // Number of tests started in this iteration.
    147   size_t test_started_count_;
    148 
    149   // Number of tests finished in this iteration.
    150   size_t test_finished_count_;
    151 
    152   // Number of tests successfully finished in this iteration.
    153   size_t test_success_count_;
    154 
    155   // Number of tests either timing out or having an unknown result,
    156   // likely indicating a more systemic problem if widespread.
    157   size_t test_broken_count_;
    158 
    159   // Number of retries in this iteration.
    160   size_t retry_count_;
    161 
    162   // Maximum number of retries per iteration.
    163   size_t retry_limit_;
    164 
    165   // Tests to retry in this iteration.
    166   std::set<std::string> tests_to_retry_;
    167 
    168   // Result to be returned from Run.
    169   bool run_result_;
    170 
    171   TestResultsTracker results_tracker_;
    172 
    173   // Watchdog timer to make sure we do not go without output for too long.
    174   DelayTimer<TestLauncher> watchdog_timer_;
    175 
    176   // Number of jobs to run in parallel.
    177   size_t parallel_jobs_;
    178 
    179   // Worker pool used to launch processes in parallel.
    180   scoped_ptr<SequencedWorkerPoolOwner> worker_pool_owner_;
    181 
    182   DISALLOW_COPY_AND_ASSIGN(TestLauncher);
    183 };
    184 
    185 // Extract part from |full_output| that applies to |result|.
    186 std::string GetTestOutputSnippet(const TestResult& result,
    187                                  const std::string& full_output);
    188 
    189 // Returns command line command line after gtest-specific processing
    190 // and applying |wrapper|.
    191 CommandLine PrepareCommandLineForGTest(const CommandLine& command_line,
    192                                        const std::string& wrapper);
    193 
    194 }  // namespace base
    195 
    196 #endif  // BASE_TEST_LAUNCHER_TEST_LAUNCHER_H_
    197