| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "blimp/test/fake_engine/fake_engine.h" | 5 #include "blimp/test/fake_engine/fake_engine.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <sys/socket.h> |
| 9 #include <sys/un.h> |
| 8 | 10 |
| 9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 12 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/test/launcher/unit_test_launcher.h" | 15 #include "base/test/launcher/unit_test_launcher.h" |
| 14 #include "base/test/test_suite.h" | 16 #include "base/test/test_suite.h" |
| 15 #include "blimp/test/fake_engine/proto/engine.grpc.pb.h" | 17 #include "blimp/test/fake_engine/proto/engine.grpc.pb.h" |
| 16 #include "blimp/test/fake_engine/proto/lifetime.grpc.pb.h" | 18 #include "blimp/test/fake_engine/proto/lifetime.grpc.pb.h" |
| 17 #include "blimp/test/fake_engine/proto/logging.grpc.pb.h" | 19 #include "blimp/test/fake_engine/proto/logging.grpc.pb.h" |
| 18 #include "ipc/ipc_channel.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "third_party/grpc/include/grpc++/create_channel_posix.h" | 22 #include "third_party/grpc/include/grpc++/create_channel_posix.h" |
| 22 #include "third_party/grpc/include/grpc++/grpc++.h" | 23 #include "third_party/grpc/include/grpc++/grpc++.h" |
| 23 #include "third_party/grpc/include/grpc++/server_posix.h" | 24 #include "third_party/grpc/include/grpc++/server_posix.h" |
| 24 | 25 |
| 25 using ::testing::_; | 26 using ::testing::_; |
| 26 | 27 |
| 27 namespace blimp { | 28 namespace blimp { |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 32 bool SocketPair(int* fd1, int* fd2) { |
| 33 int pipe_fds[2]; |
| 34 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
| 35 PLOG(ERROR) << "socketpair()"; |
| 36 return false; |
| 37 } |
| 38 |
| 39 // Set both ends to be non-blocking. |
| 40 if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || |
| 41 fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { |
| 42 PLOG(ERROR) << "fcntl(O_NONBLOCK)"; |
| 43 if (IGNORE_EINTR(close(pipe_fds[0])) < 0) |
| 44 PLOG(ERROR) << "close"; |
| 45 if (IGNORE_EINTR(close(pipe_fds[1])) < 0) |
| 46 PLOG(ERROR) << "close"; |
| 47 return false; |
| 48 } |
| 49 |
| 50 *fd1 = pipe_fds[0]; |
| 51 *fd2 = pipe_fds[1]; |
| 52 |
| 53 return true; |
| 54 } |
| 55 |
| 31 class MockLifetimeService : public Lifetime::Service { | 56 class MockLifetimeService : public Lifetime::Service { |
| 32 public: | 57 public: |
| 33 MOCK_METHOD3(EngineReady, grpc::Status( | 58 MOCK_METHOD3(EngineReady, grpc::Status( |
| 34 grpc::ServerContext*, const EngineReadyRequest*, EngineReadyResponse*)); | 59 grpc::ServerContext*, const EngineReadyRequest*, EngineReadyResponse*)); |
| 35 }; | 60 }; |
| 36 | 61 |
| 37 class MockLoggingService : public Logging::Service { | 62 class MockLoggingService : public Logging::Service { |
| 38 public: | 63 public: |
| 39 MOCK_METHOD3(Log, grpc::Status( | 64 MOCK_METHOD3(Log, grpc::Status( |
| 40 grpc::ServerContext*, const LogRequest*, LogResponse*)); | 65 grpc::ServerContext*, const LogRequest*, LogResponse*)); |
| 41 }; | 66 }; |
| 42 | 67 |
| 43 // The FakeEngineAppTest suite sets up a simple interface resembling the | 68 // The FakeEngineAppTest suite sets up a simple interface resembling the |
| 44 // Service, which is required by the Fake Engine. This allows testing whether | 69 // Service, which is required by the Fake Engine. This allows testing whether |
| 45 // the Fake Engine serves its purpose of simulating the real Engine as spawned | 70 // the Fake Engine serves its purpose of simulating the real Engine as spawned |
| 46 // by the Service. | 71 // by the Service. |
| 47 class FakeEngineAppTest : public testing::Test { | 72 class FakeEngineAppTest : public testing::Test { |
| 48 public: | 73 public: |
| 49 FakeEngineAppTest() | 74 FakeEngineAppTest() |
| 50 : engine_ready_(base::WaitableEvent::ResetPolicy::MANUAL, | 75 : engine_ready_(base::WaitableEvent::ResetPolicy::MANUAL, |
| 51 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 76 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
| 52 | 77 |
| 53 protected: | 78 protected: |
| 54 void SetUp() override { | 79 void SetUp() override { |
| 55 CHECK(IPC::SocketPair(&rendering_server_connect_fd_, &engine_listen_fd_)); | 80 CHECK(SocketPair(&rendering_server_connect_fd_, &engine_listen_fd_)); |
| 56 CHECK(IPC::SocketPair(&rendering_server_listen_fd_, &engine_connect_fd_)); | 81 CHECK(SocketPair(&rendering_server_listen_fd_, &engine_connect_fd_)); |
| 57 | 82 |
| 58 grpc::ServerBuilder builder; | 83 grpc::ServerBuilder builder; |
| 59 builder.RegisterService(&mock_lifetime_service_); | 84 builder.RegisterService(&mock_lifetime_service_); |
| 60 builder.RegisterService(&mock_logging_service_); | 85 builder.RegisterService(&mock_logging_service_); |
| 61 grpc_server_ = builder.BuildAndStart(); | 86 grpc_server_ = builder.BuildAndStart(); |
| 62 | 87 |
| 63 // TODO(xyzzyz): Remove fcntl when https://github.com/grpc/grpc/pull/8051 is | 88 // TODO(xyzzyz): Remove fcntl when https://github.com/grpc/grpc/pull/8051 is |
| 64 // merged and added to Chromium. | 89 // merged and added to Chromium. |
| 65 int flags = fcntl(rendering_server_listen_fd_, F_GETFL, 0); | 90 int flags = fcntl(rendering_server_listen_fd_, F_GETFL, 0); |
| 66 CHECK_EQ(0, fcntl(rendering_server_listen_fd_, F_SETFL, | 91 CHECK_EQ(0, fcntl(rendering_server_listen_fd_, F_SETFL, |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 } // namespace | 205 } // namespace |
| 181 | 206 |
| 182 } // namespace blimp | 207 } // namespace blimp |
| 183 | 208 |
| 184 int main(int argc, char** argv) { | 209 int main(int argc, char** argv) { |
| 185 blimp::FakeEngineTestSuite test_suite(argc, argv); | 210 blimp::FakeEngineTestSuite test_suite(argc, argv); |
| 186 return base::LaunchUnitTests( | 211 return base::LaunchUnitTests( |
| 187 argc, argv, | 212 argc, argv, |
| 188 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); | 213 base::Bind(&base::TestSuite::Run, base::Unretained(&test_suite))); |
| 189 } | 214 } |
| OLD | NEW |