| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 #include "components/arc/standalone/service_helper.h" | |
| 6 | |
| 7 #include <signal.h> | |
| 8 #include <unistd.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/files/file_descriptor_watcher_posix.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/memory/ptr_util.h" | |
| 16 #include "base/message_loop/message_loop.h" | |
| 17 #include "base/run_loop.h" | |
| 18 #include "base/single_thread_task_runner.h" | |
| 19 #include "base/test/launcher/unit_test_launcher.h" | |
| 20 #include "base/test/test_suite.h" | |
| 21 #include "base/threading/platform_thread.h" | |
| 22 #include "base/time/time.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 namespace arc { | |
| 26 | |
| 27 class ServiceHelperTest : public ::testing::Test { | |
| 28 public: | |
| 29 ServiceHelperTest() {} | |
| 30 ~ServiceHelperTest() override {} | |
| 31 | |
| 32 void Quit() { | |
| 33 CHECK(base_loop_); | |
| 34 CHECK(run_loop_); | |
| 35 base_loop_->task_runner()->PostTask(FROM_HERE, run_loop_->QuitClosure()); | |
| 36 } | |
| 37 | |
| 38 void Init() { | |
| 39 // Dynamically create the message loop to avoid thread check failure of task | |
| 40 // runner in Debug mode. | |
| 41 base_loop_.reset(new base::MessageLoopForIO()); | |
| 42 run_loop_.reset(new base::RunLoop()); | |
| 43 | |
| 44 // Required to watch a file descriptor from ServiceHelper. | |
| 45 file_descriptor_watcher_ = | |
| 46 base::MakeUnique<FileDescriptorWatcher>(base_loop_.get()); | |
| 47 | |
| 48 // This cannot be put inside SetUp() because we need to run it after fork(). | |
| 49 helper_.reset(new ServiceHelper()); | |
| 50 helper_->Init(base::Bind(&ServiceHelperTest::Quit, | |
| 51 base::Unretained(this))); | |
| 52 } | |
| 53 | |
| 54 protected: | |
| 55 std::unique_ptr<base::MessageLoopForIO> base_loop_; | |
| 56 std::unique_ptr<base::MessageLoopForIO> file_descriptor_watcher_; | |
| 57 std::unique_ptr<base::RunLoop> run_loop_; | |
| 58 std::unique_ptr<ServiceHelper> helper_; | |
| 59 | |
| 60 private: | |
| 61 DISALLOW_COPY_AND_ASSIGN(ServiceHelperTest); | |
| 62 }; | |
| 63 | |
| 64 TEST_F(ServiceHelperTest, CheckExternalTerm) { | |
| 65 // Fork ourselves and run it. | |
| 66 pid_t child_pid = fork(); | |
| 67 if (child_pid == 0) { // Child process | |
| 68 Init(); | |
| 69 run_loop_->Run(); | |
| 70 helper_.reset(); // Make sure ServiceHelper dtor does not crash. | |
| 71 _Exit(EXIT_SUCCESS); | |
| 72 } else if (child_pid == -1) { // Failed to fork | |
| 73 FAIL(); | |
| 74 } else { // Parent process | |
| 75 // Deliberate race. If ServiceHelper had bugs and stalled, the short | |
| 76 // timeout would kill forked process and stop the test. In that case | |
| 77 // there will be a crash from improperly terminated message loop | |
| 78 // and we shall capture the error. | |
| 79 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(1)); | |
| 80 kill(child_pid, SIGTERM); | |
| 81 int status; | |
| 82 ASSERT_EQ(child_pid, waitpid(child_pid, &status, 0)); | |
| 83 ASSERT_TRUE(WIFEXITED(status)); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 } // namespace arc | |
| 88 | |
| 89 int main(int argc, char** argv) { | |
| 90 base::TestSuite test_suite(argc, argv); | |
| 91 return base::LaunchUnitTestsSerially( | |
| 92 argc, argv, | |
| 93 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); | |
| 94 } | |
| OLD | NEW |