Skip to content
Snippets Groups Projects
Commit 2c4694ac authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

move http_client to common

parent dd0e5fbb
No related branches found
No related tags found
No related merge requests found
Showing
with 141 additions and 71 deletions
......@@ -29,6 +29,11 @@ set (HIDRA2_CXX_COMMON_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/common/cpp/includ
find_package (Threads)
set (CMAKE_PREFIX_PATH "${LIBCURL_DIR}")
find_package (CURL REQUIRED)
message (STATUS "Found cURL libraries: ${CURL_LIBRARIES}")
message (STATUS "cURL include: ${CURL_INCLUDE_DIRS}")
# format sources
include(astyle)
......
......@@ -6,6 +6,8 @@ add_subdirectory(src/json_parser)
add_subdirectory(src/data_structs)
add_subdirectory(src/http_client)
if(BUILD_MONGODB_CLIENTLIB)
add_subdirectory(src/database)
endif()
......
#ifndef HIDRA2_HTTP_CLIENT_H
#define HIDRA2_HTTP_CLIENT_H
#include <hidra2_worker.h>
#include "common/error.h"
namespace hidra2 {
......@@ -14,8 +14,6 @@ class HttpClient {
};
Error HttpCodeToWorkerError(const HttpCode& code);
enum class HttpCode : int {
Continue = 100,
SwitchingProtocols = 101,
......
set(TARGET_NAME curl_http_client)
set(SOURCE_FILES
curl_http_client.cpp
)
################################
# Library
################################
add_library(${TARGET_NAME} OBJECT ${SOURCE_FILES})
IF(WIN32)
target_compile_definitions(${TARGET_NAME} PUBLIC -DCURL_STATICLIB)
ENDIF()
target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR} ${CURL_INCLUDE_DIRS})
#include "curl_http_client.h"
#include "http_client/curl_http_client.h"
#include <cstring>
......@@ -56,7 +56,7 @@ Error ProcessCurlResponse(CURL* curl, CURLcode res, const char* errbuf,
return nullptr;
} else {
*buffer = GetCurlError(curl, res, errbuf);
return TextError(std::string(WorkerErrorMessage::kErrorReadingSource) + ": " + *buffer);
return TextError("Curl client error: " + *buffer);
}
}
......
......@@ -13,19 +13,18 @@ set(SOURCE_FILES
################################
# Library
################################
add_library(${TARGET_NAME} STATIC ${SOURCE_FILES} $<TARGET_OBJECTS:system_io>)
add_library(${TARGET_NAME} STATIC ${SOURCE_FILES} $<TARGET_OBJECTS:system_io> $<TARGET_OBJECTS:curl_http_client>)
set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR})
target_include_directories(${TARGET_NAME} PUBLIC ${HIDRA2_CXX_COMMON_INCLUDE_DIR} ${CURL_INCLUDE_DIRS})
target_link_libraries(${TARGET_NAME} ${CURL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
add_executable(${TARGET_NAME}-bin src/main.cpp)
set_target_properties(${TARGET_NAME}-bin PROPERTIES OUTPUT_NAME ${TARGET_NAME})
target_link_libraries(${TARGET_NAME}-bin ${TARGET_NAME})
#Add all necessary common libraries
GET_PROPERTY(HIDRA2_COMMON_IO_LIBRARIES GLOBAL PROPERTY HIDRA2_COMMON_IO_LIBRARIES)
target_link_libraries(${TARGET_NAME}-bin ${HIDRA2_COMMON_IO_LIBRARIES})
set_target_properties(${TARGET_NAME}-bin PROPERTIES LINKER_LANGUAGE CXX)
################################
# Testing
################################
......@@ -38,6 +37,7 @@ set(TEST_SOURCE_FILES
unittests/test_statistics.cpp
unittests/test_request.cpp
unittests/test_request_handler_file_write.cpp
unittests/test_statistics_sender_influx_db.cpp
)
#
set(TEST_LIBRARIES "${TARGET_NAME};system_io")
......
......@@ -18,15 +18,15 @@ StatisticsToSend Statistics::PrepareStatisticsToSend() const noexcept {
stat.n_requests = nrequests_;
stat.data_volume = volume_counter_;
stat.elapsed_ms = GetTotalElapsedMs();
for (auto i=0;i<kNStatisticEntities;i++){
stat.entity_shares[i] = double(GetElapsedMs(StatisticEntity(i)))/stat.elapsed_ms;
for (auto i = 0; i < kNStatisticEntities; i++) {
stat.entity_shares[i] = double(GetElapsedMs(StatisticEntity(i))) / stat.elapsed_ms;
}
return stat;
}
uint64_t Statistics::GetTotalElapsedMs() const noexcept {
return std::chrono::duration_cast<std::chrono::milliseconds>
( high_resolution_clock::now() - last_timepoint_).count();
( high_resolution_clock::now() - last_timepoint_).count();
}
uint64_t Statistics::GetElapsedMs(StatisticEntity entity) const noexcept {
......@@ -50,7 +50,8 @@ void Statistics::IncreaseRequestCounter() noexcept {
nrequests_++;
}
Statistics::Statistics(unsigned int write_frequency) : statistics_sender__{new StatisticsSenderInfluxDb},write_interval_{write_frequency}{
Statistics::Statistics(unsigned int write_frequency) : statistics_sender__{new StatisticsSenderInfluxDb},
write_interval_{write_frequency} {
ResetStatistics();
}
......
......@@ -16,10 +16,10 @@ enum StatisticEntity : int {
};
struct StatisticsToSend {
double entity_shares[kNStatisticEntities];
uint64_t elapsed_ms;
uint64_t data_volume;
uint64_t n_requests;
double entity_shares[kNStatisticEntities];
uint64_t elapsed_ms;
uint64_t data_volume;
uint64_t n_requests;
};
class Statistics {
......
......@@ -8,7 +8,7 @@ namespace hidra2 {
struct StatisticsToSend;
class StatisticsSender {
public:
public:
virtual void SendStatistics(const StatisticsToSend& statistic) const noexcept = 0;
virtual ~StatisticsSender() = default;
};
......
#include "statistics_sender_influx_db.h"
#include "statistics.h"
#include "http_client/curl_http_client.h"
namespace hidra2 {
......@@ -7,5 +8,9 @@ void StatisticsSenderInfluxDb::SendStatistics(const StatisticsToSend& statistic)
}
StatisticsSenderInfluxDb::StatisticsSenderInfluxDb(): httpclient__{new CurlHttpClient} {
};
}
#ifndef HIDRA2_STATISTICS_SENDER_INFLUX_DB_H
#define HIDRA2_STATISTICS_SENDER_INFLUX_DB_H
#include "http_client/http_client.h"
#include "statistics_sender.h"
namespace hidra2 {
class StatisticsSenderInfluxDb : public StatisticsSender{
virtual void SendStatistics(const StatisticsToSend& statistic) const noexcept override;
class StatisticsSenderInfluxDb : public StatisticsSender {
public:
StatisticsSenderInfluxDb();
virtual void SendStatistics(const StatisticsToSend& statistic) const noexcept override;
std::unique_ptr<HttpClient> httpclient__;
};
}
......
......@@ -9,30 +9,30 @@
namespace hidra2 {
class MockStatistics : public hidra2::Statistics {
public:
void SendIfNeeded() noexcept override {
SendIfNeeded_t();
}
void IncreaseRequestCounter() noexcept override {
IncreaseRequestCounter_t();
}
void StartTimer(const hidra2::StatisticEntity &entity) noexcept override {
StartTimer_t(entity);
}
void IncreaseRequestDataVolume(uint64_t transferred_data_volume) noexcept override {
IncreaseRequestDataVolume_t(transferred_data_volume);
}
void StopTimer() noexcept override {
StopTimer_t();
}
MOCK_METHOD0(SendIfNeeded_t, void());
MOCK_METHOD0(IncreaseRequestCounter_t, void());
MOCK_METHOD0(StopTimer_t, void());
MOCK_METHOD1(IncreaseRequestDataVolume_t, void (uint64_t
transferred_data_volume));
MOCK_METHOD1(StartTimer_t, void(
const hidra2::StatisticEntity &entity));
public:
void SendIfNeeded() noexcept override {
SendIfNeeded_t();
}
void IncreaseRequestCounter() noexcept override {
IncreaseRequestCounter_t();
}
void StartTimer(const hidra2::StatisticEntity& entity) noexcept override {
StartTimer_t(entity);
}
void IncreaseRequestDataVolume(uint64_t transferred_data_volume) noexcept override {
IncreaseRequestDataVolume_t(transferred_data_volume);
}
void StopTimer() noexcept override {
StopTimer_t();
}
MOCK_METHOD0(SendIfNeeded_t, void());
MOCK_METHOD0(IncreaseRequestCounter_t, void());
MOCK_METHOD0(StopTimer_t, void());
MOCK_METHOD1(IncreaseRequestDataVolume_t, void (uint64_t
transferred_data_volume));
MOCK_METHOD1(StartTimer_t, void(
const hidra2::StatisticEntity& entity));
};
......
......@@ -45,7 +45,7 @@ class MockReqestHandler : public hidra2::RequestHandler {
}
StatisticEntity GetStatisticEntity() const {
return StatisticEntity::kDisk;
return StatisticEntity::kDisk;
}
MOCK_CONST_METHOD1(ProcessRequest_t, ErrorInterface * (const Request& request));
......@@ -135,7 +135,7 @@ TEST_F(RequestTests, HandleProcessesRequests) {
MockReqestHandler mock_request_handler;
MockStatistics mock_statistics;
auto stat = std::unique_ptr<hidra2::Statistics>{&mock_statistics};
auto stat = std::unique_ptr<hidra2::Statistics> {&mock_statistics};
EXPECT_CALL(mock_request_handler, ProcessRequest_t(_)).WillOnce(
Return(nullptr)
......
......@@ -32,25 +32,25 @@ TEST(StatisticTestsConstructor, Constructor) {
class MockStatisticsSender: public StatisticsSender {
public:
void SendStatistics(const StatisticsToSend& statistics) const noexcept override {
SendStatistics_t(statistics);
}
MOCK_CONST_METHOD1(SendStatistics_t, void (const StatisticsToSend&));
public:
void SendStatistics(const StatisticsToSend& statistics) const noexcept override {
SendStatistics_t(statistics);
}
MOCK_CONST_METHOD1(SendStatistics_t, void (const StatisticsToSend&));
};
class StatisticTests : public Test {
public:
Statistics statistics{0};
void TestTimer(const StatisticEntity& entity);
MockStatisticsSender mock_statistics_sender;
void SetUp() override {
statistics.statistics_sender__.reset(&mock_statistics_sender);
}
void TearDown() override {
statistics.statistics_sender__.release();
}
StatisticsToSend ExtractStat();
public:
Statistics statistics{0};
void TestTimer(const StatisticEntity& entity);
MockStatisticsSender mock_statistics_sender;
void SetUp() override {
statistics.statistics_sender__.reset(&mock_statistics_sender);
}
void TearDown() override {
statistics.statistics_sender__.release();
}
StatisticsToSend ExtractStat();
};
......@@ -59,7 +59,7 @@ ACTION_P(SaveArg1ToSendStat, value) {
value->n_requests = resp.n_requests;
value->data_volume = resp.data_volume;
value->elapsed_ms = resp.elapsed_ms;
for (int i=0;i<hidra2::kNStatisticEntities;i++){
for (int i = 0; i < hidra2::kNStatisticEntities; i++) {
value->entity_shares[i] = resp.entity_shares[i];
}
......@@ -72,12 +72,12 @@ StatisticsToSend StatisticTests::ExtractStat() {
stat.elapsed_ms = 0;
stat.n_requests = 0;
stat.data_volume = 0;
for (int i=0;i<hidra2::kNStatisticEntities;i++){
for (int i = 0; i < hidra2::kNStatisticEntities; i++) {
stat.entity_shares[i] = 0.0;
}
EXPECT_CALL(mock_statistics_sender,SendStatistics_t(_)).
WillOnce(SaveArg1ToSendStat(&stat));
EXPECT_CALL(mock_statistics_sender, SendStatistics_t(_)).
WillOnce(SaveArg1ToSendStat(&stat));
statistics.SendIfNeeded();
return stat;
......@@ -130,7 +130,7 @@ TEST_F(StatisticTests, DataVolumeZeroAtInit) {
ASSERT_THAT(stat.data_volume, Eq(0));
}
void StatisticTests::TestTimer(const StatisticEntity& entity){
void StatisticTests::TestTimer(const StatisticEntity& entity) {
statistics.StartTimer(entity);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
......@@ -158,7 +158,7 @@ TEST_F(StatisticTests, TimerForDisk) {
TEST_F(StatisticTests, SendStaticsDoesCallsSender) {
statistics.SetWriteInterval(1000);
EXPECT_CALL(mock_statistics_sender,SendStatistics_t(_)).Times(0);
EXPECT_CALL(mock_statistics_sender, SendStatistics_t(_)).Times(0);
statistics.SendIfNeeded();
}
......
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <unittests/MockIO.h>
#include "../src/statistics_sender_influx_db.h"
#include "../src/statistics_sender.h"
#include "http_client/curl_http_client.h"
using ::testing::Test;
using ::testing::Return;
using ::testing::_;
using ::testing::DoAll;
using ::testing::SetArgReferee;
using ::testing::Gt;
using ::testing::Eq;
using ::testing::Ne;
using ::testing::Mock;
using ::testing::NiceMock;
using ::testing::SaveArg;
using ::testing::SaveArgPointee;
using ::testing::InSequence;
using ::testing::SetArgPointee;
using hidra2::StatisticsSenderInfluxDb;
namespace {
TEST(SenderInfluxDb, Constructor) {
StatisticsSenderInfluxDb sender;
ASSERT_THAT(dynamic_cast<hidra2::CurlHttpClient*>(sender.httpclient__.get()), Ne(nullptr));
}
}
......@@ -18,5 +18,6 @@ endif()
add_subdirectory(worker)
add_subdirectory(curl_http_client)
add_subdirectory(producer_receiver)
\ No newline at end of file
add_subdirectory(curl_http_client_get)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment