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

Side by Side Diff: net/socket/socket_test_util.cc

Issue 2593063003: Add Socket::ReadIfReady() (Closed)
Patch Set: Self Created 3 years, 10 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 "net/socket/socket_test_util.h" 5 #include "net/socket/socket_test_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/callback_helpers.h"
13 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
14 #include "base/location.h" 15 #include "base/location.h"
15 #include "base/logging.h" 16 #include "base/logging.h"
16 #include "base/memory/ptr_util.h" 17 #include "base/memory/ptr_util.h"
17 #include "base/run_loop.h" 18 #include "base/run_loop.h"
18 #include "base/single_thread_task_runner.h" 19 #include "base/single_thread_task_runner.h"
19 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/time/time.h" 21 #include "base/time/time.h"
21 #include "net/base/address_family.h" 22 #include "net/base/address_family.h"
22 #include "net/base/address_list.h" 23 #include "net/base/address_list.h"
(...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 693
693 NET_TRACE(1, " *** ") << " Completing socket write for: " 694 NET_TRACE(1, " *** ") << " Completing socket write for: "
694 << data.sequence_number; 695 << data.sequence_number;
695 socket()->OnWriteComplete(rv); 696 socket()->OnWriteComplete(rv);
696 NET_TRACE(1, " *** ") << "Done"; 697 NET_TRACE(1, " *** ") << "Done";
697 } 698 }
698 699
699 SequencedSocketData::~SequencedSocketData() { 700 SequencedSocketData::~SequencedSocketData() {
700 } 701 }
701 702
702 MockClientSocketFactory::MockClientSocketFactory() {} 703 MockClientSocketFactory::MockClientSocketFactory()
704 : enable_read_if_ready_(false) {}
703 705
704 MockClientSocketFactory::~MockClientSocketFactory() {} 706 MockClientSocketFactory::~MockClientSocketFactory() {}
705 707
706 void MockClientSocketFactory::AddSocketDataProvider( 708 void MockClientSocketFactory::AddSocketDataProvider(
707 SocketDataProvider* data) { 709 SocketDataProvider* data) {
708 mock_data_.Add(data); 710 mock_data_.Add(data);
709 } 711 }
710 712
711 void MockClientSocketFactory::AddSSLSocketDataProvider( 713 void MockClientSocketFactory::AddSSLSocketDataProvider(
712 SSLSocketDataProvider* data) { 714 SSLSocketDataProvider* data) {
(...skipping 23 matching lines...) Expand all
736 738
737 std::unique_ptr<StreamSocket> 739 std::unique_ptr<StreamSocket>
738 MockClientSocketFactory::CreateTransportClientSocket( 740 MockClientSocketFactory::CreateTransportClientSocket(
739 const AddressList& addresses, 741 const AddressList& addresses,
740 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, 742 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
741 NetLog* net_log, 743 NetLog* net_log,
742 const NetLogSource& source) { 744 const NetLogSource& source) {
743 SocketDataProvider* data_provider = mock_data_.GetNext(); 745 SocketDataProvider* data_provider = mock_data_.GetNext();
744 std::unique_ptr<MockTCPClientSocket> socket( 746 std::unique_ptr<MockTCPClientSocket> socket(
745 new MockTCPClientSocket(addresses, net_log, data_provider)); 747 new MockTCPClientSocket(addresses, net_log, data_provider));
748 if (enable_read_if_ready_)
749 socket->set_enable_read_if_ready(enable_read_if_ready_);
746 return std::move(socket); 750 return std::move(socket);
747 } 751 }
748 752
749 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( 753 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket(
750 std::unique_ptr<ClientSocketHandle> transport_socket, 754 std::unique_ptr<ClientSocketHandle> transport_socket,
751 const HostPortPair& host_and_port, 755 const HostPortPair& host_and_port,
752 const SSLConfig& ssl_config, 756 const SSLConfig& ssl_config,
753 const SSLClientSocketContext& context) { 757 const SSLClientSocketContext& context) {
754 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); 758 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext();
755 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { 759 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) {
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 SocketDataProvider* data) 879 SocketDataProvider* data)
876 : MockClientSocket(NetLogWithSource::Make(net_log, NetLogSourceType::NONE)), 880 : MockClientSocket(NetLogWithSource::Make(net_log, NetLogSourceType::NONE)),
877 addresses_(addresses), 881 addresses_(addresses),
878 data_(data), 882 data_(data),
879 read_offset_(0), 883 read_offset_(0),
880 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), 884 read_data_(SYNCHRONOUS, ERR_UNEXPECTED),
881 need_read_data_(true), 885 need_read_data_(true),
882 peer_closed_connection_(false), 886 peer_closed_connection_(false),
883 pending_read_buf_(NULL), 887 pending_read_buf_(NULL),
884 pending_read_buf_len_(0), 888 pending_read_buf_len_(0),
885 was_used_to_convey_data_(false) { 889 was_used_to_convey_data_(false),
890 enable_read_if_ready_(false),
891 weak_factory_(this) {
886 DCHECK(data_); 892 DCHECK(data_);
887 peer_addr_ = data->connect_data().peer_addr; 893 peer_addr_ = data->connect_data().peer_addr;
888 data_->Initialize(this); 894 data_->Initialize(this);
889 } 895 }
890 896
891 MockTCPClientSocket::~MockTCPClientSocket() { 897 MockTCPClientSocket::~MockTCPClientSocket() {
892 if (data_) 898 if (data_)
893 data_->DetachSocket(); 899 data_->DetachSocket();
894 } 900 }
895 901
896 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, 902 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len,
897 const CompletionCallback& callback) { 903 const CompletionCallback& callback) {
898 if (!connected_ || !data_)
899 return ERR_UNEXPECTED;
900
901 // If the buffer is already in use, a read is already in progress! 904 // If the buffer is already in use, a read is already in progress!
902 DCHECK(!pending_read_buf_); 905 DCHECK(!pending_read_buf_);
903 906
904 // Store our async IO data. 907 int rv = ReadIfReadyHelper(
905 pending_read_buf_ = buf; 908 buf, buf_len,
906 pending_read_buf_len_ = buf_len; 909 base::Bind(&MockTCPClientSocket::RetryRead, weak_factory_.GetWeakPtr()));
davidben 2017/02/10 23:33:48 Ditto re whether you actually need the WeakPtr.
xunjieli 2017/02/13 20:28:18 Done.
907 pending_read_callback_ = callback; 910 if (rv == ERR_IO_PENDING) {
911 pending_read_buf_ = buf;
912 pending_read_buf_len_ = buf_len;
913 pending_read_callback_ = callback;
914 }
915 return rv;
916 }
908 917
909 if (need_read_data_) { 918 int MockTCPClientSocket::ReadIfReady(IOBuffer* buf,
910 read_data_ = data_->OnRead(); 919 int buf_len,
911 if (read_data_.result == ERR_CONNECTION_CLOSED) { 920 const CompletionCallback& callback) {
912 // This MockRead is just a marker to instruct us to set 921 DCHECK(!pending_read_if_ready_callback_);
913 // peer_closed_connection_.
914 peer_closed_connection_ = true;
915 }
916 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) {
917 // This MockRead is just a marker to instruct us to set
918 // peer_closed_connection_. Skip it and get the next one.
919 read_data_ = data_->OnRead();
920 peer_closed_connection_ = true;
921 }
922 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility
923 // to complete the async IO manually later (via OnReadComplete).
924 if (read_data_.result == ERR_IO_PENDING) {
925 // We need to be using async IO in this case.
926 DCHECK(!callback.is_null());
927 return ERR_IO_PENDING;
928 }
929 need_read_data_ = false;
930 }
931 922
932 return CompleteRead(); 923 if (!enable_read_if_ready_)
924 return ERR_READ_IF_READY_NOT_IMPLEMENTED;
925 return ReadIfReadyHelper(buf, buf_len, callback);
933 } 926 }
934 927
935 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, 928 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len,
936 const CompletionCallback& callback) { 929 const CompletionCallback& callback) {
937 DCHECK(buf); 930 DCHECK(buf);
938 DCHECK_GT(buf_len, 0); 931 DCHECK_GT(buf_len, 0);
939 932
940 if (!connected_ || !data_) 933 if (!connected_ || !data_)
941 return ERR_UNEXPECTED; 934 return ERR_UNEXPECTED;
942 935
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1041 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { 1034 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
1042 return false; 1035 return false;
1043 } 1036 }
1044 1037
1045 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { 1038 void MockTCPClientSocket::OnReadComplete(const MockRead& data) {
1046 // If |data_| has been destroyed, safest to just do nothing. 1039 // If |data_| has been destroyed, safest to just do nothing.
1047 if (!data_) 1040 if (!data_)
1048 return; 1041 return;
1049 1042
1050 // There must be a read pending. 1043 // There must be a read pending.
1051 DCHECK(pending_read_buf_.get()); 1044 DCHECK(pending_read_if_ready_callback_);
1052 // You can't complete a read with another ERR_IO_PENDING status code. 1045 // You can't complete a read with another ERR_IO_PENDING status code.
1053 DCHECK_NE(ERR_IO_PENDING, data.result); 1046 DCHECK_NE(ERR_IO_PENDING, data.result);
1054 // Since we've been waiting for data, need_read_data_ should be true. 1047 // Since we've been waiting for data, need_read_data_ should be true.
1055 DCHECK(need_read_data_); 1048 DCHECK(need_read_data_);
1056 1049
1057 read_data_ = data; 1050 read_data_ = data;
1058 need_read_data_ = false; 1051 need_read_data_ = false;
1059 1052
1060 // The caller is simulating that this IO completes right now. Don't 1053 // The caller is simulating that this IO completes right now. Don't
1061 // let CompleteRead() schedule a callback. 1054 // let CompleteRead() schedule a callback.
1062 read_data_.mode = SYNCHRONOUS; 1055 read_data_.mode = SYNCHRONOUS;
1063 1056 RunReadIfReadyCallback(read_data_.result > 0 ? OK : read_data_.result);
1064 CompletionCallback callback = pending_read_callback_;
1065 int rv = CompleteRead();
1066 RunCallback(callback, rv);
1067 } 1057 }
1068 1058
1069 void MockTCPClientSocket::OnWriteComplete(int rv) { 1059 void MockTCPClientSocket::OnWriteComplete(int rv) {
1070 // If |data_| has been destroyed, safest to just do nothing. 1060 // If |data_| has been destroyed, safest to just do nothing.
1071 if (!data_) 1061 if (!data_)
1072 return; 1062 return;
1073 1063
1074 // There must be a read pending. 1064 // There must be a read pending.
1075 DCHECK(!pending_write_callback_.is_null()); 1065 DCHECK(!pending_write_callback_.is_null());
1076 CompletionCallback callback = pending_write_callback_; 1066 CompletionCallback callback = pending_write_callback_;
1077 RunCallback(callback, rv); 1067 RunCallback(callback, rv);
1078 } 1068 }
1079 1069
1080 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) { 1070 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) {
1081 // If |data_| has been destroyed, safest to just do nothing. 1071 // If |data_| has been destroyed, safest to just do nothing.
1082 if (!data_) 1072 if (!data_)
1083 return; 1073 return;
1084 1074
1085 CompletionCallback callback = pending_connect_callback_; 1075 CompletionCallback callback = pending_connect_callback_;
1086 RunCallback(callback, data.result); 1076 RunCallback(callback, data.result);
1087 } 1077 }
1088 1078
1089 void MockTCPClientSocket::OnDataProviderDestroyed() { 1079 void MockTCPClientSocket::OnDataProviderDestroyed() {
1090 data_ = nullptr; 1080 data_ = nullptr;
1091 } 1081 }
1092 1082
1093 int MockTCPClientSocket::CompleteRead() { 1083 void MockTCPClientSocket::RetryRead(int rv) {
1084 DCHECK(pending_read_callback_);
1094 DCHECK(pending_read_buf_.get()); 1085 DCHECK(pending_read_buf_.get());
1095 DCHECK(pending_read_buf_len_ > 0); 1086 DCHECK_LT(0, pending_read_buf_len_);
1087
1088 if (rv == OK) {
1089 rv = ReadIfReadyHelper(pending_read_buf_.get(), pending_read_buf_len_,
1090 base::Bind(&MockTCPClientSocket::RetryRead,
1091 weak_factory_.GetWeakPtr()));
1092 if (rv == ERR_IO_PENDING)
1093 return;
1094 }
1095 pending_read_buf_ = nullptr;
1096 pending_read_buf_len_ = 0;
1097 base::ResetAndReturn(&pending_read_callback_).Run(rv);
1098 }
1099
1100 int MockTCPClientSocket::ReadIfReadyHelper(IOBuffer* buf,
1101 int buf_len,
1102 const CompletionCallback& callback) {
1103 if (!connected_ || !data_)
1104 return ERR_UNEXPECTED;
1105
1106 DCHECK(!pending_read_if_ready_callback_);
1107
1108 if (need_read_data_) {
1109 read_data_ = data_->OnRead();
1110 if (read_data_.result == ERR_CONNECTION_CLOSED) {
1111 // This MockRead is just a marker to instruct us to set
1112 // peer_closed_connection_.
1113 peer_closed_connection_ = true;
1114 }
1115 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) {
1116 // This MockRead is just a marker to instruct us to set
1117 // peer_closed_connection_. Skip it and get the next one.
1118 read_data_ = data_->OnRead();
1119 peer_closed_connection_ = true;
1120 }
1121 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility
1122 // to complete the async IO manually later (via OnReadComplete).
davidben 2017/02/10 23:33:48 Hrm. Does this feature work for ReadIfReady? (I do
xunjieli 2017/02/13 20:28:18 I believe it does, but I might be missing somethin
1123 if (read_data_.result == ERR_IO_PENDING) {
1124 // We need to be using async IO in this case.
1125 DCHECK(!callback.is_null());
1126 pending_read_if_ready_callback_ = callback;
1127 return ERR_IO_PENDING;
1128 }
1129 need_read_data_ = false;
1130 }
1096 1131
1097 was_used_to_convey_data_ = true; 1132 was_used_to_convey_data_ = true;
1098 1133
1099 // Save the pending async IO data and reset our |pending_| state.
1100 scoped_refptr<IOBuffer> buf = pending_read_buf_;
1101 int buf_len = pending_read_buf_len_;
1102 CompletionCallback callback = pending_read_callback_;
1103 pending_read_buf_ = NULL;
1104 pending_read_buf_len_ = 0;
1105 pending_read_callback_.Reset();
1106
1107 int result = read_data_.result; 1134 int result = read_data_.result;
1108 DCHECK(result != ERR_IO_PENDING); 1135 DCHECK_NE(ERR_IO_PENDING, result);
1109 1136
1110 if (read_data_.data) { 1137 if (read_data_.data) {
1111 if (read_data_.data_len - read_offset_ > 0) { 1138 if (read_data_.data_len - read_offset_ > 0) {
1112 result = std::min(buf_len, read_data_.data_len - read_offset_); 1139 result = std::min(buf_len, read_data_.data_len - read_offset_);
davidben 2017/02/10 23:33:48 If you've got some data in here and read_data_.mod
xunjieli 2017/02/13 20:28:18 Done. Ah, good catch!
1113 memcpy(buf->data(), read_data_.data + read_offset_, result); 1140 memcpy(buf->data(), read_data_.data + read_offset_, result);
1114 read_offset_ += result; 1141 read_offset_ += result;
1115 if (read_offset_ == read_data_.data_len) { 1142 if (read_offset_ == read_data_.data_len) {
1116 need_read_data_ = true; 1143 need_read_data_ = true;
1117 read_offset_ = 0; 1144 read_offset_ = 0;
1118 } 1145 }
1119 } else { 1146 } else {
1120 result = 0; // EOF 1147 result = 0; // EOF
1121 } 1148 }
1122 } 1149 }
1123 1150
1124 if (read_data_.mode == ASYNC) { 1151 if (read_data_.mode == ASYNC) {
1125 DCHECK(!callback.is_null()); 1152 DCHECK(!callback.is_null());
1126 RunCallbackAsync(callback, result); 1153 pending_read_if_ready_callback_ = callback;
1154 read_data_.mode = SYNCHRONOUS;
1155 base::ThreadTaskRunnerHandle::Get()->PostTask(
1156 FROM_HERE, base::Bind(&MockTCPClientSocket::RunReadIfReadyCallback,
1157 weak_factory_.GetWeakPtr(), result));
davidben 2017/02/10 23:33:48 Does RunCallbackAsync(callback, result) not work?
xunjieli 2017/02/13 20:28:18 Done.
1127 return ERR_IO_PENDING; 1158 return ERR_IO_PENDING;
1128 } 1159 }
1129 return result; 1160 return result;
1130 } 1161 }
1131 1162
1163 void MockTCPClientSocket::RunReadIfReadyCallback(int result) {
1164 DCHECK(pending_read_if_ready_callback_);
1165 base::ResetAndReturn(&pending_read_if_ready_callback_).Run(result);
1166 }
1167
1132 // static 1168 // static
1133 void MockSSLClientSocket::ConnectCallback( 1169 void MockSSLClientSocket::ConnectCallback(
1134 MockSSLClientSocket* ssl_client_socket, 1170 MockSSLClientSocket* ssl_client_socket,
1135 const CompletionCallback& callback, 1171 const CompletionCallback& callback,
1136 int rv) { 1172 int rv) {
1137 if (rv == OK) 1173 if (rv == OK)
1138 ssl_client_socket->connected_ = true; 1174 ssl_client_socket->connected_ = true;
1139 callback.Run(rv); 1175 callback.Run(rv);
1140 } 1176 }
1141 1177
(...skipping 15 matching lines...) Expand all
1157 1193
1158 MockSSLClientSocket::~MockSSLClientSocket() { 1194 MockSSLClientSocket::~MockSSLClientSocket() {
1159 Disconnect(); 1195 Disconnect();
1160 } 1196 }
1161 1197
1162 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, 1198 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len,
1163 const CompletionCallback& callback) { 1199 const CompletionCallback& callback) {
1164 return transport_->socket()->Read(buf, buf_len, callback); 1200 return transport_->socket()->Read(buf, buf_len, callback);
1165 } 1201 }
1166 1202
1203 int MockSSLClientSocket::ReadIfReady(IOBuffer* buf,
1204 int buf_len,
1205 const CompletionCallback& callback) {
1206 return transport_->socket()->ReadIfReady(buf, buf_len, callback);
1207 }
1208
1167 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, 1209 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len,
1168 const CompletionCallback& callback) { 1210 const CompletionCallback& callback) {
1169 return transport_->socket()->Write(buf, buf_len, callback); 1211 return transport_->socket()->Write(buf, buf_len, callback);
1170 } 1212 }
1171 1213
1172 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { 1214 int MockSSLClientSocket::Connect(const CompletionCallback& callback) {
1173 int rv = transport_->socket()->Connect( 1215 int rv = transport_->socket()->Connect(
1174 base::Bind(&ConnectCallback, base::Unretained(this), callback)); 1216 base::Bind(&ConnectCallback, base::Unretained(this), callback));
1175 if (rv == OK) { 1217 if (rv == OK) {
1176 if (data_->connect.result == OK) 1218 if (data_->connect.result == OK)
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
1744 } 1786 }
1745 1787
1746 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { 1788 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) {
1747 int64_t total = 0; 1789 int64_t total = 0;
1748 for (const MockWrite* write = writes; write != writes + writes_size; ++write) 1790 for (const MockWrite* write = writes; write != writes + writes_size; ++write)
1749 total += write->data_len; 1791 total += write->data_len;
1750 return total; 1792 return total;
1751 } 1793 }
1752 1794
1753 } // namespace net 1795 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698