diff options
author | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-16 01:06:46 +0000 |
---|---|---|
committer | kkania@chromium.org <kkania@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-16 01:06:46 +0000 |
commit | 952394afc0a0dc5e3e6a1f3c2e1c0fa99b7b681f (patch) | |
tree | 85d8246efdada076c5dda1b0a561d6c3322f37d1 /ipc/ipc_sync_channel_unittest.cc | |
parent | 8bbba87862f0ce45234380cebf60e3ded5d88147 (diff) | |
download | chromium_src-952394afc0a0dc5e3e6a1f3c2e1c0fa99b7b681f.zip chromium_src-952394afc0a0dc5e3e6a1f3c2e1c0fa99b7b681f.tar.gz chromium_src-952394afc0a0dc5e3e6a1f3c2e1c0fa99b7b681f.tar.bz2 |
Allow proxy channels to be created without initializing the underlying channel.
This fixes a bug where a client needed to guarantee a message filter was in
place before any messages were received.
It also follows the style of not having constructors that do complex
initialization.
BUG=102894
TEST=none
Review URL: http://codereview.chromium.org/8417054
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@110229 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ipc/ipc_sync_channel_unittest.cc')
-rw-r--r-- | ipc/ipc_sync_channel_unittest.cc | 79 |
1 files changed, 76 insertions, 3 deletions
diff --git a/ipc/ipc_sync_channel_unittest.cc b/ipc/ipc_sync_channel_unittest.cc index 029e49c..6982b28 100644 --- a/ipc/ipc_sync_channel_unittest.cc +++ b/ipc/ipc_sync_channel_unittest.cc @@ -120,6 +120,7 @@ class Worker : public Channel::Listener, public Message::Sender { DCHECK_EQ(answer, (succeed ? 10 : 0)); return result; } + const std::string& channel_name() { return channel_name_; } Channel::Mode mode() { return mode_; } WaitableEvent* done_event() { return done_.get(); } WaitableEvent* shutdown_event() { return &shutdown_event_; } @@ -156,6 +157,12 @@ class Worker : public Channel::Listener, public Message::Sender { NOTREACHED(); } + virtual SyncChannel* CreateChannel() { + return new SyncChannel( + channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true, + &shutdown_event_); + } + base::Thread* ListenerThread() { return overrided_thread_ ? overrided_thread_ : &listener_thread_; } @@ -167,9 +174,7 @@ class Worker : public Channel::Listener, public Message::Sender { void OnStart() { // Link ipc_thread_, listener_thread_ and channel_ altogether. StartThread(&ipc_thread_, MessageLoop::TYPE_IO); - channel_.reset(new SyncChannel( - channel_name_, mode_, this, ipc_thread_.message_loop_proxy(), true, - &shutdown_event_)); + channel_.reset(CreateChannel()); channel_created_->Signal(); Run(); } @@ -311,6 +316,74 @@ TEST_F(IPCSyncChannelTest, Simple) { namespace { +// Worker classes which override how the sync channel is created to use the +// two-step initialization (calling the lightweight constructor and then +// ChannelProxy::Init separately) process. +class TwoStepServer : public Worker { + public: + explicit TwoStepServer(bool create_pipe_now) + : Worker(Channel::MODE_SERVER, "simpler_server"), + create_pipe_now_(create_pipe_now) { } + + void Run() { + SendAnswerToLife(false, base::kNoTimeout, true); + Done(); + } + + virtual SyncChannel* CreateChannel() { + SyncChannel* channel = new SyncChannel( + this, ipc_thread().message_loop_proxy(), shutdown_event()); + channel->Init(channel_name(), mode(), create_pipe_now_); + return channel; + } + + bool create_pipe_now_; +}; + +class TwoStepClient : public Worker { + public: + TwoStepClient(bool create_pipe_now) + : Worker(Channel::MODE_CLIENT, "simple_client"), + create_pipe_now_(create_pipe_now) { } + + void OnAnswer(int* answer) { + *answer = 42; + Done(); + } + + virtual SyncChannel* CreateChannel() { + SyncChannel* channel = new SyncChannel( + this, ipc_thread().message_loop_proxy(), shutdown_event()); + channel->Init(channel_name(), mode(), create_pipe_now_); + return channel; + } + + bool create_pipe_now_; +}; + +void TwoStep(bool create_server_pipe_now, bool create_client_pipe_now) { + std::vector<Worker*> workers; + workers.push_back(new TwoStepServer(create_server_pipe_now)); + workers.push_back(new TwoStepClient(create_client_pipe_now)); + RunTest(workers); +} + +} // namespace + +// Tests basic two-step initialization, where you call the lightweight +// constructor then Init. +TEST_F(IPCSyncChannelTest, TwoStepInitialization) { + TwoStep(false, false); + TwoStep(false, true); + TwoStep(true, false); + TwoStep(true, true); +} + + +//----------------------------------------------------------------------------- + +namespace { + class DelayClient : public Worker { public: DelayClient() : Worker(Channel::MODE_CLIENT, "delay_client") { } |