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

Side by Side Diff: chrome/browser/extensions/api/socket/tcp_socket_unittest.cc

Issue 10310170: Improve socket.write() to make sure all data will be sent out. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fix compile error on Mac Created 8 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
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 "chrome/browser/extensions/api/socket/tcp_socket.h" 5 #include "chrome/browser/extensions/api/socket/tcp_socket.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h" 8 #include "chrome/browser/extensions/api/api_resource_event_notifier.h"
9 #include "net/base/address_list.h" 9 #include "net/base/address_list.h"
10 #include "net/base/completion_callback.h" 10 #include "net/base/completion_callback.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 85
86 TEST(SocketTest, TestTCPSocketWrite) { 86 TEST(SocketTest, TestTCPSocketWrite) {
87 net::AddressList address_list; 87 net::AddressList address_list;
88 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); 88 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
89 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 89 APIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
90 CompleteHandler handler; 90 CompleteHandler handler;
91 91
92 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting( 92 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
93 tcp_client_socket, notifier)); 93 tcp_client_socket, notifier));
94 94
95 net::CompletionCallback callback;
95 EXPECT_CALL(*tcp_client_socket, Write(_, _, _)) 96 EXPECT_CALL(*tcp_client_socket, Write(_, _, _))
96 .Times(1); 97 .Times(2)
98 .WillRepeatedly(testing::DoAll(SaveArg<2>(&callback),
99 Return(128)));
97 EXPECT_CALL(handler, OnComplete(_)) 100 EXPECT_CALL(handler, OnComplete(_))
98 .Times(1); 101 .Times(1);
99 102
100 scoped_refptr<net::IOBufferWithSize> io_buffer( 103 scoped_refptr<net::IOBufferWithSize> io_buffer(
101 new net::IOBufferWithSize(256)); 104 new net::IOBufferWithSize(256));
102 socket->Write(io_buffer.get(), io_buffer->size(), 105 socket->Write(io_buffer.get(), io_buffer->size(),
103 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler))); 106 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
104 } 107 }
105 108
106 TEST(SocketTest, TestTCPSocketBlockedWrite) { 109 TEST(SocketTest, TestTCPSocketBlockedWrite) {
107 net::AddressList address_list; 110 net::AddressList address_list;
108 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list); 111 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
109 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier(); 112 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
110 CompleteHandler handler; 113 CompleteHandler handler;
111 114
112 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting( 115 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
113 tcp_client_socket, notifier)); 116 tcp_client_socket, notifier));
114 117
115 net::CompletionCallback callback; 118 net::CompletionCallback callback;
116 EXPECT_CALL(*tcp_client_socket, Write(_, _, _)) 119 EXPECT_CALL(*tcp_client_socket, Write(_, _, _))
117 .Times(1) 120 .Times(2)
118 .WillOnce(testing::DoAll(SaveArg<2>(&callback), 121 .WillRepeatedly(testing::DoAll(SaveArg<2>(&callback),
119 Return(net::ERR_IO_PENDING))); 122 Return(net::ERR_IO_PENDING)));
120 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize( 123 scoped_refptr<net::IOBufferWithSize> io_buffer(new net::IOBufferWithSize(42));
121 1));
122 socket->Write(io_buffer.get(), io_buffer->size(), 124 socket->Write(io_buffer.get(), io_buffer->size(),
123 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler))); 125 base::Bind(&CompleteHandler::OnComplete, base::Unretained(&handler)));
124 126
125 // Good. Original call came back unable to complete. Now pretend the socket 127 // Good. Original call came back unable to complete. Now pretend the socket
126 // finished, and confirm that we passed the error back. 128 // finished, and confirm that we passed the error back.
127 EXPECT_CALL(handler, OnComplete(42)) 129 EXPECT_CALL(handler, OnComplete(42))
128 .Times(1); 130 .Times(1);
129 callback.Run(42); 131 callback.Run(40);
132 callback.Run(2);
133 }
134
135 TEST(SocketTest, TestTCPSocketBlockedWriteReentry) {
136 net::AddressList address_list;
137 MockTCPSocket* tcp_client_socket = new MockTCPSocket(address_list);
138 MockAPIResourceEventNotifier* notifier = new MockAPIResourceEventNotifier();
139 CompleteHandler handlers[5];
140
141 scoped_ptr<TCPSocket> socket(TCPSocket::CreateSocketForTesting(
142 tcp_client_socket, notifier));
143
144 net::CompletionCallback callback;
145 EXPECT_CALL(*tcp_client_socket, Write(_, _, _))
146 .Times(5)
147 .WillRepeatedly(testing::DoAll(SaveArg<2>(&callback),
148 Return(net::ERR_IO_PENDING)));
149 scoped_refptr<net::IOBufferWithSize> io_buffers[5];
150 int i;
151 for (i = 0; i < 5; i++) {
152 io_buffers[i] = new net::IOBufferWithSize(128 + i * 50);
153 scoped_refptr<net::IOBufferWithSize> io_buffer1(
154 new net::IOBufferWithSize(42));
155 socket->Write(io_buffers[i].get(), io_buffers[i]->size(),
156 base::Bind(&CompleteHandler::OnComplete,
157 base::Unretained(&handlers[i])));
158
159 EXPECT_CALL(handlers[i], OnComplete(io_buffers[i]->size()))
160 .Times(1);
161 }
162
163 for (i = 0; i < 5; i++) {
164 callback.Run(128 + i * 50);
165 }
130 } 166 }
131 167
132 } // namespace extensions 168 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/socket/tcp_socket.cc ('k') | chrome/browser/extensions/api/socket/udp_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698