1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2015 Red Hat
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
14 #include "rgw/rgw_rados.h"
15 #include "rgw/rgw_http_client.h"
16 #include "global/global_init.h"
17 #include "common/ceph_argparse.h"
18 #include <curl/curl.h>
19 #include <boost/asio/ip/tcp.hpp>
20 #include <boost/asio/write.hpp>
22 #include <gtest/gtest.h>
26 TEST(HTTPManager
, ReadTruncated
)
28 using tcp
= boost::asio::ip::tcp
;
29 tcp::endpoint
endpoint(tcp::v4(), 0);
30 boost::asio::io_context ioctx
;
31 tcp::acceptor
acceptor(ioctx
);
32 acceptor
.open(endpoint
.protocol());
33 acceptor
.bind(endpoint
);
36 std::thread server
{[&] {
37 tcp::socket socket
{ioctx
};
38 acceptor
.accept(socket
);
39 std::string_view response
=
41 "Content-Length: 1024\r\n"
44 boost::asio::write(socket
, boost::asio::buffer(response
));
46 const auto url
= std::string
{"http://127.0.0.1:"} + std::to_string(acceptor
.local_endpoint().port());
48 RGWHTTPClient client
{g_ceph_context
, "GET", url
};
49 EXPECT_EQ(-EAGAIN
, RGWHTTP::process(&client
, null_yield
));
54 TEST(HTTPManager
, Head
)
56 using tcp
= boost::asio::ip::tcp
;
57 tcp::endpoint
endpoint(tcp::v4(), 0);
58 boost::asio::io_context ioctx
;
59 tcp::acceptor
acceptor(ioctx
);
60 acceptor
.open(endpoint
.protocol());
61 acceptor
.bind(endpoint
);
64 std::thread server
{[&] {
65 tcp::socket socket
{ioctx
};
66 acceptor
.accept(socket
);
67 std::string_view response
=
69 "Content-Length: 1024\r\n"
71 boost::asio::write(socket
, boost::asio::buffer(response
));
73 const auto url
= std::string
{"http://127.0.0.1:"} + std::to_string(acceptor
.local_endpoint().port());
75 RGWHTTPClient client
{g_ceph_context
, "HEAD", url
};
76 EXPECT_EQ(0, RGWHTTP::process(&client
, null_yield
));
81 TEST(HTTPManager
, SignalThread
)
83 auto cct
= g_ceph_context
;
84 RGWHTTPManager
http(cct
);
86 ASSERT_EQ(0, http
.start());
88 // default pipe buffer size according to man pipe
89 constexpr size_t max_pipe_buffer_size
= 65536;
90 // each signal writes 4 bytes to the pipe
91 constexpr size_t max_pipe_signals
= max_pipe_buffer_size
/ sizeof(uint32_t);
92 // add_request and unregister_request
93 constexpr size_t pipe_signals_per_request
= 2;
94 // number of http requests to fill the pipe buffer
95 constexpr size_t max_requests
= max_pipe_signals
/ pipe_signals_per_request
;
97 // send one extra request to test that we don't deadlock
98 constexpr size_t num_requests
= max_requests
+ 1;
100 for (size_t i
= 0; i
< num_requests
; i
++) {
101 RGWHTTPClient client
{cct
, "PUT", "http://127.0.0.1:80"};
102 http
.add_request(&client
);
106 int main(int argc
, char** argv
)
108 auto args
= argv_to_vec(argc
, argv
);
109 auto cct
= global_init(NULL
, args
, CEPH_ENTITY_TYPE_CLIENT
,
110 CODE_ENVIRONMENT_UTILITY
,
111 CINIT_FLAG_NO_DEFAULT_CONFIG_FILE
);
112 common_init_finish(g_ceph_context
);
114 rgw_http_client_init(cct
->get());
115 rgw_setup_saved_curl_handles();
116 ::testing::InitGoogleTest(&argc
, argv
);
117 int r
= RUN_ALL_TESTS();
118 rgw_release_all_curl_handles();
119 rgw_http_client_cleanup();