forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Sync] Fixed sync crash regression in ServerNotifierThread
Added unit tests for ServerNotifierThread. Put DISABLE_RUNNABLE_METHOD_REFCOUNT() call for MediatorThreadImpl in the right place. BUG=57898 TEST=New unittests Review URL: http://codereview.chromium.org/5722002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@68891 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
akalin@chromium.org
committed
Dec 10, 2010
1 parent
62e43e2
commit 5827135
Showing
7 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
chrome/browser/sync/notifier/server_notifier_thread_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// 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 <string> | ||
|
||
#include "base/compiler_specific.h" | ||
#include "base/message_loop.h" | ||
#include "base/scoped_ptr.h" | ||
#include "base/task.h" | ||
#include "chrome/browser/sync/notifier/server_notifier_thread.h" | ||
#include "chrome/browser/sync/notifier/state_writer.h" | ||
#include "jingle/notifier/base/fake_base_task.h" | ||
#include "jingle/notifier/base/notifier_options.h" | ||
#include "talk/xmpp/xmppclientsettings.h" | ||
#include "testing/gmock/include/gmock/gmock.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace sync_notifier { | ||
|
||
class FakeServerNotifierThread : public ServerNotifierThread { | ||
public: | ||
FakeServerNotifierThread() | ||
: ServerNotifierThread(notifier::NotifierOptions(), "fake state", | ||
ALLOW_THIS_IN_INITIALIZER_LIST(this)) {} | ||
|
||
virtual ~FakeServerNotifierThread() {} | ||
|
||
virtual void Start() { | ||
DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | ||
ServerNotifierThread::Start(); | ||
worker_message_loop()->PostTask( | ||
FROM_HERE, | ||
NewRunnableMethod(this, | ||
&FakeServerNotifierThread::InitFakes)); | ||
} | ||
|
||
virtual void Logout() { | ||
DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | ||
worker_message_loop()->PostTask( | ||
FROM_HERE, | ||
NewRunnableMethod(this, | ||
&FakeServerNotifierThread::DestroyFakes)); | ||
ServerNotifierThread::Logout(); | ||
} | ||
|
||
// We prevent the real connection attempt from happening and use | ||
// SimulateConnection()/SimulateDisconnection() instead. | ||
virtual void Login(const buzz::XmppClientSettings& settings) { | ||
DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | ||
} | ||
|
||
// We pass ourselves as the StateWriter in the constructor, so shim | ||
// out WriteState() to prevent an infinite loop. | ||
virtual void WriteState(const std::string& state) { | ||
DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | ||
} | ||
|
||
void SimulateConnect() { | ||
DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | ||
worker_message_loop()->PostTask( | ||
FROM_HERE, | ||
NewRunnableMethod(this, | ||
&FakeServerNotifierThread::DoSimulateConnect)); | ||
} | ||
|
||
void SimulateDisconnect() { | ||
DCHECK_EQ(MessageLoop::current(), parent_message_loop_); | ||
worker_message_loop()->PostTask( | ||
FROM_HERE, | ||
NewRunnableMethod(this, | ||
&FakeServerNotifierThread::DoSimulateDisconnect)); | ||
} | ||
|
||
private: | ||
void InitFakes() { | ||
DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | ||
fake_base_task_.reset(new notifier::FakeBaseTask()); | ||
} | ||
|
||
void DestroyFakes() { | ||
DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | ||
fake_base_task_.reset(); | ||
} | ||
|
||
void DoSimulateConnect() { | ||
DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | ||
OnConnect(fake_base_task_->AsWeakPtr()); | ||
} | ||
|
||
void DoSimulateDisconnect() { | ||
DCHECK_EQ(MessageLoop::current(), worker_message_loop()); | ||
OnDisconnect(); | ||
} | ||
|
||
// Used only on the worker thread. | ||
scoped_ptr<notifier::FakeBaseTask> fake_base_task_; | ||
}; | ||
|
||
} // namespace sync_notifier | ||
|
||
DISABLE_RUNNABLE_METHOD_REFCOUNT(sync_notifier::FakeServerNotifierThread); | ||
|
||
namespace sync_notifier { | ||
|
||
namespace { | ||
|
||
class ServerNotifierThreadTest : public testing::Test { | ||
protected: | ||
MessageLoop message_loop_; | ||
}; | ||
|
||
TEST_F(ServerNotifierThreadTest, Basic) { | ||
FakeServerNotifierThread server_notifier_thread; | ||
|
||
server_notifier_thread.Start(); | ||
server_notifier_thread.Login(buzz::XmppClientSettings()); | ||
server_notifier_thread.SimulateConnect(); | ||
server_notifier_thread.ListenForUpdates(); | ||
server_notifier_thread.SubscribeForUpdates(std::vector<std::string>()); | ||
server_notifier_thread.Logout(); | ||
} | ||
|
||
TEST_F(ServerNotifierThreadTest, DisconnectBeforeListen) { | ||
FakeServerNotifierThread server_notifier_thread; | ||
|
||
server_notifier_thread.Start(); | ||
server_notifier_thread.Login(buzz::XmppClientSettings()); | ||
server_notifier_thread.ListenForUpdates(); | ||
server_notifier_thread.SubscribeForUpdates(std::vector<std::string>()); | ||
server_notifier_thread.Logout(); | ||
} | ||
|
||
TEST_F(ServerNotifierThreadTest, Disconnected) { | ||
FakeServerNotifierThread server_notifier_thread; | ||
|
||
server_notifier_thread.Start(); | ||
server_notifier_thread.Login(buzz::XmppClientSettings()); | ||
server_notifier_thread.SimulateConnect(); | ||
server_notifier_thread.SimulateDisconnect(); | ||
server_notifier_thread.ListenForUpdates(); | ||
server_notifier_thread.SubscribeForUpdates(std::vector<std::string>()); | ||
server_notifier_thread.Logout(); | ||
} | ||
|
||
} // namespace | ||
|
||
} // namespace sync_notifier |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters