|
25 | 25 |
|
26 | 26 | #include <catch2/catch_test_macros.hpp> |
27 | 27 | #include <algorithm> |
28 | | -#include <atomic> |
29 | 28 | #include <chrono> |
30 | | -#include <condition_variable> |
31 | 29 | #include <cstdint> |
32 | 30 | #include <functional> |
33 | 31 | #include <mutex> |
@@ -78,38 +76,8 @@ bool wait_for(const std::function<bool()>& predicate, |
78 | 76 | struct NetworkPair { |
79 | 77 | NUClearNet a; |
80 | 78 | 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 | | - } |
110 | 79 |
|
111 | 80 | ~NetworkPair() { |
112 | | - stop(); |
113 | 81 | a.shutdown(); |
114 | 82 | b.shutdown(); |
115 | 83 | } |
@@ -140,50 +108,41 @@ SCENARIO("Two NUClearNet instances discover and exchange messages", "[nuclearnet |
140 | 108 | net.b.set_subscriptions({HASH_A}); |
141 | 109 |
|
142 | 110 | std::mutex mutex; |
143 | | - std::condition_variable cv; |
144 | 111 | std::vector<std::string> join_events; |
145 | 112 | std::vector<std::string> leave_events; |
146 | 113 | std::vector<std::pair<std::string, std::vector<uint8_t>>> received; |
147 | 114 |
|
148 | 115 | net.a.set_join_callback([&](const PeerInfo& peer) { |
149 | 116 | std::lock_guard<std::mutex> lock(mutex); |
150 | 117 | join_events.push_back("a:" + peer.name); |
151 | | - cv.notify_all(); |
152 | 118 | }); |
153 | 119 | net.b.set_join_callback([&](const PeerInfo& peer) { |
154 | 120 | std::lock_guard<std::mutex> lock(mutex); |
155 | 121 | join_events.push_back("b:" + peer.name); |
156 | | - cv.notify_all(); |
157 | 122 | }); |
158 | 123 |
|
159 | 124 | net.a.set_leave_callback([&](const PeerInfo& peer) { |
160 | 125 | std::lock_guard<std::mutex> lock(mutex); |
161 | 126 | leave_events.push_back("a:" + peer.name); |
162 | | - cv.notify_all(); |
163 | 127 | }); |
164 | 128 | net.b.set_leave_callback([&](const PeerInfo& peer) { |
165 | 129 | std::lock_guard<std::mutex> lock(mutex); |
166 | 130 | leave_events.push_back("b:" + peer.name); |
167 | | - cv.notify_all(); |
168 | 131 | }); |
169 | 132 |
|
170 | 133 | net.a.set_packet_callback([&](const sock_t&, const std::string& peer_name, uint64_t hash, bool reliable, |
171 | 134 | std::vector<uint8_t>&& payload) { |
172 | 135 | std::lock_guard<std::mutex> lock(mutex); |
173 | 136 | received.emplace_back("a:" + peer_name + ":" + std::to_string(hash) + ":" + (reliable ? "1" : "0"), |
174 | 137 | std::move(payload)); |
175 | | - cv.notify_all(); |
176 | 138 | }); |
177 | 139 | net.b.set_packet_callback([&](const sock_t&, const std::string& peer_name, uint64_t hash, bool reliable, |
178 | 140 | std::vector<uint8_t>&& payload) { |
179 | 141 | std::lock_guard<std::mutex> lock(mutex); |
180 | 142 | received.emplace_back("b:" + peer_name + ":" + std::to_string(hash) + ":" + (reliable ? "1" : "0"), |
181 | 143 | std::move(payload)); |
182 | | - cv.notify_all(); |
183 | 144 | }); |
184 | 145 |
|
185 | | - net.start(); |
186 | | - |
187 | 146 | REQUIRE(wait_for([&] { |
188 | 147 | std::lock_guard<std::mutex> lock(mutex); |
189 | 148 | 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 |
234 | 193 | return std::find(leave_events.begin(), leave_events.end(), "a:bravo") != leave_events.end(); |
235 | 194 | }, 5s, [&] { |
236 | 195 | net.a.process(); |
237 | | - net.b.process(); |
238 | 196 | })); |
239 | | - |
240 | | - net.stop(); |
241 | 197 | } |
242 | 198 |
|
243 | 199 | SCENARIO("NUClearNet handles bidirectional reliable traffic", "[nuclearnet][integration]") { |
@@ -276,8 +232,6 @@ SCENARIO("NUClearNet handles bidirectional reliable traffic", "[nuclearnet][inte |
276 | 232 | b_received.push_back(std::move(payload)); |
277 | 233 | }); |
278 | 234 |
|
279 | | - net.start(); |
280 | | - |
281 | 235 | REQUIRE(wait_for([&] { |
282 | 236 | std::lock_guard<std::mutex> lock(mutex); |
283 | 237 | 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 |
306 | 260 | REQUIRE(a_received[0] == small_payload); |
307 | 261 | REQUIRE(b_received[0] == large_payload); |
308 | 262 | } |
309 | | - |
310 | | - net.stop(); |
311 | 263 | } |
0 commit comments