forked from Brill-Power/thingsetplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAsioIpClientServer.cpp
More file actions
133 lines (118 loc) · 3.8 KB
/
TestAsioIpClientServer.cpp
File metadata and controls
133 lines (118 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (c) 2025 Brill Power.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "thingset++/ip/asio/ThingSetAsyncSocketClientTransport.hpp"
#include "thingset++/ip/asio/ThingSetAsyncSocketServerTransport.hpp"
#include "thingset++/ThingSetClient.hpp"
#include "thingset++/ThingSetServer.hpp"
#include "thingset++/ThingSetFunction.hpp"
#include "gtest/gtest.h"
#include <asio.hpp>
#include <thread>
using namespace ThingSet;
using namespace ThingSet::Ip::Async;
using namespace asio;
static std::array<uint8_t, 1024> rxBuffer;
static std::array<uint8_t, 1024> txBuffer;
#define ASIO_TEST(Name, Body) \
TEST(AsioIpClientServer, Name) \
{ \
ThingSetReadWriteProperty totalVoltage { 0x300, 0, "totalVoltage", 24.0f }; \
ThingSetUserFunction<0x1000, 0x0, "xAddNumber", int, int, int> doSomething([](auto x, auto y) { return x + y; }); \
\
io_context serverContext(1); \
ThingSetAsyncSocketServerTransport serverTransport(serverContext); \
auto server = ThingSetServerBuilder::build(serverTransport); \
server.listen(); \
std::thread serverThread([&]() \
{ \
serverContext.run_for(chrono::seconds(5)); \
}); \
\
io_context clientContext(1); \
auto endpoint = asio::ip::tcp::endpoint(asio::ip::address_v4::loopback(), 9001); \
ThingSetAsyncSocketClientTransport clientTransport(clientContext, endpoint); \
auto client = ThingSetClient(clientTransport, rxBuffer, txBuffer); \
bool clientRanSuccessfully = false; \
std::thread clientThread([&]() \
{ \
std::this_thread::sleep_for(std::chrono::milliseconds(125)); \
ASSERT_TRUE(client.connect()); \
Body \
clientRanSuccessfully = true; \
serverContext.stop(); \
}); \
\
clientThread.join(); \
serverThread.join(); \
\
ASSERT_TRUE(clientRanSuccessfully); \
}
ASIO_TEST(GetFloatById,
float tv;
ASSERT_TRUE(client.get(0x300, tv));
ASSERT_EQ(24.0f, tv);
)
ASIO_TEST(GetFloatByName,
float tv;
ASSERT_TRUE(client.get("totalVoltage", tv));
ASSERT_EQ(24.0f, tv);
)
ASIO_TEST(NotFoundById,
float tv;
auto result = client.get(0x1300, tv);
ASSERT_FALSE(result);
ASSERT_EQ(ThingSetStatusCode::notFound, result.code());
)
ASIO_TEST(NotFoundByName,
float tv;
auto result = client.get("notTotalVoltage", tv);
ASSERT_FALSE(result);
ASSERT_EQ(ThingSetStatusCode::notFound, result.code());
)
ASIO_TEST(GetFunction,
int result;
ASSERT_FALSE(client.get(0x1000, result));
)
ASIO_TEST(ExecFunction,
int value;
auto result = client.exec(0x1000, &value, 2, 3);
ASSERT_TRUE(result);
ASSERT_EQ(ThingSetStatusCode::changed, result.code());
ASSERT_EQ(5, value);
)
ASIO_TEST(FetchFunctionParameters,
std::vector<int> ids;
ASSERT_TRUE(client.fetch(0x1000, ids));
ASSERT_EQ(2, ids.size());
ASSERT_GE(ids[0], 0x1001);
ASSERT_GE(ids[1], 0x1001);
std::vector<std::string> names;
ASSERT_TRUE(client.fetch(std::string("xAddNumber"), names));
ASSERT_EQ(2, names.size());
ASSERT_STREQ("xAddNumberi32", names[0].substr(0, 13).c_str());
)
ASIO_TEST(UpdateFloatByName,
auto result = client.update("totalVoltage", 25.0f);
ASSERT_TRUE(result);
ASSERT_EQ(ThingSetStatusCode::changed, result.code());
ASSERT_EQ(25.0, totalVoltage.getValue());
)
ASIO_TEST(UpdateFloatById,
auto result = client.update(0x300, 26.0f);
ASSERT_TRUE(result);
ASSERT_EQ(ThingSetStatusCode::changed, result.code());
ASSERT_EQ(26.0, totalVoltage.getValue());
)
ASIO_TEST(CannotUpdateFunctionByName,
auto result = client.update("xAddNumber", 26.0f);
ASSERT_FALSE(result);
ASSERT_EQ(ThingSetStatusCode::badRequest, result.code());
)
ASIO_TEST(CannotUpdateFunctionById,
auto result = client.update(0x1000, 26.0f);
ASSERT_FALSE(result);
ASSERT_EQ(ThingSetStatusCode::badRequest, result.code());
)