| Index: blimp/net/grpc_connection_unittest.cc
|
| diff --git a/blimp/net/grpc_connection_unittest.cc b/blimp/net/grpc_connection_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..13b71f51027ffe9b8f4a517954d9e620a53a8fdd
|
| --- /dev/null
|
| +++ b/blimp/net/grpc_connection_unittest.cc
|
| @@ -0,0 +1,164 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "blimp/net/grpc_connection.h"
|
| +
|
| +#include <stddef.h>
|
| +
|
| +#include <string>
|
| +#include <utility>
|
| +
|
| +#include "base/callback_helpers.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "blimp/common/create_blimp_message.h"
|
| +#include "blimp/common/proto/blimp_message.pb.h"
|
| +#include "blimp/common/public/session/assignment_options.h"
|
| +#include "blimp/net/common.h"
|
| +#include "blimp/net/connection_error_observer.h"
|
| +#include "blimp/net/grpc_client_stream.h"
|
| +#include "blimp/net/grpc_engine_stream.h"
|
| +#include "blimp/net/grpc_stream.h"
|
| +#include "blimp/net/message_port.h"
|
| +#include "blimp/net/test_common.h"
|
| +#include "net/base/completion_callback.h"
|
| +#include "net/base/io_buffer.h"
|
| +#include "net/base/net_errors.h"
|
| +#include "net/base/test_completion_callback.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +using testing::_;
|
| +using testing::InSequence;
|
| +using testing::Return;
|
| +using testing::SaveArg;
|
| +
|
| +namespace blimp {
|
| +namespace {
|
| +
|
| +// This is a minimal test for GrpcConnection given that this class is going
|
| +// away.
|
| +class GrpcConnectionTest : public testing::Test {
|
| + public:
|
| + GrpcConnectionTest() {
|
| + assignment_options_.engine_endpoint =
|
| + net::IPEndPoint(net::IPAddress(127, 0, 0, 1), 0);
|
| + receiver_ = new MockBlimpMessageProcessor();
|
| + }
|
| +
|
| + ~GrpcConnectionTest() override {}
|
| +
|
| + void DropConnection() { connection_.reset(); }
|
| +
|
| + protected:
|
| + AssignmentOptions assignment_options_;
|
| +
|
| + std::unique_ptr<BlimpMessage> CreateInputMessage() {
|
| + InputMessage* input;
|
| + return CreateBlimpMessage(&input);
|
| + }
|
| +
|
| + std::unique_ptr<BlimpMessage> CreateControlMessage() {
|
| + TabControlMessage* control;
|
| + return CreateBlimpMessage(&control);
|
| + }
|
| +
|
| + base::MessageLoop message_loop_;
|
| +
|
| + MockBlimpMessageProcessor* receiver_;
|
| + std::unique_ptr<BlimpConnection> connection_;
|
| +};
|
| +
|
| +class GrpcConnectionHelper {
|
| + public:
|
| + std::unique_ptr<GrpcEngineStream> engine;
|
| + std::unique_ptr<GrpcClientStream> client;
|
| + std::unique_ptr<GrpcConnection> connection;
|
| +
|
| + std::unique_ptr<GrpcConnection> Setup(AssignmentOptions assignment_options,
|
| + bool is_engine) {
|
| + net::TestCompletionCallback engine_callback;
|
| + engine = base::MakeUnique<GrpcEngineStream>(assignment_options,
|
| + engine_callback.callback());
|
| +
|
| + net::TestCompletionCallback client_callback;
|
| + client = base::MakeUnique<GrpcClientStream>(engine->GetAssignmentOptions(),
|
| + client_callback.callback());
|
| +
|
| + EXPECT_EQ(net::OK, engine_callback.WaitForResult());
|
| + EXPECT_EQ(net::OK, client_callback.WaitForResult());
|
| +
|
| + if (is_engine) {
|
| + return base::MakeUnique<GrpcConnection>(std::move(engine));
|
| + } else {
|
| + return base::MakeUnique<GrpcConnection>(std::move(client));
|
| + }
|
| + }
|
| +};
|
| +
|
| +// Helper to validate that a given |BlimpMessage| is received.
|
| +void OnReceive(BlimpMessage* blimp_msg, net::TestCompletionCallback* received,
|
| + std::unique_ptr<HeliumWrapper> received_msg,
|
| + helium::Result result) {
|
| + BlimpMessage received_blimp_msg;
|
| + EXPECT_EQ(true, received_blimp_msg.ParseFromString(
|
| + received_msg->serialized_helium_message()));
|
| + std::string blimp_msg_str;
|
| + EXPECT_EQ(true, blimp_msg->SerializeToString(&blimp_msg_str));
|
| + EXPECT_EQ(blimp_msg_str, received_msg->serialized_helium_message());
|
| +
|
| + DVLOG(3) << "Received and checked msg: " << received_blimp_msg;
|
| + received->callback().Run(static_cast<int>(result));
|
| +}
|
| +
|
| +// Helper to check when a |BlimpMessage| is being sent.
|
| +void OnSend(net::TestCompletionCallback* sent, helium::Result result) {
|
| + sent->callback().Run(static_cast<int>(result));
|
| +}
|
| +
|
| +// Sets up a gRPC connection on the engine-side and a simple gRPC stream for the
|
| +// client to interact with.
|
| +TEST_F(GrpcConnectionTest, SimpleConnect) {
|
| + GrpcConnectionHelper helper;
|
| + std::unique_ptr<GrpcConnection> connection =
|
| + helper.Setup(assignment_options_, true);
|
| +
|
| + { // First test sending using the |BlimpConnection|.
|
| + std::unique_ptr<BlimpMessage> blimp_msg = CreateInputMessage();
|
| + BlimpMessage copy_msg = *blimp_msg;
|
| + net::TestCompletionCallback client_received_callback;
|
| + helper.client->ReceiveMessage(
|
| + base::Bind(&OnReceive, base::Unretained(©_msg),
|
| + base::Unretained(&client_received_callback)));
|
| +
|
| + net::TestCompletionCallback engine_process_callback;
|
| + connection->GetOutgoingMessageProcessor()->ProcessMessage(
|
| + std::move(blimp_msg), engine_process_callback.callback());
|
| + EXPECT_EQ(net::OK, engine_process_callback.WaitForResult());
|
| + EXPECT_EQ(net::OK, client_received_callback.WaitForResult());
|
| + }
|
| +
|
| + { // Now test receiving using the |BlimpConnection|.
|
| + std::unique_ptr<BlimpMessage> blimp_msg = CreateInputMessage();
|
| + BlimpMessage copy_msg = *blimp_msg;
|
| + net::TestCompletionCallback engine_received_callback;
|
| + BlimpMessage received_msg;
|
| + receiver_->SetTestCompletionCallback(engine_received_callback.callback());
|
| + connection->SetIncomingMessageProcessor(receiver_);
|
| + std::unique_ptr<HeliumWrapper> helium_msg =
|
| + base::MakeUnique<HeliumWrapper>();
|
| + EXPECT_EQ(true, blimp_msg->SerializeToString(
|
| + helium_msg->mutable_serialized_helium_message()));
|
| +
|
| + net::TestCompletionCallback sent;
|
| + helper.client->SendMessage(std::move(helium_msg),
|
| + base::Bind(&OnSend, base::Unretained(&sent)));
|
| + EXPECT_EQ(net::OK, sent.WaitForResult());
|
| + EXPECT_EQ(net::OK, engine_received_callback.WaitForResult());
|
| + EXPECT_THAT(copy_msg, EqualsProto(receiver_->received_msg));
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +} // namespace blimp
|
|
|