OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "blimp/net/grpc_connection.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include <string> |
| 10 #include <utility> |
| 11 |
| 12 #include "base/callback_helpers.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "blimp/common/create_blimp_message.h" |
| 16 #include "blimp/common/proto/blimp_message.pb.h" |
| 17 #include "blimp/common/public/session/assignment_options.h" |
| 18 #include "blimp/net/common.h" |
| 19 #include "blimp/net/connection_error_observer.h" |
| 20 #include "blimp/net/grpc_client_stream.h" |
| 21 #include "blimp/net/grpc_engine_stream.h" |
| 22 #include "blimp/net/grpc_stream.h" |
| 23 #include "blimp/net/message_port.h" |
| 24 #include "blimp/net/test_common.h" |
| 25 #include "net/base/completion_callback.h" |
| 26 #include "net/base/io_buffer.h" |
| 27 #include "net/base/net_errors.h" |
| 28 #include "net/base/test_completion_callback.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" |
| 31 |
| 32 using testing::_; |
| 33 using testing::InSequence; |
| 34 using testing::Return; |
| 35 using testing::SaveArg; |
| 36 |
| 37 namespace blimp { |
| 38 namespace { |
| 39 |
| 40 // This is a minimal test for GrpcConnection given that this class is going |
| 41 // away. |
| 42 class GrpcConnectionTest : public testing::Test { |
| 43 public: |
| 44 GrpcConnectionTest() { |
| 45 assignment_options_.engine_endpoint = |
| 46 net::IPEndPoint(net::IPAddress(127, 0, 0, 1), 0); |
| 47 receiver_ = new MockBlimpMessageProcessor(); |
| 48 } |
| 49 |
| 50 ~GrpcConnectionTest() override {} |
| 51 |
| 52 void DropConnection() { connection_.reset(); } |
| 53 |
| 54 protected: |
| 55 AssignmentOptions assignment_options_; |
| 56 |
| 57 std::unique_ptr<BlimpMessage> CreateInputMessage() { |
| 58 InputMessage* input; |
| 59 return CreateBlimpMessage(&input); |
| 60 } |
| 61 |
| 62 std::unique_ptr<BlimpMessage> CreateControlMessage() { |
| 63 TabControlMessage* control; |
| 64 return CreateBlimpMessage(&control); |
| 65 } |
| 66 |
| 67 base::MessageLoop message_loop_; |
| 68 |
| 69 MockBlimpMessageProcessor* receiver_; |
| 70 std::unique_ptr<BlimpConnection> connection_; |
| 71 }; |
| 72 |
| 73 class GrpcConnectionHelper { |
| 74 public: |
| 75 std::unique_ptr<GrpcEngineStream> engine; |
| 76 std::unique_ptr<GrpcClientStream> client; |
| 77 std::unique_ptr<GrpcConnection> connection; |
| 78 |
| 79 std::unique_ptr<GrpcConnection> Setup(AssignmentOptions assignment_options, |
| 80 bool is_engine) { |
| 81 net::TestCompletionCallback engine_callback; |
| 82 engine = base::MakeUnique<GrpcEngineStream>(assignment_options, |
| 83 engine_callback.callback()); |
| 84 |
| 85 net::TestCompletionCallback client_callback; |
| 86 client = base::MakeUnique<GrpcClientStream>(engine->GetAssignmentOptions(), |
| 87 client_callback.callback()); |
| 88 |
| 89 EXPECT_EQ(net::OK, engine_callback.WaitForResult()); |
| 90 EXPECT_EQ(net::OK, client_callback.WaitForResult()); |
| 91 |
| 92 if (is_engine) { |
| 93 return base::MakeUnique<GrpcConnection>(std::move(engine)); |
| 94 } else { |
| 95 return base::MakeUnique<GrpcConnection>(std::move(client)); |
| 96 } |
| 97 } |
| 98 }; |
| 99 |
| 100 // Helper to validate that a given |BlimpMessage| is received. |
| 101 void OnReceive(BlimpMessage* blimp_msg, net::TestCompletionCallback* received, |
| 102 std::unique_ptr<HeliumWrapper> received_msg, |
| 103 helium::Result result) { |
| 104 BlimpMessage received_blimp_msg; |
| 105 EXPECT_EQ(true, received_blimp_msg.ParseFromString( |
| 106 received_msg->serialized_helium_message())); |
| 107 std::string blimp_msg_str; |
| 108 EXPECT_EQ(true, blimp_msg->SerializeToString(&blimp_msg_str)); |
| 109 EXPECT_EQ(blimp_msg_str, received_msg->serialized_helium_message()); |
| 110 |
| 111 DVLOG(3) << "Received and checked msg: " << received_blimp_msg; |
| 112 received->callback().Run(static_cast<int>(result)); |
| 113 } |
| 114 |
| 115 // Helper to check when a |BlimpMessage| is being sent. |
| 116 void OnSend(net::TestCompletionCallback* sent, helium::Result result) { |
| 117 sent->callback().Run(static_cast<int>(result)); |
| 118 } |
| 119 |
| 120 // Sets up a gRPC connection on the engine-side and a simple gRPC stream for the |
| 121 // client to interact with. |
| 122 TEST_F(GrpcConnectionTest, SimpleConnect) { |
| 123 GrpcConnectionHelper helper; |
| 124 std::unique_ptr<GrpcConnection> connection = |
| 125 helper.Setup(assignment_options_, true); |
| 126 |
| 127 { // First test sending using the |BlimpConnection|. |
| 128 std::unique_ptr<BlimpMessage> blimp_msg = CreateInputMessage(); |
| 129 BlimpMessage copy_msg = *blimp_msg; |
| 130 net::TestCompletionCallback client_received_callback; |
| 131 helper.client->ReceiveMessage( |
| 132 base::Bind(&OnReceive, base::Unretained(©_msg), |
| 133 base::Unretained(&client_received_callback))); |
| 134 |
| 135 net::TestCompletionCallback engine_process_callback; |
| 136 connection->GetOutgoingMessageProcessor()->ProcessMessage( |
| 137 std::move(blimp_msg), engine_process_callback.callback()); |
| 138 EXPECT_EQ(net::OK, engine_process_callback.WaitForResult()); |
| 139 EXPECT_EQ(net::OK, client_received_callback.WaitForResult()); |
| 140 } |
| 141 |
| 142 { // Now test receiving using the |BlimpConnection|. |
| 143 std::unique_ptr<BlimpMessage> blimp_msg = CreateInputMessage(); |
| 144 BlimpMessage copy_msg = *blimp_msg; |
| 145 net::TestCompletionCallback engine_received_callback; |
| 146 BlimpMessage received_msg; |
| 147 receiver_->SetTestCompletionCallback(engine_received_callback.callback()); |
| 148 connection->SetIncomingMessageProcessor(receiver_); |
| 149 std::unique_ptr<HeliumWrapper> helium_msg = |
| 150 base::MakeUnique<HeliumWrapper>(); |
| 151 EXPECT_EQ(true, blimp_msg->SerializeToString( |
| 152 helium_msg->mutable_serialized_helium_message())); |
| 153 |
| 154 net::TestCompletionCallback sent; |
| 155 helper.client->SendMessage(std::move(helium_msg), |
| 156 base::Bind(&OnSend, base::Unretained(&sent))); |
| 157 EXPECT_EQ(net::OK, sent.WaitForResult()); |
| 158 EXPECT_EQ(net::OK, engine_received_callback.WaitForResult()); |
| 159 EXPECT_THAT(copy_msg, EqualsProto(receiver_->received_msg)); |
| 160 } |
| 161 } |
| 162 |
| 163 } // namespace |
| 164 } // namespace blimp |
OLD | NEW |