forked from Brill-Power/thingsetplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSocketIpClientServer.cpp
More file actions
62 lines (56 loc) · 1.79 KB
/
TestSocketIpClientServer.cpp
File metadata and controls
62 lines (56 loc) · 1.79 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
/*
* Copyright (c) 2025 Brill Power.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "thingset++/ip/sockets/ThingSetSocketClientTransport.hpp"
#include "thingset++/ip/sockets/ThingSetSocketServerTransport.hpp"
#include "thingset++/ThingSetClient.hpp"
#include "thingset++/ThingSetServer.hpp"
#include "thingset++/ThingSetFunction.hpp"
#include "gtest/gtest.h"
#include <thread>
using namespace ThingSet;
using namespace ThingSet::Ip::Sockets;
static std::array<uint8_t, 1024> rxBuffer;
static std::array<uint8_t, 1024> txBuffer;
#define SOCKET_TEST(Name, Body) \
TEST(SocketIpClientServer, Name) \
{ \
ThingSetReadWriteProperty<float> totalVoltage { 0x300, 0, "totalVoltage", 24.0f }; \
ThingSetUserFunction<0x1000, 0x0, "xAddNumber", int, int, int> doSomething([](auto x, auto y) { return x + y; }); \
\
ThingSetSocketServerTransport serverTransport; \
auto server = ThingSetServerBuilder::build(serverTransport); \
server.listen(); \
\
auto endpoint = "127.0.0.1"; \
ThingSetSocketClientTransport clientTransport(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; \
}); \
\
clientThread.join(); \
\
ASSERT_TRUE(clientRanSuccessfully); \
}
SOCKET_TEST(GetFloat,
float tv;
ASSERT_TRUE(client.get(0x300, tv));
ASSERT_EQ(24.0f, tv);
)
SOCKET_TEST(ExecFunction,
int result;
ASSERT_TRUE(client.exec(0x1000, &result, 2, 3));
ASSERT_EQ(5, result);
)
SOCKET_TEST(UpdateFloat,
ASSERT_TRUE(client.update("totalVoltage", 25.0f));
ASSERT_EQ(25.0, totalVoltage.getValue());
)