Skip to content

Commit 40e573d

Browse files
authored
Fix ENet test duplication causing Windows link errors (#51)
1 parent 9116ba2 commit 40e573d

4 files changed

Lines changed: 118 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ third_party/EE/EngineDebug64DX11_pdb.zip-> third_party/EE/EngineDebug64DX11.pdb
1717

1818
## Building
1919

20-
CMake presets are provided for both platforms. Example commands:
20+
CMake presets are provided for both platforms. **The Linux presets use Clang,**
21+
as the third-party Titan Engine headers rely on MSVC extensions not supported
22+
by GCC. Example commands:
2123

2224
```bash
2325
cmake --preset linux-release # or windows-release

tests/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ add_executable(UnitTests
2525
MyClassTests.cpp
2626
SpdlogTest.cpp
2727
EnetTest.cpp
28+
ServiceHostTest.cpp
29+
${PROJECT_SOURCE_DIR}/apps/client/src/ConnectionToServer.cpp
2830
EE_boot_stubs.cpp
2931
${PROJECT_SOURCE_DIR}/libs/game/src/stdafx.cpp
3032
)
3133

34+
target_include_directories(UnitTests PRIVATE
35+
${PROJECT_SOURCE_DIR}/apps/client/src
36+
)
37+
3238
target_compile_definitions(UnitTests PRIVATE FMT_USE_FCNTL=0 SPDLOG_FORCE_COLOR=1)
3339

3440
# Suppress spdlog warnings triggered by -ffast-math when using clang

tests/EnetTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define ENET_IMPLEMENTATION
21
#include "enet.h"
32
#include "gtest/gtest.h"
43

tests/ServiceHostTest.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// clang-format off
2+
#include "gtest/gtest.h"
3+
#include <unordered_map>
4+
#include <cfloat>
5+
#include "enet.h"
6+
#if defined(__linux__)
7+
#pragma push_macro("LOCK_READ")
8+
#pragma push_macro("LOCK_WRITE")
9+
#undef LOCK_READ
10+
#undef LOCK_WRITE
11+
#endif
12+
#include "stdafx.h"
13+
#include "@@headers.h"
14+
#if defined(__linux__)
15+
#pragma pop_macro("LOCK_WRITE")
16+
#pragma pop_macro("LOCK_READ")
17+
#endif
18+
// clang-format on
19+
20+
extern ENetHost *gClient;
21+
extern ENetPeer *gClientPeer;
22+
extern bool gEnetReady;
23+
extern int gMyId;
24+
extern std::unordered_map<int, Vec2> gOtherDots;
25+
26+
struct DotPacket {
27+
int id;
28+
Flt x;
29+
Flt y;
30+
};
31+
32+
void ServiceHost(ENetHost *host);
33+
34+
TEST(ServiceHost, UpdatesDotMap) {
35+
ASSERT_EQ(enet_initialize(), 0);
36+
37+
ENetAddress addr{};
38+
addr.host = ENET_HOST_ANY;
39+
addr.port = 0; // let OS choose
40+
ENetHost *server = enet_host_create(&addr, 1, 1, 0, 0);
41+
ASSERT_NE(server, nullptr);
42+
43+
gClient = enet_host_create(nullptr, 1, 1, 0, 0);
44+
ASSERT_NE(gClient, nullptr);
45+
46+
ENetAddress connectAddr = addr;
47+
enet_address_set_host(&connectAddr, "127.0.0.1");
48+
connectAddr.port = server->address.port;
49+
50+
gClientPeer = enet_host_connect(gClient, &connectAddr, 1, 0);
51+
ASSERT_NE(gClientPeer, nullptr);
52+
53+
ENetEvent event;
54+
gEnetReady = false;
55+
gMyId = -1;
56+
gOtherDots.clear();
57+
58+
for (int i = 0; i < 100 && !gEnetReady; i++) {
59+
ServiceHost(gClient);
60+
while (enet_host_service(server, &event, 0) > 0) {
61+
}
62+
}
63+
ASSERT_TRUE(gEnetReady);
64+
65+
DotPacket pkt{42, 1.0f, 2.0f};
66+
ENetPacket *p =
67+
enet_packet_create(&pkt, sizeof(pkt), ENET_PACKET_FLAG_RELIABLE);
68+
enet_host_broadcast(server, 0, p);
69+
enet_host_flush(server);
70+
71+
bool received = false;
72+
for (int i = 0; i < 100 && !received; i++) {
73+
ServiceHost(gClient);
74+
while (enet_host_service(server, &event, 0) > 0) {
75+
}
76+
if (gOtherDots.find(42) != gOtherDots.end())
77+
received = true;
78+
}
79+
ASSERT_TRUE(received);
80+
auto it = gOtherDots.find(42);
81+
ASSERT_NE(it, gOtherDots.end());
82+
EXPECT_FLOAT_EQ(it->second.x, 1.0f);
83+
EXPECT_FLOAT_EQ(it->second.y, 2.0f);
84+
EXPECT_EQ(gMyId, 42);
85+
86+
DotPacket rem{42, FLT_MAX, FLT_MAX};
87+
ENetPacket *p2 =
88+
enet_packet_create(&rem, sizeof(rem), ENET_PACKET_FLAG_RELIABLE);
89+
enet_host_broadcast(server, 0, p2);
90+
enet_host_flush(server);
91+
92+
for (int i = 0; i < 100 && gOtherDots.find(42) != gOtherDots.end(); i++) {
93+
ServiceHost(gClient);
94+
while (enet_host_service(server, &event, 0) > 0) {
95+
}
96+
}
97+
EXPECT_EQ(gOtherDots.count(42), 0u);
98+
99+
enet_peer_disconnect(gClientPeer, 0);
100+
for (int i = 0; i < 10; i++) {
101+
ServiceHost(gClient);
102+
while (enet_host_service(server, &event, 0) > 0) {
103+
}
104+
}
105+
106+
enet_host_destroy(gClient);
107+
enet_host_destroy(server);
108+
enet_deinitialize();
109+
}

0 commit comments

Comments
 (0)