Skip to content

Commit 1df030f

Browse files
Fix CI: clang-tidy const warning, stabilise timing-sensitive tests
1 parent f536179 commit 1df030f

3 files changed

Lines changed: 4 additions & 52 deletions

File tree

src/util/network/sock_t.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace util {
7575
< std::forward_as_tuple(ntohl(b.ipv4.sin_addr.s_addr), ntohs(b.ipv4.sin_port));
7676
}
7777
if (a.sock.sa_family == AF_INET6) {
78-
int cmp = std::memcmp(&a.ipv6.sin6_addr, &b.ipv6.sin6_addr, sizeof(in6_addr));
78+
const int cmp = std::memcmp(&a.ipv6.sin6_addr, &b.ipv6.sin6_addr, sizeof(in6_addr));
7979
if (cmp != 0) {
8080
return cmp < 0;
8181
}

tests/tests/nuclearnet/Discovery.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,19 +156,19 @@ SCENARIO("Discovery check_timeouts removes stale peers", "[nuclearnet][discovery
156156
}
157157

158158
SCENARIO("Discovery touch_peer resets timeout", "[nuclearnet][discovery]") {
159-
Discovery disc(std::chrono::milliseconds(50));
159+
Discovery disc(std::chrono::milliseconds(200));
160160

161161
sock_t peer_addr = make_addr(0x0A000001, 5000);
162162

163163
auto announce = Discovery::build_announce_packet("peer_a", {});
164164
disc.process_announce(peer_addr, announce.data(), announce.size());
165165

166166
// Wait part of the timeout, then touch
167-
std::this_thread::sleep_for(std::chrono::milliseconds(30));
167+
std::this_thread::sleep_for(std::chrono::milliseconds(120));
168168
disc.touch_peer(peer_addr);
169169

170170
// Wait another partial timeout (total would have expired without touch)
171-
std::this_thread::sleep_for(std::chrono::milliseconds(30));
171+
std::this_thread::sleep_for(std::chrono::milliseconds(120));
172172

173173
auto removed = disc.check_timeouts();
174174
REQUIRE(removed.empty());

tests/tests/nuclearnet/Integration.cpp

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525

2626
#include <catch2/catch_test_macros.hpp>
2727
#include <algorithm>
28-
#include <atomic>
2928
#include <chrono>
30-
#include <condition_variable>
3129
#include <cstdint>
3230
#include <functional>
3331
#include <mutex>
@@ -78,38 +76,8 @@ bool wait_for(const std::function<bool()>& predicate,
7876
struct NetworkPair {
7977
NUClearNet a;
8078
NUClearNet b;
81-
std::thread worker_a;
82-
std::thread worker_b;
83-
std::atomic<bool> running{false};
84-
85-
void start() {
86-
running = true;
87-
worker_a = std::thread([this] {
88-
while (running) {
89-
a.process();
90-
std::this_thread::sleep_for(5ms);
91-
}
92-
});
93-
worker_b = std::thread([this] {
94-
while (running) {
95-
b.process();
96-
std::this_thread::sleep_for(5ms);
97-
}
98-
});
99-
}
100-
101-
void stop() {
102-
running = false;
103-
if (worker_a.joinable()) {
104-
worker_a.join();
105-
}
106-
if (worker_b.joinable()) {
107-
worker_b.join();
108-
}
109-
}
11079

11180
~NetworkPair() {
112-
stop();
11381
a.shutdown();
11482
b.shutdown();
11583
}
@@ -140,50 +108,41 @@ SCENARIO("Two NUClearNet instances discover and exchange messages", "[nuclearnet
140108
net.b.set_subscriptions({HASH_A});
141109

142110
std::mutex mutex;
143-
std::condition_variable cv;
144111
std::vector<std::string> join_events;
145112
std::vector<std::string> leave_events;
146113
std::vector<std::pair<std::string, std::vector<uint8_t>>> received;
147114

148115
net.a.set_join_callback([&](const PeerInfo& peer) {
149116
std::lock_guard<std::mutex> lock(mutex);
150117
join_events.push_back("a:" + peer.name);
151-
cv.notify_all();
152118
});
153119
net.b.set_join_callback([&](const PeerInfo& peer) {
154120
std::lock_guard<std::mutex> lock(mutex);
155121
join_events.push_back("b:" + peer.name);
156-
cv.notify_all();
157122
});
158123

159124
net.a.set_leave_callback([&](const PeerInfo& peer) {
160125
std::lock_guard<std::mutex> lock(mutex);
161126
leave_events.push_back("a:" + peer.name);
162-
cv.notify_all();
163127
});
164128
net.b.set_leave_callback([&](const PeerInfo& peer) {
165129
std::lock_guard<std::mutex> lock(mutex);
166130
leave_events.push_back("b:" + peer.name);
167-
cv.notify_all();
168131
});
169132

170133
net.a.set_packet_callback([&](const sock_t&, const std::string& peer_name, uint64_t hash, bool reliable,
171134
std::vector<uint8_t>&& payload) {
172135
std::lock_guard<std::mutex> lock(mutex);
173136
received.emplace_back("a:" + peer_name + ":" + std::to_string(hash) + ":" + (reliable ? "1" : "0"),
174137
std::move(payload));
175-
cv.notify_all();
176138
});
177139
net.b.set_packet_callback([&](const sock_t&, const std::string& peer_name, uint64_t hash, bool reliable,
178140
std::vector<uint8_t>&& payload) {
179141
std::lock_guard<std::mutex> lock(mutex);
180142
received.emplace_back("b:" + peer_name + ":" + std::to_string(hash) + ":" + (reliable ? "1" : "0"),
181143
std::move(payload));
182-
cv.notify_all();
183144
});
184145

185-
net.start();
186-
187146
REQUIRE(wait_for([&] {
188147
std::lock_guard<std::mutex> lock(mutex);
189148
return std::find(join_events.begin(), join_events.end(), "a:bravo") != join_events.end()
@@ -234,10 +193,7 @@ SCENARIO("Two NUClearNet instances discover and exchange messages", "[nuclearnet
234193
return std::find(leave_events.begin(), leave_events.end(), "a:bravo") != leave_events.end();
235194
}, 5s, [&] {
236195
net.a.process();
237-
net.b.process();
238196
}));
239-
240-
net.stop();
241197
}
242198

243199
SCENARIO("NUClearNet handles bidirectional reliable traffic", "[nuclearnet][integration]") {
@@ -276,8 +232,6 @@ SCENARIO("NUClearNet handles bidirectional reliable traffic", "[nuclearnet][inte
276232
b_received.push_back(std::move(payload));
277233
});
278234

279-
net.start();
280-
281235
REQUIRE(wait_for([&] {
282236
std::lock_guard<std::mutex> lock(mutex);
283237
return std::find(join_events.begin(), join_events.end(), "a:right") != join_events.end()
@@ -306,6 +260,4 @@ SCENARIO("NUClearNet handles bidirectional reliable traffic", "[nuclearnet][inte
306260
REQUIRE(a_received[0] == small_payload);
307261
REQUIRE(b_received[0] == large_payload);
308262
}
309-
310-
net.stop();
311263
}

0 commit comments

Comments
 (0)