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

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

Issue 2593063003: Add Socket::ReadIfReady() (Closed)
Patch Set: Fix perf tests (removed invalid CHECKs) 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
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after
692 692
693 NET_TRACE(1, " *** ") << " Completing socket write for: " 693 NET_TRACE(1, " *** ") << " Completing socket write for: "
694 << data.sequence_number; 694 << data.sequence_number;
695 socket()->OnWriteComplete(rv); 695 socket()->OnWriteComplete(rv);
696 NET_TRACE(1, " *** ") << "Done"; 696 NET_TRACE(1, " *** ") << "Done";
697 } 697 }
698 698
699 SequencedSocketData::~SequencedSocketData() { 699 SequencedSocketData::~SequencedSocketData() {
700 } 700 }
701 701
702 MockClientSocketFactory::MockClientSocketFactory() {} 702 MockClientSocketFactory::MockClientSocketFactory()
703 : enable_read_if_ready_(false) {}
703 704
704 MockClientSocketFactory::~MockClientSocketFactory() {} 705 MockClientSocketFactory::~MockClientSocketFactory() {}
705 706
706 void MockClientSocketFactory::AddSocketDataProvider( 707 void MockClientSocketFactory::AddSocketDataProvider(
707 SocketDataProvider* data) { 708 SocketDataProvider* data) {
708 mock_data_.Add(data); 709 mock_data_.Add(data);
709 } 710 }
710 711
711 void MockClientSocketFactory::AddSSLSocketDataProvider( 712 void MockClientSocketFactory::AddSSLSocketDataProvider(
712 SSLSocketDataProvider* data) { 713 SSLSocketDataProvider* data) {
(...skipping 23 matching lines...) Expand all
736 737
737 std::unique_ptr<StreamSocket> 738 std::unique_ptr<StreamSocket>
738 MockClientSocketFactory::CreateTransportClientSocket( 739 MockClientSocketFactory::CreateTransportClientSocket(
739 const AddressList& addresses, 740 const AddressList& addresses,
740 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher, 741 std::unique_ptr<SocketPerformanceWatcher> socket_performance_watcher,
741 NetLog* net_log, 742 NetLog* net_log,
742 const NetLogSource& source) { 743 const NetLogSource& source) {
743 SocketDataProvider* data_provider = mock_data_.GetNext(); 744 SocketDataProvider* data_provider = mock_data_.GetNext();
744 std::unique_ptr<MockTCPClientSocket> socket( 745 std::unique_ptr<MockTCPClientSocket> socket(
745 new MockTCPClientSocket(addresses, net_log, data_provider)); 746 new MockTCPClientSocket(addresses, net_log, data_provider));
747 if (enable_read_if_ready_)
748 socket->set_enable_read_if_ready(enable_read_if_ready_);
746 return std::move(socket); 749 return std::move(socket);
747 } 750 }
748 751
749 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket( 752 std::unique_ptr<SSLClientSocket> MockClientSocketFactory::CreateSSLClientSocket(
750 std::unique_ptr<ClientSocketHandle> transport_socket, 753 std::unique_ptr<ClientSocketHandle> transport_socket,
751 const HostPortPair& host_and_port, 754 const HostPortPair& host_and_port,
752 const SSLConfig& ssl_config, 755 const SSLConfig& ssl_config,
753 const SSLClientSocketContext& context) { 756 const SSLClientSocketContext& context) {
754 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext(); 757 SSLSocketDataProvider* next_ssl_data = mock_ssl_data_.GetNext();
755 if (!next_ssl_data->next_protos_expected_in_ssl_config.empty()) { 758 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) 878 SocketDataProvider* data)
876 : MockClientSocket(NetLogWithSource::Make(net_log, NetLogSourceType::NONE)), 879 : MockClientSocket(NetLogWithSource::Make(net_log, NetLogSourceType::NONE)),
877 addresses_(addresses), 880 addresses_(addresses),
878 data_(data), 881 data_(data),
879 read_offset_(0), 882 read_offset_(0),
880 read_data_(SYNCHRONOUS, ERR_UNEXPECTED), 883 read_data_(SYNCHRONOUS, ERR_UNEXPECTED),
881 need_read_data_(true), 884 need_read_data_(true),
882 peer_closed_connection_(false), 885 peer_closed_connection_(false),
883 pending_read_buf_(NULL), 886 pending_read_buf_(NULL),
884 pending_read_buf_len_(0), 887 pending_read_buf_len_(0),
885 was_used_to_convey_data_(false) { 888 was_used_to_convey_data_(false),
889 enable_read_if_ready_(false) {
886 DCHECK(data_); 890 DCHECK(data_);
887 peer_addr_ = data->connect_data().peer_addr; 891 peer_addr_ = data->connect_data().peer_addr;
888 data_->Initialize(this); 892 data_->Initialize(this);
889 } 893 }
890 894
891 MockTCPClientSocket::~MockTCPClientSocket() { 895 MockTCPClientSocket::~MockTCPClientSocket() {
892 if (data_) 896 if (data_)
893 data_->DetachSocket(); 897 data_->DetachSocket();
894 } 898 }
895 899
896 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len, 900 int MockTCPClientSocket::Read(IOBuffer* buf, int buf_len,
897 const CompletionCallback& callback) { 901 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! 902 // If the buffer is already in use, a read is already in progress!
902 DCHECK(!pending_read_buf_); 903 DCHECK(!pending_read_buf_);
904 // Use base::Unretained() is safe because whenever callback is run
905 // asynchrously, it takes the weak ptr of the base class.
906 int rv = ReadIfReadyHelper(
907 buf, buf_len,
908 base::Bind(&MockTCPClientSocket::RetryRead, base::Unretained(this)));
909 if (rv == ERR_IO_PENDING) {
910 pending_read_buf_ = buf;
911 pending_read_buf_len_ = buf_len;
912 pending_read_callback_ = callback;
913 }
914 return rv;
915 }
903 916
904 // Store our async IO data. 917 int MockTCPClientSocket::ReadIfReady(IOBuffer* buf,
905 pending_read_buf_ = buf; 918 int buf_len,
906 pending_read_buf_len_ = buf_len; 919 const CompletionCallback& callback) {
907 pending_read_callback_ = callback; 920 DCHECK(!pending_read_if_ready_callback_);
908 921
909 if (need_read_data_) { 922 if (!enable_read_if_ready_)
910 read_data_ = data_->OnRead(); 923 return ERR_READ_IF_READY_NOT_IMPLEMENTED;
911 if (read_data_.result == ERR_CONNECTION_CLOSED) { 924 return ReadIfReadyHelper(buf, buf_len, callback);
davidben 2017/03/02 20:47:02 Nit: Perhaps ReadIfReadyHelper => ReadIfReadyImpl?
xunjieli 2017/03/02 22:30:20 Done.
912 // This MockRead is just a marker to instruct us to set
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
932 return CompleteRead();
933 } 925 }
934 926
935 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len, 927 int MockTCPClientSocket::Write(IOBuffer* buf, int buf_len,
936 const CompletionCallback& callback) { 928 const CompletionCallback& callback) {
937 DCHECK(buf); 929 DCHECK(buf);
938 DCHECK_GT(buf_len, 0); 930 DCHECK_GT(buf_len, 0);
939 931
940 if (!connected_ || !data_) 932 if (!connected_ || !data_)
941 return ERR_UNEXPECTED; 933 return ERR_UNEXPECTED;
942 934
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1041 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) { 1033 bool MockTCPClientSocket::GetSSLInfo(SSLInfo* ssl_info) {
1042 return false; 1034 return false;
1043 } 1035 }
1044 1036
1045 void MockTCPClientSocket::OnReadComplete(const MockRead& data) { 1037 void MockTCPClientSocket::OnReadComplete(const MockRead& data) {
1046 // If |data_| has been destroyed, safest to just do nothing. 1038 // If |data_| has been destroyed, safest to just do nothing.
1047 if (!data_) 1039 if (!data_)
1048 return; 1040 return;
1049 1041
1050 // There must be a read pending. 1042 // There must be a read pending.
1051 DCHECK(pending_read_buf_.get()); 1043 DCHECK(pending_read_if_ready_callback_);
1052 // You can't complete a read with another ERR_IO_PENDING status code. 1044 // You can't complete a read with another ERR_IO_PENDING status code.
1053 DCHECK_NE(ERR_IO_PENDING, data.result); 1045 DCHECK_NE(ERR_IO_PENDING, data.result);
1054 // Since we've been waiting for data, need_read_data_ should be true. 1046 // Since we've been waiting for data, need_read_data_ should be true.
1055 DCHECK(need_read_data_); 1047 DCHECK(need_read_data_);
1056 1048
1057 read_data_ = data; 1049 read_data_ = data;
1058 need_read_data_ = false; 1050 need_read_data_ = false;
1059 1051
1060 // The caller is simulating that this IO completes right now. Don't 1052 // The caller is simulating that this IO completes right now. Don't
1061 // let CompleteRead() schedule a callback. 1053 // let CompleteRead() schedule a callback.
1062 read_data_.mode = SYNCHRONOUS; 1054 read_data_.mode = SYNCHRONOUS;
1063 1055 CompletionCallback callback = pending_read_if_ready_callback_;
1064 CompletionCallback callback = pending_read_callback_; 1056 pending_read_if_ready_callback_.Reset();
1065 int rv = CompleteRead(); 1057 RunCallback(callback, read_data_.result > 0 ? OK : read_data_.result);
davidben 2017/03/02 20:47:02 Nit: This is probably a little tidier: RunCallb
xunjieli 2017/03/02 22:30:20 Done.
1066 RunCallback(callback, rv);
1067 } 1058 }
1068 1059
1069 void MockTCPClientSocket::OnWriteComplete(int rv) { 1060 void MockTCPClientSocket::OnWriteComplete(int rv) {
1070 // If |data_| has been destroyed, safest to just do nothing. 1061 // If |data_| has been destroyed, safest to just do nothing.
1071 if (!data_) 1062 if (!data_)
1072 return; 1063 return;
1073 1064
1074 // There must be a read pending. 1065 // There must be a read pending.
1075 DCHECK(!pending_write_callback_.is_null()); 1066 DCHECK(!pending_write_callback_.is_null());
1076 CompletionCallback callback = pending_write_callback_; 1067 CompletionCallback callback = pending_write_callback_;
1077 RunCallback(callback, rv); 1068 RunCallback(callback, rv);
1078 } 1069 }
1079 1070
1080 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) { 1071 void MockTCPClientSocket::OnConnectComplete(const MockConnect& data) {
1081 // If |data_| has been destroyed, safest to just do nothing. 1072 // If |data_| has been destroyed, safest to just do nothing.
1082 if (!data_) 1073 if (!data_)
1083 return; 1074 return;
1084 1075
1085 CompletionCallback callback = pending_connect_callback_; 1076 CompletionCallback callback = pending_connect_callback_;
1086 RunCallback(callback, data.result); 1077 RunCallback(callback, data.result);
1087 } 1078 }
1088 1079
1089 void MockTCPClientSocket::OnDataProviderDestroyed() { 1080 void MockTCPClientSocket::OnDataProviderDestroyed() {
1090 data_ = nullptr; 1081 data_ = nullptr;
1091 } 1082 }
1092 1083
1093 int MockTCPClientSocket::CompleteRead() { 1084 void MockTCPClientSocket::RetryRead(int rv) {
1085 DCHECK(pending_read_callback_);
1094 DCHECK(pending_read_buf_.get()); 1086 DCHECK(pending_read_buf_.get());
1095 DCHECK(pending_read_buf_len_ > 0); 1087 DCHECK_LT(0, pending_read_buf_len_);
1088
1089 if (rv == OK) {
1090 rv = ReadIfReadyHelper(
1091 pending_read_buf_.get(), pending_read_buf_len_,
1092 base::Bind(&MockTCPClientSocket::RetryRead, base::Unretained(this)));
1093 if (rv == ERR_IO_PENDING)
1094 return;
1095 }
1096 pending_read_buf_ = nullptr;
1097 pending_read_buf_len_ = 0;
1098 CompletionCallback callback = pending_read_callback_;
1099 pending_read_callback_.Reset();
1100 RunCallback(callback, rv);
davidben 2017/03/02 20:47:02 Nit: RunCallback(base::ResetAndReturn(&pending_rea
xunjieli 2017/03/02 22:30:20 Done.
1101 }
1102
1103 int MockTCPClientSocket::ReadIfReadyHelper(IOBuffer* buf,
1104 int buf_len,
1105 const CompletionCallback& callback) {
1106 if (!connected_ || !data_)
1107 return ERR_UNEXPECTED;
1108
1109 DCHECK(!pending_read_if_ready_callback_);
1110
1111 if (need_read_data_) {
1112 read_data_ = data_->OnRead();
1113 if (read_data_.result == ERR_CONNECTION_CLOSED) {
1114 // This MockRead is just a marker to instruct us to set
1115 // peer_closed_connection_.
1116 peer_closed_connection_ = true;
1117 }
1118 if (read_data_.result == ERR_TEST_PEER_CLOSE_AFTER_NEXT_MOCK_READ) {
1119 // This MockRead is just a marker to instruct us to set
1120 // peer_closed_connection_. Skip it and get the next one.
1121 read_data_ = data_->OnRead();
1122 peer_closed_connection_ = true;
1123 }
1124 // ERR_IO_PENDING means that the SocketDataProvider is taking responsibility
1125 // to complete the async IO manually later (via OnReadComplete).
1126 if (read_data_.result == ERR_IO_PENDING) {
1127 // We need to be using async IO in this case.
1128 DCHECK(!callback.is_null());
1129 pending_read_if_ready_callback_ = callback;
1130 return ERR_IO_PENDING;
1131 }
1132 need_read_data_ = false;
1133 }
1096 1134
1097 was_used_to_convey_data_ = true; 1135 was_used_to_convey_data_ = true;
davidben 2017/03/02 20:47:02 I think you want to move this to after the ASYNC c
xunjieli 2017/03/02 22:30:20 Done. Good catch. You are right!
1098 1136
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; 1137 int result = read_data_.result;
1108 DCHECK(result != ERR_IO_PENDING); 1138 DCHECK_NE(ERR_IO_PENDING, result);
1109 1139 if (read_data_.mode == ASYNC) {
1140 DCHECK(!callback.is_null());
1141 read_data_.mode = SYNCHRONOUS;
1142 RunCallbackAsync(callback, result);
1143 return ERR_IO_PENDING;
1144 }
1110 if (read_data_.data) { 1145 if (read_data_.data) {
1111 if (read_data_.data_len - read_offset_ > 0) { 1146 if (read_data_.data_len - read_offset_ > 0) {
1112 result = std::min(buf_len, read_data_.data_len - read_offset_); 1147 result = std::min(buf_len, read_data_.data_len - read_offset_);
1113 memcpy(buf->data(), read_data_.data + read_offset_, result); 1148 memcpy(buf->data(), read_data_.data + read_offset_, result);
1114 read_offset_ += result; 1149 read_offset_ += result;
1115 if (read_offset_ == read_data_.data_len) { 1150 if (read_offset_ == read_data_.data_len) {
1116 need_read_data_ = true; 1151 need_read_data_ = true;
1117 read_offset_ = 0; 1152 read_offset_ = 0;
1118 } 1153 }
1119 } else { 1154 } else {
1120 result = 0; // EOF 1155 result = 0; // EOF
1121 } 1156 }
1122 } 1157 }
1123
1124 if (read_data_.mode == ASYNC) {
1125 DCHECK(!callback.is_null());
1126 RunCallbackAsync(callback, result);
1127 return ERR_IO_PENDING;
1128 }
1129 return result; 1158 return result;
1130 } 1159 }
1131 1160
1132 // static 1161 // static
1133 void MockSSLClientSocket::ConnectCallback( 1162 void MockSSLClientSocket::ConnectCallback(
1134 MockSSLClientSocket* ssl_client_socket, 1163 MockSSLClientSocket* ssl_client_socket,
1135 const CompletionCallback& callback, 1164 const CompletionCallback& callback,
1136 int rv) { 1165 int rv) {
1137 if (rv == OK) 1166 if (rv == OK)
1138 ssl_client_socket->connected_ = true; 1167 ssl_client_socket->connected_ = true;
(...skipping 18 matching lines...) Expand all
1157 1186
1158 MockSSLClientSocket::~MockSSLClientSocket() { 1187 MockSSLClientSocket::~MockSSLClientSocket() {
1159 Disconnect(); 1188 Disconnect();
1160 } 1189 }
1161 1190
1162 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len, 1191 int MockSSLClientSocket::Read(IOBuffer* buf, int buf_len,
1163 const CompletionCallback& callback) { 1192 const CompletionCallback& callback) {
1164 return transport_->socket()->Read(buf, buf_len, callback); 1193 return transport_->socket()->Read(buf, buf_len, callback);
1165 } 1194 }
1166 1195
1196 int MockSSLClientSocket::ReadIfReady(IOBuffer* buf,
1197 int buf_len,
1198 const CompletionCallback& callback) {
1199 return transport_->socket()->ReadIfReady(buf, buf_len, callback);
1200 }
1201
1167 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len, 1202 int MockSSLClientSocket::Write(IOBuffer* buf, int buf_len,
1168 const CompletionCallback& callback) { 1203 const CompletionCallback& callback) {
1169 return transport_->socket()->Write(buf, buf_len, callback); 1204 return transport_->socket()->Write(buf, buf_len, callback);
1170 } 1205 }
1171 1206
1172 int MockSSLClientSocket::Connect(const CompletionCallback& callback) { 1207 int MockSSLClientSocket::Connect(const CompletionCallback& callback) {
1173 int rv = transport_->socket()->Connect( 1208 int rv = transport_->socket()->Connect(
1174 base::Bind(&ConnectCallback, base::Unretained(this), callback)); 1209 base::Bind(&ConnectCallback, base::Unretained(this), callback));
1175 if (rv == OK) { 1210 if (rv == OK) {
1176 if (data_->connect.result == OK) 1211 if (data_->connect.result == OK)
(...skipping 567 matching lines...) Expand 10 before | Expand all | Expand 10 after
1744 } 1779 }
1745 1780
1746 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) { 1781 int64_t CountWriteBytes(const MockWrite writes[], size_t writes_size) {
1747 int64_t total = 0; 1782 int64_t total = 0;
1748 for (const MockWrite* write = writes; write != writes + writes_size; ++write) 1783 for (const MockWrite* write = writes; write != writes + writes_size; ++write)
1749 total += write->data_len; 1784 total += write->data_len;
1750 return total; 1785 return total;
1751 } 1786 }
1752 1787
1753 } // namespace net 1788 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698