CARLA
Loading...
Searching...
No Matches
test_streaming.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#include "test.h"
8
9#include <carla/ThreadGroup.h>
17
18#include <boost/asio/executor_work_guard.hpp>
19
20#include <atomic>
21#include <chrono>
22#include <future>
23#include <thread>
24
25using namespace std::chrono_literals;
26
27// This is required for low level to properly stop the threads in case of
28// exception/assert.
30public:
31
32 boost::asio::io_context service;
33
34 explicit io_context_running(size_t threads = 2u)
35 : _work_to_do(boost::asio::make_work_guard(service)) {
36 _threads.CreateThreads(threads, [this]() { service.run(); });
37 }
38
40 stop();
41 }
42
43 void stop() {
44 service.stop();
46 }
47
48private:
49
50 boost::asio::executor_work_guard<boost::asio::io_context::executor_type> _work_to_do;
51
53};
54
55TEST(streaming, low_level_sending_strings) {
56 using namespace util::buffer;
57 using namespace carla::streaming;
58 using namespace carla::streaming::detail;
59 using namespace carla::streaming::low_level;
60
61 constexpr auto number_of_messages = 100u;
62 const std::string message_text = "Hello client!";
63
64 std::atomic_size_t message_count{0u};
65
67
69 srv.SetTimeout(1s);
70
71 auto stream = srv.MakeStream();
72
74 c.Subscribe(io.service, stream.token(), [&](auto message) {
75 ++message_count;
76 ASSERT_EQ(message.size(), message_text.size());
77 const std::string msg = as_string(message);
78 ASSERT_EQ(msg, message_text);
79 });
80
81 carla::Buffer Buf(boost::asio::buffer(message_text.c_str(), message_text.size()));
83 for (auto i = 0u; i < number_of_messages; ++i) {
84 std::this_thread::sleep_for(2ms);
85 carla::SharedBufferView View = BufView;
86 stream.Write(View);
87 }
88
89 std::this_thread::sleep_for(2ms);
90 ASSERT_GE(message_count, number_of_messages - 3u);
91
92 io.stop();
93}
94
95TEST(streaming, low_level_unsubscribing) {
96 using namespace util::buffer;
97 using namespace carla::streaming;
98 using namespace carla::streaming::detail;
99 using namespace carla::streaming::low_level;
100
101 constexpr auto number_of_messages = 50u;
102 const std::string message_text = "Hello client!";
103
105
107 srv.SetTimeout(1s);
108
110 for (auto n = 0u; n < 10u; ++n) {
111 auto stream = srv.MakeStream();
112 std::atomic_size_t message_count{0u};
113
114 c.Subscribe(io.service, stream.token(), [&](auto message) {
115 ++message_count;
116 ASSERT_EQ(message.size(), message_text.size());
117 const std::string msg = as_string(message);
118 ASSERT_EQ(msg, message_text);
119 });
120
121 carla::Buffer Buf(boost::asio::buffer(message_text.c_str(), message_text.size()));
123 for (auto i = 0u; i < number_of_messages; ++i) {
124 std::this_thread::sleep_for(4ms);
125 carla::SharedBufferView View = BufView;
126 stream.Write(View);
127 }
128
129 std::this_thread::sleep_for(4ms);
130 c.UnSubscribe(stream.token());
131
132 for (auto i = 0u; i < number_of_messages; ++i) {
133 std::this_thread::sleep_for(2ms);
134 carla::SharedBufferView View = BufView;
135 stream.Write(View);
136 }
137
138 ASSERT_GE(message_count, number_of_messages - 3u);
139 }
140
141 io.stop();
142}
143
144TEST(streaming, low_level_tcp_small_message) {
145 using namespace carla::streaming;
146 using namespace carla::streaming::detail;
147
148 boost::asio::io_context io_context;
149 tcp::Server::endpoint ep(boost::asio::ip::tcp::v4(), TESTING_PORT);
150
151 tcp::Server srv(io_context, ep);
152 srv.SetTimeout(1s);
153 std::atomic_bool done{false};
154 std::atomic_size_t message_count{0u};
155
156 const std::string msg = "Hola!";
157
158 srv.Listen([&](std::shared_ptr<tcp::ServerSession> session) {
159 ASSERT_EQ(session->get_stream_id(), 1u);
160
161 carla::Buffer Buf(boost::asio::buffer(msg.c_str(), msg.size()));
163 while (!done) {
164 std::this_thread::sleep_for(1ns);
165 carla::SharedBufferView View = BufView;
166 session->Write(View);
167 }
168 std::cout << "done!\n";
169 }, [](std::shared_ptr<tcp::ServerSession>) { std::cout << "session closed!\n"; });
170
171 // Use loopback: Windows refuses to connect to the 0.0.0.0 wildcard the server binds to.
172 Dispatcher dispatcher{make_endpoint<tcp::Client::protocol_type>(
173 {make_localhost_address(), srv.GetLocalEndpoint().port()})};
174 auto stream = dispatcher.MakeStream();
175 auto c = std::make_shared<tcp::Client>(io_context, stream.token(), [&](carla::Buffer message) {
176 ++message_count;
177 ASSERT_FALSE(message.empty());
178 ASSERT_EQ(message.size(), 5u);
179 const std::string received = util::buffer::as_string(message);
180 ASSERT_EQ(received, msg);
181 });
182 c->Connect();
183
184 // We need at least two threads because this server loop consumes one.
185 carla::ThreadGroup threads;
186 threads.CreateThreads(
187 std::max(2u, std::thread::hardware_concurrency()),
188 [&]() { io_context.run(); });
189
190 std::this_thread::sleep_for(2s);
191 io_context.stop();
192 done = true;
193 std::cout << "client received " << message_count << " messages\n";
194 ASSERT_GT(message_count, 10u);
195 c->Stop();
196}
197
198// A callback that throws, such as an RPC timing out in the light refresh after
199// a map change (#10165), used to escape io_context::run() and call
200// std::terminate. The client now logs it and keeps reading the stream.
201TEST(streaming, low_level_tcp_callback_exception_does_not_terminate) {
202 using namespace carla::streaming;
203 using namespace carla::streaming::detail;
204
205 boost::asio::io_context io_context;
206 tcp::Server::endpoint ep(boost::asio::ip::tcp::v4(), TESTING_PORT);
207
208 tcp::Server srv(io_context, ep);
209 srv.SetTimeout(1s);
210 std::atomic_bool done{false};
211 std::atomic_size_t message_count{0u};
212
213 const std::string msg = "Hola!";
214
215 srv.Listen([&](std::shared_ptr<tcp::ServerSession> session) {
216 carla::Buffer Buf(boost::asio::buffer(msg.c_str(), msg.size()));
218 while (!done) {
219 std::this_thread::sleep_for(1ns);
220 carla::SharedBufferView View = BufView;
221 session->Write(View);
222 }
223 }, [](std::shared_ptr<tcp::ServerSession>) {});
224
225 // Use loopback: Windows refuses to connect to the 0.0.0.0 wildcard the server binds to.
226 Dispatcher dispatcher{make_endpoint<tcp::Client::protocol_type>(
227 {make_localhost_address(), srv.GetLocalEndpoint().port()})};
228 auto stream = dispatcher.MakeStream();
229 auto c = std::make_shared<tcp::Client>(io_context, stream.token(), [&](carla::Buffer) {
230 if (++message_count == 1u) {
231 throw std::runtime_error("callback failure on the first message");
232 }
233 });
234 c->Connect();
235
236 // We need at least two threads because this server loop consumes one.
237 carla::ThreadGroup threads;
238 threads.CreateThreads(
239 std::max(2u, std::thread::hardware_concurrency()),
240 [&]() { io_context.run(); });
241
242 // Wait for the messages instead of a fixed 2 s: a retried connect takes ~2 s to fail on Windows.
243 const auto deadline = std::chrono::steady_clock::now() + 15s;
244 while (message_count <= 10u && std::chrono::steady_clock::now() < deadline) {
245 std::this_thread::sleep_for(10ms);
246 }
247 io_context.stop();
248 done = true;
249 std::cout << "client received " << message_count << " messages\n";
250 ASSERT_GT(message_count, 10u);
251 c->Stop();
252}
253
254struct DoneGuard {
255 ~DoneGuard() { done = true; };
256 std::atomic_bool &done;
257};
258
259TEST(streaming, stream_outlives_server) {
260 using namespace carla::streaming;
261 using namespace util::buffer;
262 constexpr size_t iterations = 10u;
263 std::atomic_bool done{false};
264 const std::string message = "Hello client, how are you?";
265 std::shared_ptr<Stream> stream;
266
267 carla::ThreadGroup sender;
268 DoneGuard g = {done};
269 sender.CreateThread([&]() {
270
271 carla::Buffer Buf(boost::asio::buffer(message.c_str(), message.size()));
273 while (!done) {
274 // sleep_for(1ms) takes ~15 ms on Windows, too slow for the client's 20 ms window.
275 const auto next_write = std::chrono::steady_clock::now() + 1ms;
276 while (!done && std::chrono::steady_clock::now() < next_write) {
277 std::this_thread::yield();
278 }
279 auto s = std::atomic_load_explicit(&stream, std::memory_order_relaxed);
280 if (s != nullptr) {
281 carla::SharedBufferView View = BufView;
282 s->Write(View);
283 }
284 }
285 });
286
287 for (auto i = 0u; i < iterations; ++i) {
288 Server srv(TESTING_PORT);
289 srv.AsyncRun(2u);
290 {
291 auto s = std::make_shared<Stream>(srv.MakeStream());
292 std::atomic_store_explicit(&stream, s, std::memory_order_relaxed);
293 }
294 std::atomic_size_t messages_received{0u};
295 {
296 Client c;
297 c.AsyncRun(2u);
298 c.Subscribe(stream->token(), [&](auto buffer) {
299 const std::string result = as_string(buffer);
300 ASSERT_EQ(result, message);
301 ++messages_received;
302 });
303 std::this_thread::sleep_for(20ms);
304 } // client dies here.
305 ASSERT_GT(messages_received, 0u);
306 // Detach the sender from this iteration's stream before the server tears
307 // down. Otherwise the sender can call Session::Write on a session whose
308 // io_context is being stopped inside ~Server, which segfaults
309 // intermittently in Release builds on slow runners.
310 std::atomic_store_explicit(&stream, std::shared_ptr<Stream>(), std::memory_order_relaxed);
311 std::this_thread::sleep_for(20ms);
312 } // server dies here.
313 done = true;
314} // stream dies here.
315
316TEST(streaming, multi_stream) {
317 using namespace carla::streaming;
318 using namespace util::buffer;
319 constexpr size_t number_of_messages = 100u;
320 constexpr size_t number_of_clients = 6u;
321 constexpr size_t iterations = 10u;
322 const std::string message = "Hi y'all!";
323
324 Server srv(TESTING_PORT);
325 srv.AsyncRun(number_of_clients);
326 auto stream = srv.MakeStream();
327
328 for (auto i = 0u; i < iterations; ++i) {
329 std::vector<std::pair<std::atomic_size_t, std::unique_ptr<Client>>> v(number_of_clients);
330
331 for (auto &pair : v) {
332 pair.first = 0u;
333 pair.second = std::make_unique<Client>();
334 pair.second->AsyncRun(1u);
335 pair.second->Subscribe(stream.token(), [&](auto buffer) {
336 const std::string result = as_string(buffer);
337 ASSERT_EQ(result, message);
338 ++pair.first;
339 });
340 }
341
342 carla::Buffer Buf(boost::asio::buffer(message.c_str(), message.size()));
344 std::this_thread::sleep_for(6ms);
345 for (auto j = 0u; j < number_of_messages; ++j) {
346 std::this_thread::sleep_for(6ms);
347 carla::SharedBufferView View = BufView;
348 stream.Write(View);
349 }
350 std::this_thread::sleep_for(6ms);
351
352 for (auto &pair : v) {
353 ASSERT_GE(pair.first, number_of_messages - 3u);
354 }
355 }
356}
357
358// When the ROS2 topic-visibility default is left off (the out-of-the-box
359// behavior), a freshly created stream is not visible to ROS 2 until something
360// explicitly enables it.
361TEST(streaming, ros_topic_visibility_default_off) {
362 using namespace carla::streaming;
363 using namespace carla::streaming::detail;
364
365 Server srv(TESTING_PORT);
366 constexpr stream_id_type sensor_id{1u};
367 srv.GetToken(sensor_id); // Materializes the stream in the dispatcher.
368 EXPECT_FALSE(srv.IsEnabledForROS(sensor_id));
369}
370
371// With the default flag on, every stream created afterwards is ROS-visible from
372// the moment it is born (this is what ROS2TopicVisibility=true delivers).
373TEST(streaming, ros_topic_visibility_default_on) {
374 using namespace carla::streaming;
375 using namespace carla::streaming::detail;
376
377 Server srv(TESTING_PORT);
379 constexpr stream_id_type sensor_id{1u};
380 srv.GetToken(sensor_id);
381 EXPECT_TRUE(srv.IsEnabledForROS(sensor_id));
382}
383
384// The default flag must also apply to streams born through MakeStream(), not
385// only through the GetToken() create-on-miss path.
386TEST(streaming, ros_topic_visibility_make_stream_path) {
387 using namespace carla::streaming;
388 using namespace carla::streaming::detail;
389
390 Server srv(TESTING_PORT);
392 auto stream = srv.MakeStream();
393 const auto sensor_id = token_type(stream.token()).get_stream_id();
394 EXPECT_TRUE(srv.IsEnabledForROS(sensor_id));
395}
396
397// Independently of the startup default, the per-stream EnableForROS /
398// DisableForROS surface this feature relies on must round-trip.
399TEST(streaming, ros_topic_visibility_manual_toggle) {
400 using namespace carla::streaming;
401 using namespace carla::streaming::detail;
402
403 Server srv(TESTING_PORT);
404 constexpr stream_id_type sensor_id{1u};
405 srv.GetToken(sensor_id);
406 EXPECT_FALSE(srv.IsEnabledForROS(sensor_id));
407 srv.EnableForROS(sensor_id);
408 EXPECT_TRUE(srv.IsEnabledForROS(sensor_id));
409 srv.DisableForROS(sensor_id);
410 EXPECT_FALSE(srv.IsEnabledForROS(sensor_id));
411}
412
413// In synchronous mode the server waits for the previous message of a session
414// to be sent before writing the next one. A client that stops reading (a
415// listen() callback that never returns) used to keep that wait going forever,
416// on the game or render thread, and the whole server stopped answering
417// (#10233). The wait is now bounded by the session timeout: the stuck client
418// is dropped and the other clients of the same stream keep receiving.
419TEST(streaming, sync_mode_client_not_reading_does_not_block_writer) {
420 using namespace carla::streaming;
421 constexpr auto timeout = 1s;
422 // Larger than the socket buffers, so a client that does not read blocks the
423 // second write.
424 const std::string big(32u * 1024u * 1024u, 'x');
425
426 Server srv(TESTING_PORT);
427 srv.SetTimeout(timeout);
428 srv.SetSynchronousMode(true);
429 srv.AsyncRun(4u);
430 auto stream = srv.MakeStream();
431
432 std::promise<void> release;
433 std::shared_future<void> released = release.get_future().share();
434 std::atomic_size_t stuck_count{0u};
435 Client stuck;
436 stuck.AsyncRun(1u);
437 stuck.Subscribe(stream.token(), [&](auto) {
438 ++stuck_count;
439 released.wait(); // never returns until the end of the test
440 });
441
442 std::atomic_size_t healthy_count{0u};
443 Client healthy;
444 healthy.AsyncRun(1u);
445 healthy.Subscribe(stream.token(), [&](auto) { ++healthy_count; });
446
447 std::this_thread::sleep_for(200ms); // let both sessions connect
448
449 carla::Buffer Buf(boost::asio::buffer(big.c_str(), big.size()));
451 constexpr size_t number_of_messages = 6u;
452 const auto start = std::chrono::steady_clock::now();
453 for (auto i = 0u; i < number_of_messages; ++i) {
454 carla::SharedBufferView View = BufView;
455 stream.Write(View);
456 }
457 const auto elapsed = std::chrono::steady_clock::now() - start;
458
459 // At most one bounded wait for the stuck session, then it is dropped.
460 EXPECT_LT(elapsed, 3 * timeout);
461 EXPECT_GE(stuck_count, 1u);
462 for (auto i = 0; i < 100 && healthy_count < number_of_messages - 1u; ++i) {
463 std::this_thread::sleep_for(20ms);
464 }
465 // Only the message written while the stuck session was being dropped may
466 // be lost.
467 EXPECT_GE(healthy_count, number_of_messages - 1u);
468
469 release.set_value();
470}
static std::shared_ptr< BufferView > CreateFrom(Buffer &&buffer)
Definition BufferView.h:56
A piece of raw data.
void CreateThreads(size_t count, F functor)
Definition ThreadGroup.h:32
void CreateThread(F &&functor)
Definition ThreadGroup.h:27
A client able to subscribe to multiple streams.
void AsyncRun(size_t worker_threads)
void Subscribe(const Token &token, Functor &&callback)
A streaming server.
token_type GetToken(stream_id sensor_id)
void SetTimeout(time_duration timeout)
bool IsEnabledForROS(stream_id sensor_id)
void EnableForROS(stream_id sensor_id)
void SetROS2TopicVisibilityDefaultEnabled(bool enabled)
void AsyncRun(size_t worker_threads)
void DisableForROS(stream_id sensor_id)
void SetSynchronousMode(bool is_synchro)
Keeps the mapping between streams and sessions.
Definition Dispatcher.h:28
carla::streaming::Stream MakeStream()
void SetTimeout(time_duration timeout)
Set session time-out.
void Listen(FunctorT1 on_session_opened, FunctorT2 on_session_closed)
Start listening for connections.
Serializes a stream endpoint.
A client able to subscribe to multiple streams.
void Subscribe(boost::asio::io_context &io_context, token_type token, Functor &&callback)
boost::asio::executor_work_guard< boost::asio::io_context::executor_type > _work_to_do
boost::asio::io_context service
carla::ThreadGroup _threads
io_context_running(size_t threads=2u)
std::shared_ptr< BufferView > SharedBufferView
Definition BufferView.h:151
std::atomic_bool & done
constexpr uint16_t TESTING_PORT
Definition test.h:24
TEST(agents_types, road_option_values_match_python)