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

Unified Diff: net/socket/socket_test_util.h

Issue 10796085: Give MockRead and MockWrite distinct types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase and fix compile. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/http/http_proxy_client_socket_pool_spdy3_unittest.cc ('k') | net/socket/socket_test_util.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/socket/socket_test_util.h
diff --git a/net/socket/socket_test_util.h b/net/socket/socket_test_util.h
index 171e3c3d2e536d4cbc3878e9c57d2de837c7d72a..70876400d4a26eaec2cbcbb84422ac1bd5b0d211 100644
--- a/net/socket/socket_test_util.h
+++ b/net/socket/socket_test_util.h
@@ -72,48 +72,65 @@ struct MockConnect {
IPEndPoint peer_addr;
};
-struct MockRead {
+// MockRead and MockWrite shares the same interface and members, but we'd like
+// to have distinct types because we don't want to have them used
+// interchangably. To do this, a struct template is defined, and MockRead and
+// MockWrite are instantiated by using this template. Template parameter |type|
+// is not used in the struct definition (it purely exists for creating a new
+// type).
+//
+// |data| in MockRead and MockWrite has different meanings: |data| in MockRead
+// is the data returned from the socket when MockTCPClientSocket::Read() is
+// attempted, while |data| in MockWrite is the expected data that should be
+// given in MockTCPClientSocket::Write().
+enum MockReadWriteType {
+ MOCK_READ,
+ MOCK_WRITE
+};
+
+template <MockReadWriteType type>
+struct MockReadWrite {
// Flag to indicate that the message loop should be terminated.
enum {
STOPLOOP = 1 << 31
};
// Default
- MockRead() : mode(SYNCHRONOUS), result(0), data(NULL), data_len(0),
+ MockReadWrite() : mode(SYNCHRONOUS), result(0), data(NULL), data_len(0),
sequence_number(0), time_stamp(base::Time::Now()) {}
- // Read failure (no data).
- MockRead(IoMode io_mode, int result) : mode(io_mode), result(result),
+ // Read/write failure (no data).
+ MockReadWrite(IoMode io_mode, int result) : mode(io_mode), result(result),
data(NULL), data_len(0), sequence_number(0),
time_stamp(base::Time::Now()) { }
- // Read failure (no data), with sequence information.
- MockRead(IoMode io_mode, int result, int seq) : mode(io_mode),
+ // Read/write failure (no data), with sequence information.
+ MockReadWrite(IoMode io_mode, int result, int seq) : mode(io_mode),
result(result), data(NULL), data_len(0), sequence_number(seq),
time_stamp(base::Time::Now()) { }
- // Asynchronous read success (inferred data length).
- explicit MockRead(const char* data) : mode(ASYNC), result(0), data(data),
- data_len(strlen(data)), sequence_number(0),
+ // Asynchronous read/write success (inferred data length).
+ explicit MockReadWrite(const char* data) : mode(ASYNC), result(0),
+ data(data), data_len(strlen(data)), sequence_number(0),
time_stamp(base::Time::Now()) { }
- // Read success (inferred data length).
- MockRead(IoMode io_mode, const char* data) : mode(io_mode), result(0),
+ // Read/write success (inferred data length).
+ MockReadWrite(IoMode io_mode, const char* data) : mode(io_mode), result(0),
data(data), data_len(strlen(data)), sequence_number(0),
time_stamp(base::Time::Now()) { }
- // Read success.
- MockRead(IoMode io_mode, const char* data, int data_len) : mode(io_mode),
+ // Read/write success.
+ MockReadWrite(IoMode io_mode, const char* data, int data_len) : mode(io_mode),
result(0), data(data), data_len(data_len), sequence_number(0),
time_stamp(base::Time::Now()) { }
- // Read success (inferred data length) with sequence information.
- MockRead(IoMode io_mode, int seq, const char* data) : mode(io_mode),
+ // Read/write success (inferred data length) with sequence information.
+ MockReadWrite(IoMode io_mode, int seq, const char* data) : mode(io_mode),
result(0), data(data), data_len(strlen(data)), sequence_number(seq),
time_stamp(base::Time::Now()) { }
- // Read success with sequence information.
- MockRead(IoMode io_mode, const char* data, int data_len, int seq) :
+ // Read/write success with sequence information.
+ MockReadWrite(IoMode io_mode, const char* data, int data_len, int seq) :
mode(io_mode), result(0), data(data), data_len(data_len),
sequence_number(seq), time_stamp(base::Time::Now()) { }
@@ -130,11 +147,8 @@ struct MockRead {
base::Time time_stamp; // The time stamp at which the operation occurred.
};
-// MockWrite uses the same member fields as MockRead, but with different
-// meanings. The expected input to MockTCPClientSocket::Write() is given
-// by {data, data_len}, and the return value of Write() is controlled by
-// {async, result}.
-typedef MockRead MockWrite;
+typedef MockReadWrite<MOCK_READ> MockRead;
+typedef MockReadWrite<MOCK_WRITE> MockWrite;
struct MockWriteResult {
MockWriteResult(IoMode io_mode, int result)
@@ -461,7 +475,7 @@ class DeterministicSocketData
bool stopped() const { return stopped_; }
void SetStopped(bool val) { stopped_ = val; }
MockRead& current_read() { return current_read_; }
- MockRead& current_write() { return current_write_; }
+ MockWrite& current_write() { return current_write_; }
int sequence_number() const { return sequence_number_; }
void set_socket(base::WeakPtr<DeterministicMockTCPClientSocket> socket) {
socket_ = socket;
« no previous file with comments | « net/http/http_proxy_client_socket_pool_spdy3_unittest.cc ('k') | net/socket/socket_test_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698