Split task_runnner_thread from end_to_end_integrationtest.

This will be reused in the end to end fuzzer.

Bug: 69150303
Change-Id: Id32868b5a456cfb956d680c5913f1fbc48c3c3a5
diff --git a/Android.bp b/Android.bp
index 908e647..994f051 100644
--- a/Android.bp
+++ b/Android.bp
@@ -219,6 +219,7 @@
     "test/end_to_end_integrationtest.cc",
     "test/fake_consumer.cc",
     "test/fake_producer.cc",
+    "test/task_runner_thread.cc",
   ],
   shared_libs: [
     "libandroid",
diff --git a/src/base/BUILD.gn b/src/base/BUILD.gn
index a1b6f7b..b9f9165 100644
--- a/src/base/BUILD.gn
+++ b/src/base/BUILD.gn
@@ -68,19 +68,32 @@
 
 source_set("test_support") {
   testonly = true
+  public_deps = [
+    ":test_task_runner",
+  ]
   deps = [
     ":base",
     "../../gn:default_deps",
     "../../gn:gtest_deps",
   ]
   sources = [
-    "test/test_task_runner.cc",
-    "test/test_task_runner.h",
     "test/vm_test_utils.cc",
     "test/vm_test_utils.h",
   ]
 }
 
+source_set("test_task_runner") {
+  testonly = true
+  deps = [
+    ":base",
+    "../../gn:default_deps",
+  ]
+  sources = [
+    "test/test_task_runner.cc",
+    "test/test_task_runner.h",
+  ]
+}
+
 source_set("base_unittests") {
   testonly = true
   deps = [
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 266db1e..97e42eb 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -17,6 +17,7 @@
 source_set("end_to_end_integrationtests") {
   testonly = true
   deps = [
+    ":task_runner_thread",
     "../gn:default_deps",
     "../gn:gtest_deps",
     "../protos/perfetto/trace:lite",
@@ -36,3 +37,15 @@
     deps += [ "../src/base:android_task_runner" ]
   }
 }
+
+source_set("task_runner_thread") {
+  testonly = true
+  deps = [
+    "../gn:default_deps",
+    "../src/base:test_task_runner",
+  ]
+  sources = [
+    "task_runner_thread.cc",
+    "task_runner_thread.h",
+  ]
+}
diff --git a/test/end_to_end_integrationtest.cc b/test/end_to_end_integrationtest.cc
index bf4ef6d..3b9f6a7 100644
--- a/test/end_to_end_integrationtest.cc
+++ b/test/end_to_end_integrationtest.cc
@@ -35,6 +35,7 @@
 #include "src/traced/probes/ftrace_producer.h"
 #include "test/fake_consumer.h"
 #include "test/fake_producer.h"
+#include "test/task_runner_thread.h"
 
 #if BUILDFLAG(PERFETTO_ANDROID_BUILD)
 #include "perfetto/base/android_task_runner.h"
@@ -65,80 +66,6 @@
   ~PerfettoTest() override = default;
 
  protected:
-  class ThreadDelegate {
-   public:
-    virtual ~ThreadDelegate() = default;
-
-    // Invoke on the target thread before the message loop is started.
-    virtual void Initialize(base::TaskRunner* task_runner) = 0;
-  };
-
-  class TaskRunnerThread {
-   public:
-    TaskRunnerThread() = default;
-    ~TaskRunnerThread() {
-      {
-        std::unique_lock<std::mutex> lock(mutex_);
-        if (runner_)
-          runner_->Quit();
-      }
-
-      if (thread_.joinable())
-        thread_.join();
-    }
-
-    // Blocks until the thread has been created and Initialize() has been
-    // called.
-    void Start(std::unique_ptr<ThreadDelegate> delegate) {
-      // Begin holding the lock for the condition variable.
-      std::unique_lock<std::mutex> lock(mutex_);
-
-      // Start the thread.
-      PERFETTO_DCHECK(!runner_);
-      thread_ = std::thread(&TaskRunnerThread::Run, this, std::move(delegate));
-
-      // Wait for runner to be ready.
-      ready_.wait_for(lock, std::chrono::seconds(10),
-                      [this]() { return runner_ != nullptr; });
-    }
-
-   private:
-    void Run(std::unique_ptr<ThreadDelegate> delegate) {
-      // Create the task runner and execute the specicalised code.
-      base::PlatformTaskRunner task_runner;
-      delegate->Initialize(&task_runner);
-
-      // Pass the runner back to the main thread.
-      {
-        std::unique_lock<std::mutex> lock(mutex_);
-        runner_ = &task_runner;
-      }
-
-      // Notify the main thread that the runner is ready.
-      ready_.notify_one();
-
-      // Spin the loop.
-      task_runner.Run();
-
-      // Ensure we clear out the delegate before runner goes out
-      // of scope.
-      delegate.reset();
-
-      // Cleanup the runner.
-      {
-        std::unique_lock<std::mutex> lock(mutex_);
-        runner_ = nullptr;
-      }
-    }
-
-    std::thread thread_;
-    std::condition_variable ready_;
-
-    // All variables below this point are protected by |mutex_|.
-    std::mutex mutex_;
-    base::PlatformTaskRunner* runner_ = nullptr;
-  };
-
   // This is used only in standalone integrations tests. In CTS mode (i.e. when
   // PERFETTO_ANDROID_BUILD) this code is not used and instead the system
   // daemons are used
diff --git a/test/task_runner_thread.cc b/test/task_runner_thread.cc
new file mode 100644
index 0000000..4c1ee65
--- /dev/null
+++ b/test/task_runner_thread.cc
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <thread>
+
+#include "test/task_runner_thread.h"
+
+namespace perfetto {
+
+TaskRunnerThread::TaskRunnerThread() = default;
+TaskRunnerThread::~TaskRunnerThread() {
+  {
+    std::unique_lock<std::mutex> lock(mutex_);
+    if (runner_)
+      runner_->Quit();
+  }
+
+  if (thread_.joinable())
+    thread_.join();
+}
+
+void TaskRunnerThread::Start(std::unique_ptr<ThreadDelegate> delegate) {
+  // Begin holding the lock for the condition variable.
+  std::unique_lock<std::mutex> lock(mutex_);
+
+  // Start the thread.
+  PERFETTO_DCHECK(!runner_);
+  thread_ = std::thread(&TaskRunnerThread::Run, this, std::move(delegate));
+
+  // Wait for runner to be ready.
+  ready_.wait_for(lock, std::chrono::seconds(10),
+                  [this]() { return runner_ != nullptr; });
+}
+
+void TaskRunnerThread::Run(std::unique_ptr<ThreadDelegate> delegate) {
+  // Create the task runner and execute the specicalised code.
+  base::PlatformTaskRunner task_runner;
+  delegate->Initialize(&task_runner);
+
+  // Pass the runner back to the main thread.
+  {
+    std::unique_lock<std::mutex> lock(mutex_);
+    runner_ = &task_runner;
+  }
+
+  // Notify the main thread that the runner is ready.
+  ready_.notify_one();
+
+  // Spin the loop.
+  task_runner.Run();
+
+  // Ensure we clear out the delegate before runner goes out
+  // of scope.
+  delegate.reset();
+
+  // Cleanup the runner.
+  {
+    std::unique_lock<std::mutex> lock(mutex_);
+    runner_ = nullptr;
+  }
+}
+
+}  // namespace perfetto
diff --git a/test/task_runner_thread.h b/test/task_runner_thread.h
new file mode 100644
index 0000000..6a1ed72
--- /dev/null
+++ b/test/task_runner_thread.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef TEST_TASK_RUNNER_THREAD_H_
+#define TEST_TASK_RUNNER_THREAD_H_
+
+#include <condition_variable>
+#include <mutex>
+#include <thread>
+
+#include "perfetto/base/task_runner.h"
+#include "src/base/test/test_task_runner.h"
+
+namespace perfetto {
+
+// Used to perform initialization work on a background TaskRunnerThread.
+class ThreadDelegate {
+ public:
+  virtual ~ThreadDelegate() = default;
+
+  // Invoked on the target thread before the message loop is started.
+  virtual void Initialize(base::TaskRunner* task_runner) = 0;
+};
+
+// Background thread which spins a task runner until completed or the thread is
+// destroyed. If the thread is destroyed before the task runner completes, the
+// task runner is quit and the thread is joined.
+class TaskRunnerThread {
+ public:
+  TaskRunnerThread();
+  ~TaskRunnerThread();
+
+  // Blocks until the thread has been created and Initialize() has been
+  // called.
+  void Start(std::unique_ptr<ThreadDelegate> delegate);
+
+ private:
+  void Run(std::unique_ptr<ThreadDelegate> delegate);
+
+  std::thread thread_;
+  std::condition_variable ready_;
+
+  // All variables below this point are protected by |mutex_|.
+  std::mutex mutex_;
+  base::PlatformTaskRunner* runner_ = nullptr;
+};
+
+}  // namespace perfetto
+
+#endif  // TEST_TASK_RUNNER_THREAD_H