|
3 | 3 |
|
4 | 4 | #include "tasks/basic_task.h" |
5 | 5 | #include "tasks/task_system.h" |
| 6 | +#include "tasks/worker.h" |
6 | 7 |
|
7 | 8 | #include <doctest/doctest.h> |
8 | 9 | #include <iostream> |
@@ -243,3 +244,173 @@ TEST_CASE("Scheduling" * doctest::test_suite("basic_tasks")) |
243 | 244 | std::iota(target.begin(), target.end(), 0); |
244 | 245 | REQUIRE(count_with_me == target); |
245 | 246 | } |
| 247 | + |
| 248 | +// Helper functions at namespace scope to ensure external linkage, so that |
| 249 | +// backtrace_symbols can resolve their names with -rdynamic. |
| 250 | +namespace exception_handling_test |
| 251 | +{ |
| 252 | + void level_3_throws_runtime_error() |
| 253 | + { |
| 254 | + throw std::runtime_error("Test exception"); |
| 255 | + } |
| 256 | + |
| 257 | + void level_2_calls_level_3() |
| 258 | + { |
| 259 | + level_3_throws_runtime_error(); |
| 260 | + } |
| 261 | + |
| 262 | + void level_1_calls_level_2() |
| 263 | + { |
| 264 | + level_2_calls_level_3(); |
| 265 | + } |
| 266 | + |
| 267 | + void level_3_throws_int() |
| 268 | + { |
| 269 | + throw 42; |
| 270 | + } |
| 271 | + |
| 272 | + void level_2_calls_level_3_int() |
| 273 | + { |
| 274 | + level_3_throws_int(); |
| 275 | + } |
| 276 | + |
| 277 | + void level_1_calls_level_2_int() |
| 278 | + { |
| 279 | + level_2_calls_level_3_int(); |
| 280 | + } |
| 281 | + |
| 282 | + struct ThrowsException : public ccf::tasks::BaseTask |
| 283 | + { |
| 284 | + void do_task_implementation() override |
| 285 | + { |
| 286 | + level_1_calls_level_2(); |
| 287 | + } |
| 288 | + |
| 289 | + const std::string& get_name() const override |
| 290 | + { |
| 291 | + static const std::string name = "ThrowsException"; |
| 292 | + return name; |
| 293 | + } |
| 294 | + }; |
| 295 | + |
| 296 | + struct ThrowsUnknown : public ccf::tasks::BaseTask |
| 297 | + { |
| 298 | + void do_task_implementation() override |
| 299 | + { |
| 300 | + level_1_calls_level_2_int(); |
| 301 | + } |
| 302 | + |
| 303 | + const std::string& get_name() const override |
| 304 | + { |
| 305 | + static const std::string name = "ThrowsUnknown"; |
| 306 | + return name; |
| 307 | + } |
| 308 | + }; |
| 309 | +} |
| 310 | + |
| 311 | +TEST_CASE("Exception handling" * doctest::test_suite("basic_tasks")) |
| 312 | +{ |
| 313 | + // Custom logger that captures log messages for assertion |
| 314 | + struct CapturingLogger : public ccf::logger::AbstractLogger |
| 315 | + { |
| 316 | + std::mutex mutex; |
| 317 | + std::vector<std::string> messages; |
| 318 | + |
| 319 | + void write(const ccf::logger::LogLine& ll) override |
| 320 | + { |
| 321 | + std::lock_guard<std::mutex> lock(mutex); |
| 322 | + messages.push_back(ll.msg); |
| 323 | + } |
| 324 | + |
| 325 | + bool contains(const std::string& substring) |
| 326 | + { |
| 327 | + std::lock_guard<std::mutex> lock(mutex); |
| 328 | + for (const auto& m : messages) |
| 329 | + { |
| 330 | + if (m.find(substring) != std::string::npos) |
| 331 | + { |
| 332 | + return true; |
| 333 | + } |
| 334 | + } |
| 335 | + return false; |
| 336 | + } |
| 337 | + |
| 338 | + void clear() |
| 339 | + { |
| 340 | + std::lock_guard<std::mutex> lock(mutex); |
| 341 | + messages.clear(); |
| 342 | + } |
| 343 | + }; |
| 344 | + |
| 345 | + auto capturing_logger = std::make_unique<CapturingLogger>(); |
| 346 | + auto* logger_ptr = capturing_logger.get(); |
| 347 | + ccf::logger::config::loggers().push_back(std::move(capturing_logger)); |
| 348 | + |
| 349 | + // Task that runs successfully after exceptions |
| 350 | + std::atomic<bool> success_task_ran = false; |
| 351 | + ccf::tasks::Task success_task = ccf::tasks::make_basic_task( |
| 352 | + [&success_task_ran]() { success_task_ran.store(true); }, "SuccessTask"); |
| 353 | + |
| 354 | + ccf::tasks::JobBoard job_board; |
| 355 | + std::atomic<bool> stop_signal = false; |
| 356 | + |
| 357 | + // Queue tasks: two that throw, then one that should still run |
| 358 | + job_board.add_task( |
| 359 | + std::make_shared<exception_handling_test::ThrowsException>()); |
| 360 | + job_board.add_task( |
| 361 | + std::make_shared<exception_handling_test::ThrowsUnknown>()); |
| 362 | + job_board.add_task(success_task); |
| 363 | + |
| 364 | + std::thread worker([&]() { |
| 365 | + ccf::tasks::task_worker_loop( |
| 366 | + job_board, stop_signal, /*abort_on_throw=*/false); |
| 367 | + }); |
| 368 | + |
| 369 | + // Wait for the success task to run |
| 370 | + const auto wait_step = std::chrono::milliseconds(10); |
| 371 | + const auto max_wait = std::chrono::seconds(5); |
| 372 | + auto waited = std::chrono::milliseconds(0); |
| 373 | + while (!success_task_ran.load() && waited < max_wait) |
| 374 | + { |
| 375 | + std::this_thread::sleep_for(wait_step); |
| 376 | + waited += wait_step; |
| 377 | + } |
| 378 | + |
| 379 | + stop_signal.store(true); |
| 380 | + worker.join(); |
| 381 | + |
| 382 | + // With CCF_TASK_EXCEPTION_NO_ABORT, the worker loop continues after |
| 383 | + // exceptions, so the success task should have run |
| 384 | + REQUIRE(success_task_ran.load()); |
| 385 | + |
| 386 | + // Verify that fatal messages were logged for both exception types |
| 387 | + REQUIRE(logger_ptr->contains( |
| 388 | + "ThrowsException task failed with exception: Test exception")); |
| 389 | + REQUIRE( |
| 390 | + logger_ptr->contains("ThrowsUnknown task failed with unknown exception")); |
| 391 | + |
| 392 | + // Verify that stack traces contain demangled function names from the |
| 393 | + // known call chains. These functions have external linkage and are |
| 394 | + // exported to the dynamic symbol table via -rdynamic in Debug builds. |
| 395 | + // Note: very small leaf functions (e.g. level_3_throws_int, which is |
| 396 | + // just `throw 42;`) may be inlined by the compiler, so we only assert |
| 397 | + // on the caller frames that reliably appear. |
| 398 | + |
| 399 | + // ThrowsException call chain |
| 400 | + REQUIRE(logger_ptr->contains("level_3_throws_runtime_error")); |
| 401 | + REQUIRE(logger_ptr->contains("level_2_calls_level_3()")); |
| 402 | + REQUIRE(logger_ptr->contains("level_1_calls_level_2()")); |
| 403 | + |
| 404 | + // ThrowsUnknown call chain |
| 405 | + REQUIRE(logger_ptr->contains("level_2_calls_level_3_int")); |
| 406 | + REQUIRE(logger_ptr->contains("level_1_calls_level_2_int")); |
| 407 | + |
| 408 | + // Clean up: remove the capturing logger |
| 409 | + auto& loggers = ccf::logger::config::loggers(); |
| 410 | + loggers.erase( |
| 411 | + std::remove_if( |
| 412 | + loggers.begin(), |
| 413 | + loggers.end(), |
| 414 | + [logger_ptr](const auto& l) { return l.get() == logger_ptr; }), |
| 415 | + loggers.end()); |
| 416 | +} |
0 commit comments