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
3 // found in the LICENSE file.
5 #include "net/tools/quic/quic_client.h"
10 #include "base/basictypes.h"
11 #include "base/strings/string_util.h"
12 #include "net/quic/test_tools/quic_test_utils.h"
13 #include "net/tools/epoll_server/epoll_server.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using net::EpollServer
;
24 int number_of_open_fds
= 0;
28 base::snprintf(buf
, arraysize(buf
), "/proc/%i/fd/", getpid());
29 DIR* dir
= opendir(buf
);
30 while ((dp
= readdir(dir
)) != NULL
)
34 return number_of_open_fds
;
37 // Creates a new QuicClient and Initializes it. Caller is responsible for
39 QuicClient
* CreateAndInitializeQuicClient(EpollServer
* eps
, uint16 port
) {
40 IPEndPoint
server_address(IPEndPoint(net::test::Loopback4(), port
));
41 QuicServerId
server_id("hostname", server_address
.port(), false,
42 PRIVACY_MODE_DISABLED
);
43 QuicVersionVector versions
= QuicSupportedVersions();
44 QuicClient
* client
= new QuicClient(server_address
, server_id
, versions
, eps
);
45 EXPECT_TRUE(client
->Initialize());
49 TEST(QuicClientTest
, DoNotLeakFDs
) {
50 // Make sure that the QuicClient doesn't leak FDs. Doing so could cause port
51 // exhaustion in long running processes which repeatedly create clients.
53 // Record initial number of FDs, after creation of EpollServer.
55 int number_of_open_fds
= NumOpenFDs();
57 // Create a number of clients, initialize them, and verify this has resulted
58 // in additional FDs being opened.
59 const int kNumClients
= 5;
60 for (int i
= 0; i
< kNumClients
; ++i
) {
61 scoped_ptr
<QuicClient
> client(
62 CreateAndInitializeQuicClient(&eps
, net::test::kTestPort
+ i
));
64 // Initializing the client will create a new FD.
65 EXPECT_LT(number_of_open_fds
, NumOpenFDs());
68 // The FDs created by the QuicClients should now be closed.
69 EXPECT_EQ(number_of_open_fds
, NumOpenFDs());