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

Side by Side Diff: media/audio/async_socket_io_handler_unittest.cc

Issue 10697069: Move the callback out of the Read method and into Initialize to make Read loops simpler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix expectations for the synchronous test Created 8 years, 5 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/audio/async_socket_io_handler.h" 5 #include "media/audio/async_socket_io_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace { 10 namespace {
11 const char kAsyncSocketIoTestString[] = "Hello, AsyncSocketIoHandler"; 11 const char kAsyncSocketIoTestString[] = "Hello, AsyncSocketIoHandler";
12 const size_t kAsyncSocketIoTestStringLength = 12 const size_t kAsyncSocketIoTestStringLength =
13 arraysize(kAsyncSocketIoTestString); 13 arraysize(kAsyncSocketIoTestString);
14 14
15 class TestSocketReader { 15 class TestSocketReader {
16 public: 16 public:
17 TestSocketReader(base::CancelableSyncSocket* socket, bool quit_on_read) 17 // Set |number_of_reads_before_quit| to >0 when you expect a specific number
18 : socket_(socket), buffer_(), quit_on_read_(quit_on_read) { 18 // of Read operations to complete. Once that number is reached, the current
19 io_handler.Initialize(socket_->handle()); 19 // message loop will be Quit(). Set |number_of_reads_before_quit| to -1 if
20 // callbacks should not be counted.
21 TestSocketReader(base::CancelableSyncSocket* socket,
22 int number_of_reads_before_quit,
23 bool issue_reads_from_callback)
24 : socket_(socket), buffer_(),
25 number_of_reads_before_quit_(number_of_reads_before_quit),
26 callbacks_received_(0),
27 issue_reads_from_callback_(issue_reads_from_callback) {
28 io_handler.Initialize(socket_->handle(),
29 base::Bind(&TestSocketReader::OnRead,
30 base::Unretained(this)));
20 } 31 }
21 ~TestSocketReader() {} 32 ~TestSocketReader() {}
22 33
23 bool IssueRead() { 34 bool IssueRead() {
24 return io_handler.Read(&buffer_[0], sizeof(buffer_), 35 return io_handler.Read(&buffer_[0], sizeof(buffer_));
25 base::Bind(&TestSocketReader::OnRead,
26 base::Unretained(this)));
27 } 36 }
28 37
29 const char* buffer() const { return &buffer_[0]; } 38 const char* buffer() const { return &buffer_[0]; }
30 39
40 int callbacks_received() const { return callbacks_received_; }
41
31 private: 42 private:
32 void OnRead(int bytes_read) { 43 void OnRead(int bytes_read) {
33 EXPECT_GT(bytes_read, 0); 44 EXPECT_GT(bytes_read, 0);
34 if (quit_on_read_) 45 ++callbacks_received_;
46 if (number_of_reads_before_quit_ == callbacks_received_) {
35 MessageLoop::current()->Quit(); 47 MessageLoop::current()->Quit();
48 } else if (issue_reads_from_callback_) {
49 IssueRead();
50 }
36 } 51 }
37 52
38 media::AsyncSocketIoHandler io_handler; 53 media::AsyncSocketIoHandler io_handler;
39 base::CancelableSyncSocket* socket_; // Ownership lies outside the class. 54 base::CancelableSyncSocket* socket_; // Ownership lies outside the class.
40 char buffer_[kAsyncSocketIoTestStringLength]; 55 char buffer_[kAsyncSocketIoTestStringLength];
41 bool quit_on_read_; 56 int number_of_reads_before_quit_;
57 int callbacks_received_;
58 bool issue_reads_from_callback_;
42 }; 59 };
43 60
61 // Workaround to be able to use a base::Closure for sending data.
62 // Send() returns int but a closure must return void.
63 void SendData(base::CancelableSyncSocket* socket,
64 const void* buffer,
65 size_t length) {
66 socket->Send(buffer, length);
67 }
68
44 } // end namespace. 69 } // end namespace.
45 70
46 // Tests doing a pending read from a socket and use an IO handler to get 71 // Tests doing a pending read from a socket and use an IO handler to get
47 // notified of data. 72 // notified of data.
48 TEST(AsyncSocketIoHandlerTest, AsynchronousReadWithMessageLoop) { 73 TEST(AsyncSocketIoHandlerTest, AsynchronousReadWithMessageLoop) {
49 MessageLoopForIO loop; 74 MessageLoopForIO loop;
50 75
51 base::CancelableSyncSocket pair[2]; 76 base::CancelableSyncSocket pair[2];
52 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); 77 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
53 78
54 TestSocketReader reader(&pair[0], true); 79 TestSocketReader reader(&pair[0], 1, false);
55 EXPECT_TRUE(reader.IssueRead()); 80 EXPECT_TRUE(reader.IssueRead());
56 81
57 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength); 82 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength);
58 MessageLoop::current()->Run(); 83 MessageLoop::current()->Run();
59 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0); 84 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0);
85 EXPECT_EQ(1, reader.callbacks_received());
60 } 86 }
61 87
62 // Tests doing a read from a socket when we know that there is data in the 88 // Tests doing a read from a socket when we know that there is data in the
63 // socket. Here we want to make sure that any async 'can read' notifications 89 // socket. Here we want to make sure that any async 'can read' notifications
64 // won't trip us off and that the synchronous case works as well. 90 // won't trip us off and that the synchronous case works as well.
65 TEST(AsyncSocketIoHandlerTest, SynchronousReadWithMessageLoop) { 91 TEST(AsyncSocketIoHandlerTest, SynchronousReadWithMessageLoop) {
66 MessageLoopForIO loop; 92 MessageLoopForIO loop;
67 93
68 base::CancelableSyncSocket pair[2]; 94 base::CancelableSyncSocket pair[2];
69 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1])); 95 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
70 96
71 TestSocketReader reader(&pair[0], false); 97 TestSocketReader reader(&pair[0], -1, false);
72 98
73 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength); 99 pair[1].Send(kAsyncSocketIoTestString, kAsyncSocketIoTestStringLength);
74 MessageLoop::current()->PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(), 100 MessageLoop::current()->PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(),
75 base::TimeDelta::FromMilliseconds(100)); 101 base::TimeDelta::FromMilliseconds(100));
76 MessageLoop::current()->Run(); 102 MessageLoop::current()->Run();
77 103
78 EXPECT_TRUE(reader.IssueRead()); 104 EXPECT_TRUE(reader.IssueRead());
79 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0); 105 EXPECT_EQ(strcmp(reader.buffer(), kAsyncSocketIoTestString), 0);
106 // We've now verified that the read happened synchronously, but it's not
107 // guaranteed that the callback has been issued since the callback will be
108 // called asynchronously even though the read may have been done.
109 // So we call RunAllPending() to allow any event notifications or APC's on
110 // Windows, to execute before checking the count of how many callbacks we've
111 // received.
112 MessageLoop::current()->RunAllPending();
113 EXPECT_EQ(1, reader.callbacks_received());
80 } 114 }
115
116 // Calls Read() from within a callback to test that simple read "loops" work.
117 TEST(AsyncSocketIoHandlerTest, ReadFromCallback) {
118 MessageLoopForIO loop;
119
120 base::CancelableSyncSocket pair[2];
121 ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
122
123 const int kReadOperationCount = 10;
124 TestSocketReader reader(&pair[0], kReadOperationCount, true);
125 EXPECT_TRUE(reader.IssueRead());
126
127 // Issue sends on an interval to satisfy the Read() requirements.
128 int64 milliseconds = 0;
129 for (int i = 0; i < kReadOperationCount; ++i) {
130 MessageLoop::current()->PostDelayedTask(FROM_HERE,
131 base::Bind(&SendData, &pair[1], kAsyncSocketIoTestString,
132 kAsyncSocketIoTestStringLength),
133 base::TimeDelta::FromMilliseconds(milliseconds));
134 milliseconds += 10;
135 }
136
137 MessageLoop::current()->PostDelayedTask(FROM_HERE, MessageLoop::QuitClosure(),
138 base::TimeDelta::FromMilliseconds(100 + milliseconds));
139
140 MessageLoop::current()->Run();
141 EXPECT_EQ(kReadOperationCount, reader.callbacks_received());
142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698