OLD | NEW |
---|---|
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/udp/udp_socket.h" | 5 #include "net/udp/udp_socket.h" |
6 | 6 |
7 #include "net/udp/udp_client_socket.h" | 7 #include "net/udp/udp_client_socket.h" |
8 #include "net/udp/udp_server_socket.h" | 8 #include "net/udp/udp_server_socket.h" |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/memory/weak_ptr.h" | |
12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/run_loop.h" | |
13 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
14 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
15 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
16 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
17 #include "net/base/net_log_unittest.h" | 19 #include "net/base/net_log_unittest.h" |
18 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
19 #include "net/base/test_completion_callback.h" | 21 #include "net/base/test_completion_callback.h" |
20 #include "net/test/net_test_suite.h" | 22 #include "net/test/net_test_suite.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
23 | 25 |
24 namespace net { | 26 namespace net { |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 class UDPSocketTest : public PlatformTest { | 30 class UDPSocketTest : public PlatformTest { |
29 public: | 31 public: |
30 UDPSocketTest() | 32 UDPSocketTest() |
31 : buffer_(new IOBufferWithSize(kMaxRead)) { | 33 : buffer_(new IOBufferWithSize(kMaxRead)), weak_factory_(this) {} |
32 } | |
33 | 34 |
34 // Blocks until data is read from the socket. | 35 // Blocks until data is read from the socket. |
35 std::string RecvFromSocket(UDPServerSocket* socket) { | 36 std::string RecvFromSocket(UDPServerSocket* socket) { |
36 TestCompletionCallback callback; | 37 TestCompletionCallback callback; |
37 | 38 |
38 int rv = socket->RecvFrom( | 39 int rv = socket->RecvFrom( |
39 buffer_.get(), kMaxRead, &recv_from_address_, callback.callback()); | 40 buffer_.get(), kMaxRead, &recv_from_address_, callback.callback()); |
40 if (rv == ERR_IO_PENDING) | 41 if (rv == ERR_IO_PENDING) |
41 rv = callback.WaitForResult(); | 42 rv = callback.WaitForResult(); |
42 if (rv < 0) | 43 if (rv < 0) |
43 return std::string(); // error! | 44 return std::string(); // error! |
44 return std::string(buffer_->data(), rv); | 45 return std::string(buffer_->data(), rv); |
45 } | 46 } |
46 | 47 |
48 void DoneWritePacketsToSocket(UDPClientSocket* socket, | |
49 int num_of_packets, | |
50 base::Closure done_callback, | |
51 int error) { | |
52 WritePacketsToSocket(socket, num_of_packets, done_callback); | |
53 } | |
54 | |
55 // Send |num_of_packets| to |socket|. Invoke |done_callback| when done. | |
56 void WritePacketsToSocket(UDPClientSocket* socket, | |
57 int num_of_packets, | |
58 base::Closure done_callback) { | |
59 std::string msg(kMaxRead, 'G'); | |
60 scoped_refptr<StringIOBuffer> io_buffer(new StringIOBuffer(msg)); | |
61 | |
62 while (num_of_packets) { | |
63 int rv = | |
64 socket->Write(io_buffer.get(), io_buffer->size(), | |
65 base::Bind(&UDPSocketTest::DoneWritePacketsToSocket, | |
66 weak_factory_.GetWeakPtr(), socket, | |
67 num_of_packets - 1, done_callback)); | |
68 if (rv == ERR_IO_PENDING) | |
69 break; | |
70 --num_of_packets; | |
71 } | |
72 if (!num_of_packets) { | |
73 done_callback.Run(); | |
74 return; | |
75 } | |
76 } | |
77 | |
47 // Loop until |msg| has been written to the socket or until an | 78 // Loop until |msg| has been written to the socket or until an |
48 // error occurs. | 79 // error occurs. |
49 // If |address| is specified, then it is used for the destination | 80 // If |address| is specified, then it is used for the destination |
50 // to send to. Otherwise, will send to the last socket this server | 81 // to send to. Otherwise, will send to the last socket this server |
51 // received from. | 82 // received from. |
52 int SendToSocket(UDPServerSocket* socket, std::string msg) { | 83 int SendToSocket(UDPServerSocket* socket, std::string msg) { |
53 return SendToSocket(socket, msg, recv_from_address_); | 84 return SendToSocket(socket, msg, recv_from_address_); |
54 } | 85 } |
55 | 86 |
56 int SendToSocket(UDPServerSocket* socket, | 87 int SendToSocket(UDPServerSocket* socket, |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 bytes_sent += rv; | 140 bytes_sent += rv; |
110 buffer->DidConsume(rv); | 141 buffer->DidConsume(rv); |
111 } | 142 } |
112 return bytes_sent; | 143 return bytes_sent; |
113 } | 144 } |
114 | 145 |
115 protected: | 146 protected: |
116 static const int kMaxRead = 1024; | 147 static const int kMaxRead = 1024; |
117 scoped_refptr<IOBufferWithSize> buffer_; | 148 scoped_refptr<IOBufferWithSize> buffer_; |
118 IPEndPoint recv_from_address_; | 149 IPEndPoint recv_from_address_; |
150 base::WeakPtrFactory<UDPSocketTest> weak_factory_; | |
119 }; | 151 }; |
120 | 152 |
121 // Creates and address from an ip/port and returns it in |address|. | 153 // Creates and address from an ip/port and returns it in |address|. |
122 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) { | 154 void CreateUDPAddress(std::string ip_str, uint16 port, IPEndPoint* address) { |
123 IPAddressNumber ip_number; | 155 IPAddressNumber ip_number; |
124 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); | 156 bool rv = ParseIPLiteralToNumber(ip_str, &ip_number); |
125 if (!rv) | 157 if (!rv) |
126 return; | 158 return; |
127 *address = IPEndPoint(ip_number, port); | 159 *address = IPEndPoint(ip_number, port); |
128 } | 160 } |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
615 | 647 |
616 client.SetDiffServCodePoint(DSCP_NO_CHANGE); | 648 client.SetDiffServCodePoint(DSCP_NO_CHANGE); |
617 client.SetDiffServCodePoint(DSCP_AF41); | 649 client.SetDiffServCodePoint(DSCP_AF41); |
618 client.SetDiffServCodePoint(DSCP_DEFAULT); | 650 client.SetDiffServCodePoint(DSCP_DEFAULT); |
619 client.SetDiffServCodePoint(DSCP_CS2); | 651 client.SetDiffServCodePoint(DSCP_CS2); |
620 client.SetDiffServCodePoint(DSCP_NO_CHANGE); | 652 client.SetDiffServCodePoint(DSCP_NO_CHANGE); |
621 client.SetDiffServCodePoint(DSCP_DEFAULT); | 653 client.SetDiffServCodePoint(DSCP_DEFAULT); |
622 client.Close(); | 654 client.Close(); |
623 } | 655 } |
624 | 656 |
657 TEST_F(UDPSocketTest, DISABLED_WriteBenchmark) { | |
rvargas (doing something else)
2015/01/21 22:10:09
benchmarks belong to net_perftests
Alpha Left Google
2015/01/22 01:01:25
Doing this in a later patchset.
| |
658 const uint16 kPort = 9999; | |
659 std::string simple_message("hello world!"); | |
660 | |
661 // Setup the server to listen. | |
662 IPEndPoint bind_address; | |
663 CreateUDPAddress("127.0.0.1", kPort, &bind_address); | |
664 CapturingNetLog server_log; | |
665 scoped_ptr<UDPServerSocket> server( | |
666 new UDPServerSocket(&server_log, NetLog::Source())); | |
667 server->AllowAddressReuse(); | |
668 int rv = server->Listen(bind_address); | |
669 ASSERT_EQ(OK, rv); | |
670 | |
671 // Setup the client. | |
672 IPEndPoint server_address; | |
673 CreateUDPAddress("127.0.0.1", kPort, &server_address); | |
674 CapturingNetLog client_log; | |
675 scoped_ptr<UDPClientSocket> client( | |
676 new UDPClientSocket(DatagramSocket::DEFAULT_BIND, RandIntCallback(), | |
677 &client_log, NetLog::Source())); | |
678 rv = client->Connect(server_address); | |
679 EXPECT_EQ(OK, rv); | |
680 | |
681 base::RunLoop run_loop; | |
682 base::TimeTicks start_ticks = base::TimeTicks::Now(); | |
683 int packets = 100000; | |
684 client->SetSendBufferSize(1024 * 128); | |
685 WritePacketsToSocket(client.get(), packets, run_loop.QuitClosure()); | |
686 run_loop.Run(); | |
687 | |
688 double elapsed = (base::TimeTicks::Now() - start_ticks).InSecondsF(); | |
689 LOG(INFO) << "Write speed: " << packets / 1024 / elapsed << " MB/s"; | |
690 server.reset(); | |
691 client.reset(); | |
692 } | |
693 | |
625 } // namespace | 694 } // namespace |
626 | 695 |
627 #if defined(OS_WIN) | 696 #if defined(OS_WIN) |
628 | 697 |
629 namespace { | 698 namespace { |
630 | 699 |
631 const HANDLE kFakeHandle = (HANDLE)19; | 700 const HANDLE kFakeHandle = (HANDLE)19; |
632 const QOS_FLOWID kFakeFlowId = (QOS_FLOWID)27; | 701 const QOS_FLOWID kFakeFlowId = (QOS_FLOWID)27; |
633 | 702 |
634 BOOL WINAPI FakeQOSCreateHandleFAIL(PQOS_VERSION version, PHANDLE handle) { | 703 BOOL WINAPI FakeQOSCreateHandleFAIL(PQOS_VERSION version, PHANDLE handle) { |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
743 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; | 812 g_expected_traffic_type = QOSTrafficTypeExcellentEffort; |
744 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); | 813 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_NO_CHANGE)); |
745 g_expected_dscp = DSCP_DEFAULT; | 814 g_expected_dscp = DSCP_DEFAULT; |
746 g_expected_traffic_type = QOSTrafficTypeBestEffort; | 815 g_expected_traffic_type = QOSTrafficTypeBestEffort; |
747 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); | 816 EXPECT_EQ(OK, client.SetDiffServCodePoint(DSCP_DEFAULT)); |
748 client.Close(); | 817 client.Close(); |
749 } | 818 } |
750 #endif | 819 #endif |
751 | 820 |
752 } // namespace net | 821 } // namespace net |
OLD | NEW |