diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-27 18:23:24 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-10-27 18:23:24 +0000 |
commit | 7568a1714cdbf3cf9cced51496f7d3b53738d524 (patch) | |
tree | 967f2a9a7a19edb5ca0ea3ee71110eb23c376176 /jingle | |
parent | f903d65c23759a2bc8f943b609cf020859c6375b (diff) | |
download | chromium_src-7568a1714cdbf3cf9cced51496f7d3b53738d524.zip chromium_src-7568a1714cdbf3cf9cced51496f7d3b53738d524.tar.gz chromium_src-7568a1714cdbf3cf9cced51496f7d3b53738d524.tar.bz2 |
[Sync] Added unittests for XmppConnectionGenerator.
Removed unused public function.
BUG=None
TEST=notifier_unit_tests
Review URL: http://codereview.chromium.org/4141004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@64101 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'jingle')
-rw-r--r-- | jingle/jingle.gyp | 1 | ||||
-rw-r--r-- | jingle/notifier/communicator/xmpp_connection_generator.cc | 34 | ||||
-rw-r--r-- | jingle/notifier/communicator/xmpp_connection_generator.h | 1 | ||||
-rw-r--r-- | jingle/notifier/communicator/xmpp_connection_generator_unittest.cc | 107 |
4 files changed, 122 insertions, 21 deletions
diff --git a/jingle/jingle.gyp b/jingle/jingle.gyp index 01844b3..939c9d0 100644 --- a/jingle/jingle.gyp +++ b/jingle/jingle.gyp @@ -101,6 +101,7 @@ 'notifier/base/fake_ssl_client_socket_unittest.cc', 'notifier/base/xmpp_connection_unittest.cc', 'notifier/base/weak_xmpp_client_unittest.cc', + 'notifier/communicator/xmpp_connection_generator_unittest.cc', 'notifier/listener/push_notifications_subscribe_task_unittest.cc', 'notifier/listener/talk_mediator_unittest.cc', 'notifier/listener/send_update_task_unittest.cc', diff --git a/jingle/notifier/communicator/xmpp_connection_generator.cc b/jingle/notifier/communicator/xmpp_connection_generator.cc index d50d800..c1ca8d2 100644 --- a/jingle/notifier/communicator/xmpp_connection_generator.cc +++ b/jingle/notifier/communicator/xmpp_connection_generator.cc @@ -81,6 +81,14 @@ void XmppConnectionGenerator::StartGenerating() { UseNextConnection(); } +namespace { + +const char* const PROTO_NAMES[cricket::PROTO_LAST + 1] = { + "udp", "tcp", "ssltcp" +}; + +} // namespace + void XmppConnectionGenerator::UseNextConnection() { DCHECK(settings_list_.get()); // Loop until we can use a connection or we run out of connections @@ -91,7 +99,12 @@ void XmppConnectionGenerator::UseNextConnection() { if (settings_index_ < settings_list_->GetCount()) { // We have more connection settings in the settings_list_ to // try, kick off the next one. - UseCurrentConnection(); + ConnectionSettings* settings = + settings_list_->GetSettings(settings_index_); + VLOG(1) << "*** Attempting " << PROTO_NAMES[settings->protocol()] + << " connection to " << settings->server().IPAsString() + << ":" << settings->server().port(); + delegate_->OnNewSettings(*settings); return; } @@ -169,23 +182,4 @@ void XmppConnectionGenerator::HandleServerDNSResolved(int status) { try_ssltcp_first_); } -static const char* const PROTO_NAMES[cricket::PROTO_LAST + 1] = { - "udp", "tcp", "ssltcp" -}; - -static const char* ProtocolToString(cricket::ProtocolType proto) { - return PROTO_NAMES[proto]; -} - -void XmppConnectionGenerator::UseCurrentConnection() { - VLOG(1) << "XmppConnectionGenerator::UseCurrentConnection"; - - ConnectionSettings* settings = settings_list_->GetSettings(settings_index_); - VLOG(1) << "*** Attempting " << ProtocolToString(settings->protocol()) - << " connection to " << settings->server().IPAsString() - << ":" << settings->server().port(); - - delegate_->OnNewSettings(*settings); -} - } // namespace notifier diff --git a/jingle/notifier/communicator/xmpp_connection_generator.h b/jingle/notifier/communicator/xmpp_connection_generator.h index 07106a0..0c3085c 100644 --- a/jingle/notifier/communicator/xmpp_connection_generator.h +++ b/jingle/notifier/communicator/xmpp_connection_generator.h @@ -63,7 +63,6 @@ class XmppConnectionGenerator { void StartGenerating(); void UseNextConnection(); - void UseCurrentConnection(); private: void OnServerDNSResolved(int status); diff --git a/jingle/notifier/communicator/xmpp_connection_generator_unittest.cc b/jingle/notifier/communicator/xmpp_connection_generator_unittest.cc new file mode 100644 index 0000000..70d24a0 --- /dev/null +++ b/jingle/notifier/communicator/xmpp_connection_generator_unittest.cc @@ -0,0 +1,107 @@ +// Copyright (c) 2010 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 "jingle/notifier/communicator/xmpp_connection_generator.h" + +#include "base/basictypes.h" +#include "jingle/notifier/communicator/connection_options.h" +#include "jingle/notifier/communicator/connection_settings.h" +#include "net/base/host_port_pair.h" +#include "net/base/mock_host_resolver.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace notifier { + +namespace { + +using ::testing::_; +using ::testing::InvokeWithoutArgs; + +class MockXmppConnectionGeneratorDelegate + : public XmppConnectionGenerator::Delegate { + public: + MOCK_METHOD1(OnNewSettings, void(const ConnectionSettings&)); + MOCK_METHOD2(OnExhaustedSettings, void(bool, int)); +}; + +const ServerInformation kXmppServers[] = { + { net::HostPortPair("www.foo.com", 5222), true }, + { net::HostPortPair("www.bar.com", 8080), false }, + { net::HostPortPair("www.baz.com", 80), true }, +}; + +class XmppConnectionGeneratorTest : public testing::Test { + public: + XmppConnectionGeneratorTest() + : xmpp_connection_generator_( + &delegate_, + &mock_host_resolver_, + &connection_options_, + false /* try_ssltcp_first */, + kXmppServers, + arraysize(kXmppServers)) {} + + virtual ~XmppConnectionGeneratorTest() {} + + protected: + MockXmppConnectionGeneratorDelegate delegate_; + net::MockHostResolver mock_host_resolver_; + ConnectionOptions connection_options_; + XmppConnectionGenerator xmpp_connection_generator_; +}; + +TEST_F(XmppConnectionGeneratorTest, DnsFailure) { + MessageLoop message_loop; + + EXPECT_CALL(delegate_, OnNewSettings(_)).Times(0); + EXPECT_CALL(delegate_, OnExhaustedSettings(_, _)). + WillOnce(InvokeWithoutArgs(&message_loop, &MessageLoop::Quit)); + + mock_host_resolver_.rules()->AddSimulatedFailure("*"); + xmpp_connection_generator_.StartGenerating(); + message_loop.Run(); +} + +TEST_F(XmppConnectionGeneratorTest, DnsFailureSynchronous) { + EXPECT_CALL(delegate_, OnNewSettings(_)).Times(0); + EXPECT_CALL(delegate_, OnExhaustedSettings(_, _)).Times(1); + + mock_host_resolver_.set_synchronous_mode(true); + mock_host_resolver_.rules()->AddSimulatedFailure("*"); + xmpp_connection_generator_.StartGenerating(); +} + +TEST_F(XmppConnectionGeneratorTest, ConnectionFailure) { + MessageLoop message_loop; + + EXPECT_CALL(delegate_, OnNewSettings(_)). + Times(5). + WillRepeatedly( + InvokeWithoutArgs( + &xmpp_connection_generator_, + &XmppConnectionGenerator::UseNextConnection)); + EXPECT_CALL(delegate_, OnExhaustedSettings(_, _)). + WillOnce(InvokeWithoutArgs(&message_loop, &MessageLoop::Quit)); + + xmpp_connection_generator_.StartGenerating(); + message_loop.Run(); +} + +TEST_F(XmppConnectionGeneratorTest, ConnectionFailureSynchronous) { + EXPECT_CALL(delegate_, OnNewSettings(_)). + Times(5). + WillRepeatedly( + InvokeWithoutArgs( + &xmpp_connection_generator_, + &XmppConnectionGenerator::UseNextConnection)); + EXPECT_CALL(delegate_, OnExhaustedSettings(_, _)).Times(1); + + mock_host_resolver_.set_synchronous_mode(true); + xmpp_connection_generator_.StartGenerating(); +} + +} // namespace + +} // namespace notifier |