|
1 | | -#include "xapi/internals/Connection.hpp" |
2 | | -#include "xapi/internals/Exceptions.hpp" |
3 | | -#include <gtest/gtest.h> |
4 | | -#include <string> |
5 | | -#include <vector> |
6 | | - |
7 | | -using namespace xapi; |
8 | | - |
9 | | -class ConnectionTest : public testing::Test |
10 | | -{ |
11 | | - protected: |
12 | | - void SetUp() override |
13 | | - { |
14 | | - } |
15 | | - |
16 | | - void TearDown() override |
17 | | - { |
18 | | - } |
19 | | - |
20 | | - public: |
21 | | - boost::asio::io_context &getIoContext() |
22 | | - { |
23 | | - return m_context; |
24 | | - } |
25 | | - |
26 | | - template <typename Awaitable> auto runAwaitable(Awaitable &&awaitable) |
27 | | - { |
28 | | - std::exception_ptr eptr; |
29 | | - using result_type = typename Awaitable::value_type; |
30 | | - result_type result; |
31 | | - |
32 | | - boost::asio::co_spawn( |
33 | | - m_context, |
34 | | - [&]() -> boost::asio::awaitable<void> { |
35 | | - try |
36 | | - { |
37 | | - result = co_await std::forward<Awaitable>(awaitable); |
38 | | - } |
39 | | - catch (...) |
40 | | - { |
41 | | - eptr = std::current_exception(); |
42 | | - } |
43 | | - }, |
44 | | - boost::asio::detached); |
45 | | - |
46 | | - m_context.run(); |
47 | | - |
48 | | - if (eptr) |
49 | | - { |
50 | | - std::rethrow_exception(eptr); |
51 | | - } |
52 | | - |
53 | | - return result; |
54 | | - } |
55 | | - |
56 | | - template <typename Awaitable> auto runAwaitableVoid(Awaitable &&awaitable) |
57 | | - { |
58 | | - std::exception_ptr eptr; |
59 | | - boost::asio::co_spawn( |
60 | | - m_context, |
61 | | - [&]() -> boost::asio::awaitable<void> { |
62 | | - try |
63 | | - { |
64 | | - co_await std::forward<Awaitable>(awaitable); |
65 | | - } |
66 | | - catch (...) |
67 | | - { |
68 | | - eptr = std::current_exception(); |
69 | | - } |
70 | | - }, |
71 | | - boost::asio::detached); |
72 | | - |
73 | | - m_context.run(); |
74 | | - |
75 | | - if (eptr) |
76 | | - { |
77 | | - std::rethrow_exception(eptr); |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - private: |
82 | | - boost::asio::io_context m_context; |
83 | | -}; |
84 | | - |
85 | | -TEST_F(ConnectionTest, connect_exception) |
86 | | -{ |
87 | | - internals::Connection connection(getIoContext()); |
88 | | - EXPECT_THROW(runAwaitableVoid(connection.connect(boost::url("wss://localhost:99999"))), exception::ConnectionClosed); |
89 | | -} |
90 | | - |
91 | | -TEST_F(ConnectionTest, disconnect_exception) |
92 | | -{ |
93 | | - internals::Connection connection(getIoContext()); |
94 | | - EXPECT_NO_THROW(runAwaitableVoid(connection.disconnect())); |
95 | | -} |
96 | | - |
97 | | -TEST_F(ConnectionTest, makeRequest_exception) |
98 | | -{ |
99 | | - internals::Connection connection(getIoContext()); |
100 | | - |
101 | | - boost::json::object command; |
102 | | - command["command"] = "invalid"; |
103 | | - EXPECT_THROW(runAwaitableVoid(connection.makeRequest(command)), exception::ConnectionClosed); |
104 | | -} |
105 | | - |
106 | | -TEST_F(ConnectionTest, waitResponse_exception) |
107 | | -{ |
108 | | - internals::Connection connection(getIoContext()); |
109 | | - |
110 | | - boost::json::object result; |
111 | | - EXPECT_THROW(result = runAwaitable(connection.waitResponse()), exception::ConnectionClosed); |
112 | | - EXPECT_TRUE(result.empty()); |
113 | | -} |
| 1 | +#include "xapi/internals/Connection.hpp" |
| 2 | +#include "xapi/internals/Exceptions.hpp" |
| 3 | +#include <gtest/gtest.h> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +using namespace xapi; |
| 8 | + |
| 9 | +class ConnectionTest : public testing::Test |
| 10 | +{ |
| 11 | + protected: |
| 12 | + void SetUp() override |
| 13 | + { |
| 14 | + } |
| 15 | + |
| 16 | + void TearDown() override |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + public: |
| 21 | + boost::asio::io_context &getIoContext() |
| 22 | + { |
| 23 | + return m_context; |
| 24 | + } |
| 25 | + |
| 26 | + template <typename Awaitable> auto runAwaitable(Awaitable &&awaitable) |
| 27 | + { |
| 28 | + std::exception_ptr eptr; |
| 29 | + using result_type = typename Awaitable::value_type; |
| 30 | + result_type result; |
| 31 | + |
| 32 | + boost::asio::co_spawn( |
| 33 | + m_context, |
| 34 | + [&]() -> boost::asio::awaitable<void> { |
| 35 | + try |
| 36 | + { |
| 37 | + result = co_await std::forward<Awaitable>(awaitable); |
| 38 | + } |
| 39 | + catch (...) |
| 40 | + { |
| 41 | + eptr = std::current_exception(); |
| 42 | + } |
| 43 | + }, |
| 44 | + boost::asio::detached); |
| 45 | + |
| 46 | + m_context.run(); |
| 47 | + |
| 48 | + if (eptr) |
| 49 | + { |
| 50 | + std::rethrow_exception(eptr); |
| 51 | + } |
| 52 | + |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + template <typename Awaitable> auto runAwaitableVoid(Awaitable &&awaitable) |
| 57 | + { |
| 58 | + std::exception_ptr eptr; |
| 59 | + boost::asio::co_spawn( |
| 60 | + m_context, |
| 61 | + [&]() -> boost::asio::awaitable<void> { |
| 62 | + try |
| 63 | + { |
| 64 | + co_await std::forward<Awaitable>(awaitable); |
| 65 | + } |
| 66 | + catch (...) |
| 67 | + { |
| 68 | + eptr = std::current_exception(); |
| 69 | + } |
| 70 | + }, |
| 71 | + boost::asio::detached); |
| 72 | + |
| 73 | + m_context.run(); |
| 74 | + |
| 75 | + if (eptr) |
| 76 | + { |
| 77 | + std::rethrow_exception(eptr); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private: |
| 82 | + boost::asio::io_context m_context; |
| 83 | +}; |
| 84 | + |
| 85 | +TEST_F(ConnectionTest, connect_exception) |
| 86 | +{ |
| 87 | + internals::Connection connection(getIoContext()); |
| 88 | + EXPECT_THROW(runAwaitableVoid(connection.connect(boost::url("wss://localhost:99999"))), exception::ConnectionClosed); |
| 89 | +} |
| 90 | + |
| 91 | +TEST_F(ConnectionTest, disconnect_exception) |
| 92 | +{ |
| 93 | + internals::Connection connection(getIoContext()); |
| 94 | + EXPECT_NO_THROW(runAwaitableVoid(connection.disconnect())); |
| 95 | +} |
| 96 | + |
| 97 | +TEST_F(ConnectionTest, makeRequest_exception) |
| 98 | +{ |
| 99 | + internals::Connection connection(getIoContext()); |
| 100 | + |
| 101 | + boost::json::object command; |
| 102 | + command["command"] = "invalid"; |
| 103 | + EXPECT_THROW(runAwaitableVoid(connection.makeRequest(command)), exception::ConnectionClosed); |
| 104 | +} |
| 105 | + |
| 106 | +TEST_F(ConnectionTest, waitResponse_exception) |
| 107 | +{ |
| 108 | + internals::Connection connection(getIoContext()); |
| 109 | + |
| 110 | + boost::json::object result; |
| 111 | + EXPECT_THROW(result = runAwaitable(connection.waitResponse()), exception::ConnectionClosed); |
| 112 | + EXPECT_TRUE(result.empty()); |
| 113 | +} |
0 commit comments