OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 <dirent.h> | 5 #include "net/tools/quic/quic_simple_client.h" |
6 #include <stdio.h> | |
7 | 6 |
8 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
9 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
10 #include "net/quic/test_tools/quic_test_utils.h" | 9 #include "net/quic/test_tools/quic_test_utils.h" |
11 #include "net/tools/epoll_server/epoll_server.h" | |
12 #include "net/tools/quic/quic_client.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
14 | 11 |
15 using net::EpollServer; | |
16 | |
17 namespace net { | 12 namespace net { |
18 namespace tools { | 13 namespace tools { |
19 namespace test { | 14 namespace test { |
20 namespace { | |
21 | 15 |
22 int NumOpenFDs() { | 16 TEST(QuicSimpleClientTest, Initialize) { |
23 int number_of_open_fds = 0; | 17 IPEndPoint server_address(IPEndPoint(net::test::Loopback4(), 80)); |
24 char buf[256]; | |
25 struct dirent* dp; | |
26 | |
27 base::snprintf(buf, arraysize(buf), "/proc/%i/fd/", getpid()); | |
28 DIR* dir = opendir(buf); | |
29 while ((dp = readdir(dir)) != NULL) | |
30 number_of_open_fds++; | |
31 closedir(dir); | |
32 | |
33 return number_of_open_fds; | |
34 } | |
35 | |
36 // Creates a new QuicClient and Initializes it. Caller is responsible for | |
37 // deletion. | |
38 QuicClient* CreateAndInitializeQuicClient(EpollServer* eps, uint16 port) { | |
39 IPEndPoint server_address(IPEndPoint(net::test::Loopback4(), port)); | |
40 QuicServerId server_id("hostname", server_address.port(), false, | 18 QuicServerId server_id("hostname", server_address.port(), false, |
41 PRIVACY_MODE_DISABLED); | 19 PRIVACY_MODE_DISABLED); |
42 QuicVersionVector versions = QuicSupportedVersions(); | 20 QuicVersionVector versions = QuicSupportedVersions(); |
43 QuicClient* client = new QuicClient(server_address, server_id, versions, eps); | 21 QuicSimpleClient client(server_address, server_id, versions); |
44 EXPECT_TRUE(client->Initialize()); | 22 EXPECT_TRUE(client.Initialize()); |
45 return client; | |
46 } | 23 } |
47 | 24 |
48 TEST(QuicClientTest, DoNotLeakFDs) { | |
49 // Make sure that the QuicClient doesn't leak FDs. Doing so could cause port | |
50 // exhaustion in long running processes which repeatedly create clients. | |
51 | |
52 // Record initial number of FDs, after creation of EpollServer. | |
53 EpollServer eps; | |
54 int number_of_open_fds = NumOpenFDs(); | |
55 | |
56 // Create a number of clients, initialize them, and verify this has resulted | |
57 // in additional FDs being opened. | |
58 const int kNumClients = 5; | |
59 for (int i = 0; i < kNumClients; ++i) { | |
60 std::unique_ptr<QuicClient> client( | |
61 CreateAndInitializeQuicClient(&eps, net::test::kTestPort + i)); | |
62 | |
63 // Initializing the client will create a new FD. | |
64 EXPECT_LT(number_of_open_fds, NumOpenFDs()); | |
65 } | |
66 | |
67 // The FDs created by the QuicClients should now be closed. | |
68 EXPECT_EQ(number_of_open_fds, NumOpenFDs()); | |
69 } | |
70 | |
71 } // namespace | |
72 } // namespace test | 25 } // namespace test |
73 } // namespace tools | 26 } // namespace tools |
74 } // namespace net | 27 } // namespace net |
OLD | NEW |