Skip to content

Commit

Permalink
Fix missing exception header
Browse files Browse the repository at this point in the history
  • Loading branch information
MPogotsky committed Jan 23, 2025
1 parent 8fa297a commit 9ebd246
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 227 deletions.
226 changes: 113 additions & 113 deletions test/TestConnection.cpp
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());
}
2 changes: 1 addition & 1 deletion test/TestXStationClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "MockConnection.hpp"
#include "xapi/Exceptions.hpp"
#include "xapi/internals/Exceptions.hpp"
#include "xapi/XStationClient.hpp"
#include <gtest/gtest.h>
#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion test/TestXStationClientStream.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "MockConnection.hpp"
#include "xapi/Exceptions.hpp"
#include "xapi/internals/Exceptions.hpp"
#include "xapi/XStationClientStream.hpp"
#include <gtest/gtest.h>
#include <string>
Expand Down
147 changes: 74 additions & 73 deletions xapi/CMakeLists.txt
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
)

1 change: 1 addition & 0 deletions xapi/Enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ enum class PeriodCode
};

} // namespace xapi

Loading

0 comments on commit 9ebd246

Please sign in to comment.