mirror of
https://gitlab.isc.org/isc-projects/kea
synced 2025-09-01 14:35:29 +00:00
[#1732] Minor clean up
modified: src/lib/http/client.h src/lib/http/tests/mt_client_unittests.cc
This commit is contained in:
@@ -63,9 +63,9 @@ class HttpClientImpl;
|
||||
/// an external IOService passed into the class constructor, and ultimately
|
||||
/// only a single connection per URL can be open at any given time.
|
||||
///
|
||||
/// In multi-threaded mode, an internal thread pool, driven by a private
|
||||
/// IOService instance, is used to support multiple concurrent connections
|
||||
/// per URL. Currently the number of connections per URL is equal to the
|
||||
/// In multi-threaded mode an internal thread pool driven by a private
|
||||
/// IOService instance is used to support multiple concurrent connections
|
||||
/// per URL. Currently, the number of connections per URL is set to the
|
||||
/// number of threads in the thread pool.
|
||||
///
|
||||
/// The client tests the persistent connection for usability before sending
|
||||
@@ -136,8 +136,9 @@ public:
|
||||
///
|
||||
/// @param io_service IO service to be used by the HTTP client.
|
||||
/// @param thread_pool_size maximum number of threads in the thread pool.
|
||||
/// Currently this also sets the maximum number of concurrent connections
|
||||
/// per URL.
|
||||
/// A value greater than zero enables multi-threaded mode as sets the
|
||||
/// maximum number of concurrent connections per URL. A value of zero
|
||||
/// (default) enables single-threaded mode with one connection per URL.
|
||||
explicit HttpClient(asiolink::IOService& io_service, size_t thread_pool_size = 0);
|
||||
|
||||
/// @brief Destructor.
|
||||
@@ -241,7 +242,11 @@ public:
|
||||
const CloseHandler& close_callback =
|
||||
CloseHandler());
|
||||
|
||||
/// @brief Closes all connections.
|
||||
/// @brief Halts client-side IO activity.
|
||||
///
|
||||
/// Closes all connections, discards any queued requests, and in
|
||||
/// multi-threaded mode discards the thread-pool and the internal
|
||||
/// IOService.
|
||||
void stop();
|
||||
|
||||
/// @brief Closes a connection if it has an out-of-band socket event
|
||||
|
@@ -50,12 +50,15 @@ const long TEST_TIMEOUT = 10000;
|
||||
|
||||
/// @brief Container request/response pair handled by a given thread.
|
||||
struct ClientRR {
|
||||
/// @brief Thread id of the client thread handling the request as a string.
|
||||
std::string thread_id_;
|
||||
/// @brief HTTP request submitted by the client thread.
|
||||
PostHttpRequestJsonPtr request_;
|
||||
/// @brief HTTP response received by the client thread.
|
||||
HttpResponseJsonPtr response_;
|
||||
};
|
||||
|
||||
/// @brief Pointer to a ClientRR.
|
||||
/// @brief Pointer to a ClientRR instance.
|
||||
typedef boost::shared_ptr<ClientRR> ClientRRPtr;
|
||||
|
||||
/// @brief Implementation of the @ref HttpResponseCreator.
|
||||
@@ -103,7 +106,6 @@ private:
|
||||
// Request must always be JSON.
|
||||
PostHttpRequestJsonPtr request_json =
|
||||
boost::dynamic_pointer_cast<PostHttpRequestJson>(request);
|
||||
|
||||
if (!request_json) {
|
||||
return(createStockHttpResponse(request, HttpStatusCode::BAD_REQUEST));
|
||||
}
|
||||
@@ -114,6 +116,7 @@ private:
|
||||
return(createStockHttpResponse(request, HttpStatusCode::BAD_REQUEST));
|
||||
}
|
||||
|
||||
// Create the response.
|
||||
HttpResponseJsonPtr response(new HttpResponseJson(request->getHttpVersion(),
|
||||
HttpStatusCode::OK));
|
||||
|
||||
@@ -138,7 +141,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
/// @brief Test fixture class for testing multi-threaded HTTP client.
|
||||
/// @brief Test fixture class for testing threading modes of HTTP client.
|
||||
class MtHttpClientTest : public ::testing::Test {
|
||||
public:
|
||||
|
||||
@@ -162,7 +165,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Callback function invoke upon test timeout.
|
||||
/// @brief Callback function to invoke upon test timeout.
|
||||
///
|
||||
/// It stops the IO service and reports test timeout.
|
||||
///
|
||||
@@ -174,11 +177,12 @@ public:
|
||||
io_service_.stop();
|
||||
}
|
||||
|
||||
/// @brief Runs test's IOService until the desired number of have been carried out.
|
||||
/// @brief Runs the test's IOService until the desired number of rquests
|
||||
/// have been carried out or the test fails.
|
||||
void runIOService() {
|
||||
// Loop until the clients are done, an error occurs, or the time runs out.
|
||||
while (clientRRs_.size() < num_requests_) {
|
||||
// Always call restart() before we call run();
|
||||
// Always call reset() before we call run();
|
||||
io_service_.get_io_service().reset();
|
||||
|
||||
// Run until a client stops the service.
|
||||
@@ -186,7 +190,7 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief Creates HTTP request with JSON body.
|
||||
/// @brief Creates an HTTP request with JSON body.
|
||||
///
|
||||
/// It includes a JSON parameter with a specified value.
|
||||
///
|
||||
@@ -206,7 +210,6 @@ public:
|
||||
request->setBodyAsJson(body);
|
||||
try {
|
||||
request->finalize();
|
||||
|
||||
} catch (const std::exception& ex) {
|
||||
ADD_FAILURE() << "failed to create request: " << ex.what();
|
||||
}
|
||||
@@ -236,17 +239,16 @@ public:
|
||||
|
||||
// Initiate request to the server.
|
||||
PostHttpRequestJsonPtr request_json = createRequest("sequence", sequence);
|
||||
|
||||
HttpResponseJsonPtr response_json = boost::make_shared<HttpResponseJson>();
|
||||
|
||||
ASSERT_NO_THROW(client_->asyncSendRequest(url, TlsContextPtr(),
|
||||
request_json, response_json,
|
||||
[this, request_json, response_json](const boost::system::error_code& ec,
|
||||
const HttpResponsePtr&/* response*/,
|
||||
const HttpResponsePtr&,
|
||||
const std::string&) {
|
||||
// Bail on an error.
|
||||
ASSERT_FALSE(ec) << "asyncSendRequest failed, ec: " << ec;
|
||||
|
||||
// Wait here until we have a many in progress as we have threads.
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(mutex_);
|
||||
++num_in_progress_;
|
||||
@@ -272,6 +274,7 @@ public:
|
||||
clientRR->request_ = request_json;
|
||||
clientRR->response_ = response_json;
|
||||
|
||||
// Wait here until we have as many ready to finish as we have threads.
|
||||
{
|
||||
std::unique_lock<std::mutex> lck(mutex_);
|
||||
num_finished_++;
|
||||
|
Reference in New Issue
Block a user