| Index: remoting/protocol/channel_multiplexer_unittest.cc
|
| diff --git a/remoting/protocol/channel_multiplexer_unittest.cc b/remoting/protocol/channel_multiplexer_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6afcb8c377a0a2f7b216fe12c56fabe5e28727f7
|
| --- /dev/null
|
| +++ b/remoting/protocol/channel_multiplexer_unittest.cc
|
| @@ -0,0 +1,167 @@
|
| +// Copyright (c) 2012 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 "remoting/protocol/channel_multiplexer.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/message_loop.h"
|
| +#include "net/socket/socket.h"
|
| +#include "net/socket/stream_socket.h"
|
| +#include "remoting/base/constants.h"
|
| +#include "remoting/protocol/connection_tester.h"
|
| +#include "remoting/protocol/fake_session.h"
|
| +#include "testing/gmock/include/gmock/gmock.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace remoting {
|
| +namespace protocol {
|
| +
|
| +namespace {
|
| +
|
| +const int kMessageSize = 1024;
|
| +const int kMessages = 100;
|
| +
|
| +void QuitCurrentThread() {
|
| + MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +class ChannelMultiplexerTest : public testing::Test {
|
| + protected:
|
| + virtual void SetUp() OVERRIDE {
|
| + // Create pair of multiplexers and connect them to each other.
|
| + host_mux_.reset(new ChannelMultiplexer(&host_session_));
|
| + client_mux_.reset(new ChannelMultiplexer(&client_session_));
|
| + FakeSocket* host_socket =
|
| + host_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName);
|
| + FakeSocket* client_socket =
|
| + client_session_.GetStreamChannel(ChannelMultiplexer::kMuxChannelName);
|
| + host_socket->PairWith(client_socket);
|
| +
|
| + // Make writes asynchronous in one direction.
|
| + host_socket->set_async_write(true);
|
| + client_socket->set_async_write(true);
|
| + }
|
| +
|
| + void CreateChannel(const std::string& name,
|
| + scoped_ptr<net::StreamSocket>* host_socket,
|
| + scoped_ptr<net::StreamSocket>* client_socket) {
|
| + int counter = 2;
|
| + host_mux_->CreateStreamChannel(name, base::Bind(
|
| + &ChannelMultiplexerTest::OnChannelConnected, base::Unretained(this),
|
| + host_socket, &counter));
|
| + client_mux_->CreateStreamChannel(name, base::Bind(
|
| + &ChannelMultiplexerTest::OnChannelConnected, base::Unretained(this),
|
| + client_socket, &counter));
|
| +
|
| + message_loop_.Run();
|
| +
|
| + EXPECT_TRUE(host_socket->get());
|
| + EXPECT_TRUE(client_socket->get());
|
| + }
|
| +
|
| + void OnChannelConnected(
|
| + scoped_ptr<net::StreamSocket>* storage,
|
| + int* counter,
|
| + scoped_ptr<net::StreamSocket> socket) {
|
| + *storage = socket.Pass();
|
| + --(*counter);
|
| + EXPECT_GE(*counter, 0);
|
| + if (*counter == 0)
|
| + QuitCurrentThread();
|
| + }
|
| +
|
| + MessageLoop message_loop_;
|
| +
|
| + FakeSession host_session_;
|
| + FakeSession client_session_;
|
| +
|
| + scoped_ptr<ChannelMultiplexer> host_mux_;
|
| + scoped_ptr<ChannelMultiplexer> client_mux_;
|
| +};
|
| +
|
| +
|
| +TEST_F(ChannelMultiplexerTest, OneChannel) {
|
| + scoped_ptr<net::StreamSocket> host_socket;
|
| + scoped_ptr<net::StreamSocket> client_socket;
|
| + ASSERT_NO_FATAL_FAILURE(CreateChannel("test", &host_socket, &client_socket));
|
| +
|
| + StreamConnectionTester tester(host_socket.get(), client_socket.get(),
|
| + kMessageSize, kMessages);
|
| + tester.Start();
|
| + message_loop_.Run();
|
| + tester.CheckResults();
|
| +}
|
| +
|
| +TEST_F(ChannelMultiplexerTest, TwoChannels) {
|
| + scoped_ptr<net::StreamSocket> host_socket1;
|
| + scoped_ptr<net::StreamSocket> client_socket1;
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateChannel("test", &host_socket1, &client_socket1));
|
| +
|
| + scoped_ptr<net::StreamSocket> host_socket2;
|
| + scoped_ptr<net::StreamSocket> client_socket2;
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateChannel("ch2", &host_socket2, &client_socket2));
|
| +
|
| + StreamConnectionTester tester1(host_socket1.get(), client_socket1.get(),
|
| + kMessageSize, kMessages);
|
| + StreamConnectionTester tester2(host_socket2.get(), client_socket2.get(),
|
| + kMessageSize, kMessages);
|
| + tester1.Start();
|
| + tester2.Start();
|
| + while (!tester1.done() || !tester2.done()) {
|
| + message_loop_.Run();
|
| + }
|
| + tester1.CheckResults();
|
| + tester2.CheckResults();
|
| +}
|
| +
|
| +// Four channels, two in each direction
|
| +TEST_F(ChannelMultiplexerTest, FourChannels) {
|
| + scoped_ptr<net::StreamSocket> host_socket1;
|
| + scoped_ptr<net::StreamSocket> client_socket1;
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateChannel("test", &host_socket1, &client_socket1));
|
| +
|
| + scoped_ptr<net::StreamSocket> host_socket2;
|
| + scoped_ptr<net::StreamSocket> client_socket2;
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateChannel("ch2", &host_socket2, &client_socket2));
|
| +
|
| + scoped_ptr<net::StreamSocket> host_socket3;
|
| + scoped_ptr<net::StreamSocket> client_socket3;
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateChannel("test3", &host_socket3, &client_socket3));
|
| +
|
| + scoped_ptr<net::StreamSocket> host_socket4;
|
| + scoped_ptr<net::StreamSocket> client_socket4;
|
| + ASSERT_NO_FATAL_FAILURE(
|
| + CreateChannel("ch4", &host_socket4, &client_socket4));
|
| +
|
| + StreamConnectionTester tester1(host_socket1.get(), client_socket1.get(),
|
| + kMessageSize, kMessages);
|
| + StreamConnectionTester tester2(host_socket2.get(), client_socket2.get(),
|
| + kMessageSize, kMessages);
|
| + StreamConnectionTester tester3(client_socket3.get(), host_socket3.get(),
|
| + kMessageSize, kMessages);
|
| + StreamConnectionTester tester4(client_socket4.get(), host_socket4.get(),
|
| + kMessageSize, kMessages);
|
| + tester1.Start();
|
| + tester2.Start();
|
| + tester3.Start();
|
| + tester4.Start();
|
| + while (!tester1.done() || !tester2.done() ||
|
| + !tester3.done() || !tester4.done()) {
|
| + message_loop_.Run();
|
| + }
|
| + tester1.Start();
|
| + tester2.Start();
|
| + tester3.Start();
|
| + tester4.Start();
|
| +}
|
| +
|
| +} // namespace protocol
|
| +} // namespace remoting
|
|
|