Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: ipc/unix_domain_socket_util_unittest.cc

Issue 14383024: ipc: Use base::MessageLoop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <sys/socket.h> 5 #include <sys/socket.h>
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/synchronization/waitable_event.h" 11 #include "base/synchronization/waitable_event.h"
12 #include "base/threading/thread.h" 12 #include "base/threading/thread.h"
13 #include "base/threading/thread_restrictions.h" 13 #include "base/threading/thread_restrictions.h"
14 #include "ipc/unix_domain_socket_util.h" 14 #include "ipc/unix_domain_socket_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 namespace { 17 namespace {
18 18
19 class SocketAcceptor : public MessageLoopForIO::Watcher { 19 class SocketAcceptor : public base::MessageLoopForIO::Watcher {
20 public: 20 public:
21 SocketAcceptor(int fd, base::MessageLoopProxy* target_thread) 21 SocketAcceptor(int fd, base::MessageLoopProxy* target_thread)
22 : server_fd_(-1), 22 : server_fd_(-1),
23 target_thread_(target_thread), 23 target_thread_(target_thread),
24 started_watching_event_(false, false), 24 started_watching_event_(false, false),
25 accepted_event_(false, false) { 25 accepted_event_(false, false) {
26 target_thread->PostTask(FROM_HERE, 26 target_thread->PostTask(FROM_HERE,
27 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd)); 27 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd));
28 } 28 }
29 29
(...skipping 14 matching lines...) Expand all
44 void Close() { 44 void Close() {
45 if (watcher_.get()) { 45 if (watcher_.get()) {
46 target_thread_->PostTask(FROM_HERE, 46 target_thread_->PostTask(FROM_HERE,
47 base::Bind(&SocketAcceptor::StopWatching, base::Unretained(this), 47 base::Bind(&SocketAcceptor::StopWatching, base::Unretained(this),
48 watcher_.release())); 48 watcher_.release()));
49 } 49 }
50 } 50 }
51 51
52 private: 52 private:
53 void StartWatching(int fd) { 53 void StartWatching(int fd) {
54 watcher_.reset(new MessageLoopForIO::FileDescriptorWatcher); 54 watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher);
55 MessageLoopForIO::current()->WatchFileDescriptor( 55 base::MessageLoopForIO::current()->WatchFileDescriptor(
56 fd, 56 fd, true, base::MessageLoopForIO::WATCH_READ, watcher_.get(), this);
57 true,
58 MessageLoopForIO::WATCH_READ,
59 watcher_.get(),
60 this);
61 started_watching_event_.Signal(); 57 started_watching_event_.Signal();
62 } 58 }
63 void StopWatching(MessageLoopForIO::FileDescriptorWatcher* watcher) { 59 void StopWatching(base::MessageLoopForIO::FileDescriptorWatcher* watcher) {
64 watcher->StopWatchingFileDescriptor(); 60 watcher->StopWatchingFileDescriptor();
65 delete watcher; 61 delete watcher;
66 } 62 }
67 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE { 63 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE {
68 ASSERT_EQ(-1, server_fd_); 64 ASSERT_EQ(-1, server_fd_);
69 IPC::ServerAcceptConnection(fd, &server_fd_); 65 IPC::ServerAcceptConnection(fd, &server_fd_);
70 watcher_->StopWatchingFileDescriptor(); 66 watcher_->StopWatchingFileDescriptor();
71 accepted_event_.Signal(); 67 accepted_event_.Signal();
72 } 68 }
73 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {} 69 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE {}
74 70
75 int server_fd_; 71 int server_fd_;
76 base::MessageLoopProxy* target_thread_; 72 base::MessageLoopProxy* target_thread_;
77 scoped_ptr<MessageLoopForIO::FileDescriptorWatcher> watcher_; 73 scoped_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_;
78 base::WaitableEvent started_watching_event_; 74 base::WaitableEvent started_watching_event_;
79 base::WaitableEvent accepted_event_; 75 base::WaitableEvent accepted_event_;
80 76
81 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor); 77 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor);
82 }; 78 };
83 79
84 const base::FilePath GetChannelDir() { 80 const base::FilePath GetChannelDir() {
85 #if defined(OS_ANDROID) 81 #if defined(OS_ANDROID)
86 base::FilePath tmp_dir; 82 base::FilePath tmp_dir;
87 PathService::Get(base::DIR_CACHE, &tmp_dir); 83 PathService::Get(base::DIR_CACHE, &tmp_dir);
88 return tmp_dir; 84 return tmp_dir;
89 #else 85 #else
90 return base::FilePath("/var/tmp"); 86 return base::FilePath("/var/tmp");
91 #endif 87 #endif
92 } 88 }
93 89
94 class TestUnixSocketConnection { 90 class TestUnixSocketConnection {
95 public: 91 public:
96 TestUnixSocketConnection() 92 TestUnixSocketConnection()
97 : worker_("WorkerThread"), 93 : worker_("WorkerThread"),
98 server_listen_fd_(-1), 94 server_listen_fd_(-1),
99 server_fd_(-1), 95 server_fd_(-1),
100 client_fd_(-1) { 96 client_fd_(-1) {
101 socket_name_ = GetChannelDir().Append("TestSocket"); 97 socket_name_ = GetChannelDir().Append("TestSocket");
102 base::Thread::Options options; 98 base::Thread::Options options;
103 options.message_loop_type = MessageLoop::TYPE_IO; 99 options.message_loop_type = base::MessageLoop::TYPE_IO;
104 worker_.StartWithOptions(options); 100 worker_.StartWithOptions(options);
105 } 101 }
106 102
107 bool CreateServerSocket() { 103 bool CreateServerSocket() {
108 IPC::CreateServerUnixDomainSocket(socket_name_, &server_listen_fd_); 104 IPC::CreateServerUnixDomainSocket(socket_name_, &server_listen_fd_);
109 if (server_listen_fd_ < 0) 105 if (server_listen_fd_ < 0)
110 return false; 106 return false;
111 struct stat socket_stat; 107 struct stat socket_stat;
112 stat(socket_name_.value().c_str(), &socket_stat); 108 stat(socket_name_.value().c_str(), &socket_stat);
113 EXPECT_TRUE(S_ISSOCK(socket_stat.st_mode)); 109 EXPECT_TRUE(S_ISSOCK(socket_stat.st_mode));
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0)); 166 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0));
171 ASSERT_EQ(buf_len, sent_bytes); 167 ASSERT_EQ(buf_len, sent_bytes);
172 char recv_buf[sizeof(buffer)]; 168 char recv_buf[sizeof(buffer)];
173 size_t received_bytes = 169 size_t received_bytes =
174 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0)); 170 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0));
175 ASSERT_EQ(buf_len, received_bytes); 171 ASSERT_EQ(buf_len, received_bytes);
176 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len)); 172 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len));
177 } 173 }
178 174
179 } // namespace 175 } // namespace
OLDNEW
« ipc/ipc_sync_channel_unittest.cc ('K') | « ipc/sync_socket_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698