-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
229 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,113 @@ | ||
#include "xapi/internals/Connection.hpp" | ||
#include "xapi/internals/Exceptions.hpp" | ||
#include <gtest/gtest.h> | ||
#include <string> | ||
#include <vector> | ||
|
||
using namespace xapi; | ||
|
||
class ConnectionTest : public testing::Test | ||
{ | ||
protected: | ||
void SetUp() override | ||
{ | ||
} | ||
|
||
void TearDown() override | ||
{ | ||
} | ||
|
||
public: | ||
boost::asio::io_context &getIoContext() | ||
{ | ||
return m_context; | ||
} | ||
|
||
template <typename Awaitable> auto runAwaitable(Awaitable &&awaitable) | ||
{ | ||
std::exception_ptr eptr; | ||
using result_type = typename Awaitable::value_type; | ||
result_type result; | ||
|
||
boost::asio::co_spawn( | ||
m_context, | ||
[&]() -> boost::asio::awaitable<void> { | ||
try | ||
{ | ||
result = co_await std::forward<Awaitable>(awaitable); | ||
} | ||
catch (...) | ||
{ | ||
eptr = std::current_exception(); | ||
} | ||
}, | ||
boost::asio::detached); | ||
|
||
m_context.run(); | ||
|
||
if (eptr) | ||
{ | ||
std::rethrow_exception(eptr); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
template <typename Awaitable> auto runAwaitableVoid(Awaitable &&awaitable) | ||
{ | ||
std::exception_ptr eptr; | ||
boost::asio::co_spawn( | ||
m_context, | ||
[&]() -> boost::asio::awaitable<void> { | ||
try | ||
{ | ||
co_await std::forward<Awaitable>(awaitable); | ||
} | ||
catch (...) | ||
{ | ||
eptr = std::current_exception(); | ||
} | ||
}, | ||
boost::asio::detached); | ||
|
||
m_context.run(); | ||
|
||
if (eptr) | ||
{ | ||
std::rethrow_exception(eptr); | ||
} | ||
} | ||
|
||
private: | ||
boost::asio::io_context m_context; | ||
}; | ||
|
||
TEST_F(ConnectionTest, connect_exception) | ||
{ | ||
internals::Connection connection(getIoContext()); | ||
EXPECT_THROW(runAwaitableVoid(connection.connect(boost::url("wss://localhost:99999"))), exception::ConnectionClosed); | ||
} | ||
|
||
TEST_F(ConnectionTest, disconnect_exception) | ||
{ | ||
internals::Connection connection(getIoContext()); | ||
EXPECT_NO_THROW(runAwaitableVoid(connection.disconnect())); | ||
} | ||
|
||
TEST_F(ConnectionTest, makeRequest_exception) | ||
{ | ||
internals::Connection connection(getIoContext()); | ||
|
||
boost::json::object command; | ||
command["command"] = "invalid"; | ||
EXPECT_THROW(runAwaitableVoid(connection.makeRequest(command)), exception::ConnectionClosed); | ||
} | ||
|
||
TEST_F(ConnectionTest, waitResponse_exception) | ||
{ | ||
internals::Connection connection(getIoContext()); | ||
|
||
boost::json::object result; | ||
EXPECT_THROW(result = runAwaitable(connection.waitResponse()), exception::ConnectionClosed); | ||
EXPECT_TRUE(result.empty()); | ||
} | ||
#include "xapi/internals/Connection.hpp" | ||
#include "xapi/internals/Exceptions.hpp" | ||
#include <gtest/gtest.h> | ||
#include <string> | ||
#include <vector> | ||
|
||
using namespace xapi; | ||
|
||
class ConnectionTest : public testing::Test | ||
{ | ||
protected: | ||
void SetUp() override | ||
{ | ||
} | ||
|
||
void TearDown() override | ||
{ | ||
} | ||
|
||
public: | ||
boost::asio::io_context &getIoContext() | ||
{ | ||
return m_context; | ||
} | ||
|
||
template <typename Awaitable> auto runAwaitable(Awaitable &&awaitable) | ||
{ | ||
std::exception_ptr eptr; | ||
using result_type = typename Awaitable::value_type; | ||
result_type result; | ||
|
||
boost::asio::co_spawn( | ||
m_context, | ||
[&]() -> boost::asio::awaitable<void> { | ||
try | ||
{ | ||
result = co_await std::forward<Awaitable>(awaitable); | ||
} | ||
catch (...) | ||
{ | ||
eptr = std::current_exception(); | ||
} | ||
}, | ||
boost::asio::detached); | ||
|
||
m_context.run(); | ||
|
||
if (eptr) | ||
{ | ||
std::rethrow_exception(eptr); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
template <typename Awaitable> auto runAwaitableVoid(Awaitable &&awaitable) | ||
{ | ||
std::exception_ptr eptr; | ||
boost::asio::co_spawn( | ||
m_context, | ||
[&]() -> boost::asio::awaitable<void> { | ||
try | ||
{ | ||
co_await std::forward<Awaitable>(awaitable); | ||
} | ||
catch (...) | ||
{ | ||
eptr = std::current_exception(); | ||
} | ||
}, | ||
boost::asio::detached); | ||
|
||
m_context.run(); | ||
|
||
if (eptr) | ||
{ | ||
std::rethrow_exception(eptr); | ||
} | ||
} | ||
|
||
private: | ||
boost::asio::io_context m_context; | ||
}; | ||
|
||
TEST_F(ConnectionTest, connect_exception) | ||
{ | ||
internals::Connection connection(getIoContext()); | ||
EXPECT_THROW(runAwaitableVoid(connection.connect(boost::url("wss://localhost:99999"))), exception::ConnectionClosed); | ||
} | ||
|
||
TEST_F(ConnectionTest, disconnect_exception) | ||
{ | ||
internals::Connection connection(getIoContext()); | ||
EXPECT_NO_THROW(runAwaitableVoid(connection.disconnect())); | ||
} | ||
|
||
TEST_F(ConnectionTest, makeRequest_exception) | ||
{ | ||
internals::Connection connection(getIoContext()); | ||
|
||
boost::json::object command; | ||
command["command"] = "invalid"; | ||
EXPECT_THROW(runAwaitableVoid(connection.makeRequest(command)), exception::ConnectionClosed); | ||
} | ||
|
||
TEST_F(ConnectionTest, waitResponse_exception) | ||
{ | ||
internals::Connection connection(getIoContext()); | ||
|
||
boost::json::object result; | ||
EXPECT_THROW(result = runAwaitable(connection.waitResponse()), exception::ConnectionClosed); | ||
EXPECT_TRUE(result.empty()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,74 @@ | ||
# TARGET COMPILE OPTIONS ======================================== | ||
set(COMMON_FLAGS | ||
-Wall | ||
-Werror | ||
-Wpedantic | ||
-Wextra | ||
-march=native | ||
) | ||
|
||
if(CMAKE_BUILD_TYPE STREQUAL "Release") | ||
list(APPEND COMMON_FLAGS -O3) | ||
else() | ||
list(APPEND COMMON_FLAGS -O0 -g) | ||
endif() | ||
|
||
# XAPI SETUP ================================================ | ||
|
||
set(XAPI_PUBLIC_H | ||
Enums.hpp | ||
XStationClient.hpp | ||
XStationClientStream.hpp | ||
Xapi.hpp | ||
) | ||
|
||
set(XAPI_SOURCES | ||
${XAPI_PUBLIC_H} | ||
XStationClient.cpp | ||
XStationClientStream.cpp | ||
) | ||
|
||
add_subdirectory(internals) | ||
|
||
add_library(Xapi SHARED ${XAPI_SOURCES}) | ||
target_compile_options(Xapi PRIVATE ${COMMON_FLAGS}) | ||
target_link_libraries(Xapi PRIVATE XapiConnection) | ||
|
||
# LIBRARY SETUP OPTIONS ======================================== | ||
include(GNUInstallDirs) | ||
|
||
target_include_directories(Xapi | ||
PUBLIC | ||
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>" | ||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" | ||
) | ||
|
||
install(TARGETS Xapi | ||
EXPORT XapiTargets | ||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} | ||
) | ||
|
||
install(FILES ${XAPI_PUBLIC_H} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xapi) | ||
|
||
install(EXPORT XapiTargets | ||
FILE XapiTargets.cmake | ||
NAMESPACE Xapi:: | ||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Xapi | ||
) | ||
|
||
# FIND PACKAGE ORCHESTRATION ==================================== | ||
include(CMakePackageConfigHelpers) | ||
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in | ||
"${CMAKE_CURRENT_BINARY_DIR}/XapiConfig.cmake" | ||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Xapi | ||
) | ||
|
||
install(FILES | ||
"${CMAKE_CURRENT_BINARY_DIR}/XapiConfig.cmake" | ||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Xapi | ||
) | ||
|
||
# TARGET COMPILE OPTIONS ======================================== | ||
set(COMMON_FLAGS | ||
-Wall | ||
-Werror | ||
-Wpedantic | ||
-Wextra | ||
-march=native | ||
) | ||
|
||
if(CMAKE_BUILD_TYPE STREQUAL "Release") | ||
list(APPEND COMMON_FLAGS -O3) | ||
else() | ||
list(APPEND COMMON_FLAGS -O0 -g) | ||
endif() | ||
|
||
# XAPI SETUP ================================================ | ||
|
||
set(XAPI_PUBLIC_H | ||
Enums.hpp | ||
XStationClient.hpp | ||
XStationClientStream.hpp | ||
Xapi.hpp | ||
) | ||
|
||
set(XAPI_SOURCES | ||
${XAPI_PUBLIC_H} | ||
XStationClient.cpp | ||
XStationClientStream.cpp | ||
) | ||
|
||
add_subdirectory(internals) | ||
|
||
add_library(Xapi SHARED ${XAPI_SOURCES}) | ||
target_compile_options(Xapi PRIVATE ${COMMON_FLAGS}) | ||
target_link_libraries(Xapi PRIVATE XapiConnection) | ||
target_include_directories(Xapi PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/internals) | ||
|
||
# LIBRARY SETUP OPTIONS ======================================== | ||
include(GNUInstallDirs) | ||
|
||
target_include_directories(Xapi | ||
PUBLIC | ||
"$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>" | ||
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" | ||
) | ||
|
||
install(TARGETS Xapi | ||
EXPORT XapiTargets | ||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} | ||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} | ||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} | ||
) | ||
|
||
install(FILES ${XAPI_PUBLIC_H} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/xapi) | ||
|
||
install(EXPORT XapiTargets | ||
FILE XapiTargets.cmake | ||
NAMESPACE Xapi:: | ||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Xapi | ||
) | ||
|
||
# FIND PACKAGE ORCHESTRATION ==================================== | ||
include(CMakePackageConfigHelpers) | ||
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in | ||
"${CMAKE_CURRENT_BINARY_DIR}/XapiConfig.cmake" | ||
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Xapi | ||
) | ||
|
||
install(FILES | ||
"${CMAKE_CURRENT_BINARY_DIR}/XapiConfig.cmake" | ||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/Xapi | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,3 +70,4 @@ enum class PeriodCode | |
}; | ||
|
||
} // namespace xapi | ||
|
Oops, something went wrong.