slic3r_jobs_tests.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "catch2/catch.hpp"
  2. #include <chrono>
  3. #include <thread>
  4. #include "slic3r/GUI/Jobs/BoostThreadWorker.hpp"
  5. #include "slic3r/GUI/Jobs/ProgressIndicator.hpp"
  6. //#include <boost/thread/thread.hpp>
  7. struct Progress: Slic3r::ProgressIndicator {
  8. int range = 100;
  9. int pr = 0;
  10. std::string statustxt;
  11. void set_range(int r) override { range = r; }
  12. void set_cancel_callback(CancelFn = CancelFn()) override {}
  13. void set_progress(int p) override { pr = p; }
  14. void set_status_text(const char *txt) override { statustxt = txt; }
  15. int get_range() const override { return range; }
  16. };
  17. TEST_CASE("nullptr job should be ignored", "[Jobs]") {
  18. Slic3r::GUI::BoostThreadWorker worker{std::make_unique<Progress>()};
  19. worker.push(nullptr);
  20. REQUIRE(worker.is_idle());
  21. }
  22. TEST_CASE("State should not be idle while running a job", "[Jobs]") {
  23. using namespace Slic3r;
  24. using namespace Slic3r::GUI;
  25. BoostThreadWorker worker{std::make_unique<Progress>(), "worker_thread"};
  26. queue_job(worker, [&worker](Job::Ctl &ctl) {
  27. ctl.call_on_main_thread([&worker] {
  28. REQUIRE(!worker.is_idle());
  29. }).wait();
  30. });
  31. worker.wait_for_idle();
  32. REQUIRE(worker.is_idle());
  33. }
  34. TEST_CASE("Status messages should be received by the main thread during job execution", "[Jobs]") {
  35. using namespace Slic3r;
  36. using namespace Slic3r::GUI;
  37. auto pri = std::make_shared<Progress>();
  38. BoostThreadWorker worker{pri};
  39. queue_job(worker, [](Job::Ctl &ctl){
  40. for (int s = 0; s <= 100; ++s) {
  41. ctl.update_status(s, "Running");
  42. }
  43. });
  44. worker.wait_for_idle();
  45. REQUIRE(pri->pr == 100);
  46. REQUIRE(pri->statustxt == "Running");
  47. }
  48. TEST_CASE("Cancellation should be recognized be the worker", "[Jobs]") {
  49. using namespace Slic3r;
  50. using namespace Slic3r::GUI;
  51. auto pri = std::make_shared<Progress>();
  52. BoostThreadWorker worker{pri};
  53. queue_job(
  54. worker,
  55. [](Job::Ctl &ctl) {
  56. for (int s = 0; s <= 100; ++s) {
  57. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  58. ctl.update_status(s, "Running");
  59. if (ctl.was_canceled()) break;
  60. }
  61. },
  62. [](bool cancelled, std::exception_ptr &) { // finalize
  63. REQUIRE(cancelled == true);
  64. });
  65. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  66. worker.cancel();
  67. worker.wait_for_current_job();
  68. REQUIRE(pri->pr != 100);
  69. }
  70. TEST_CASE("cancel_all should remove all pending jobs", "[Jobs]") {
  71. using namespace Slic3r;
  72. using namespace Slic3r::GUI;
  73. auto pri = std::make_shared<Progress>();
  74. BoostThreadWorker worker{pri};
  75. std::array<bool, 4> jobres = {false, false, false, false};
  76. queue_job(worker, [&jobres](Job::Ctl &) {
  77. jobres[0] = true;
  78. // FIXME: the long wait time is needed to prevent fail in MSVC
  79. // where the sleep_for function is behaving stupidly.
  80. // see https://developercommunity.visualstudio.com/t/bogus-stdthis-threadsleep-for-implementation/58530
  81. std::this_thread::sleep_for(std::chrono::seconds(1));
  82. });
  83. queue_job(worker, [&jobres](Job::Ctl &) {
  84. jobres[1] = true;
  85. });
  86. queue_job(worker, [&jobres](Job::Ctl &) {
  87. jobres[2] = true;
  88. });
  89. queue_job(worker, [&jobres](Job::Ctl &) {
  90. jobres[3] = true;
  91. });
  92. // wait until the first job's half time to be sure, the cancel is made
  93. // during the first job's execution.
  94. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  95. worker.cancel_all();
  96. REQUIRE(jobres[0] == true);
  97. REQUIRE(jobres[1] == false);
  98. REQUIRE(jobres[2] == false);
  99. REQUIRE(jobres[3] == false);
  100. }
  101. TEST_CASE("Exception should be properly forwarded to finalize()", "[Jobs]") {
  102. using namespace Slic3r;
  103. using namespace Slic3r::GUI;
  104. auto pri = std::make_shared<Progress>();
  105. BoostThreadWorker worker{pri};
  106. queue_job(
  107. worker, [](Job::Ctl &) { throw std::runtime_error("test"); },
  108. [](bool /*canceled*/, std::exception_ptr &eptr) {
  109. REQUIRE(eptr != nullptr);
  110. try {
  111. std::rethrow_exception(eptr);
  112. } catch (std::runtime_error &e) {
  113. REQUIRE(std::string(e.what()) == "test");
  114. }
  115. eptr = nullptr;
  116. });
  117. worker.wait_for_idle();
  118. REQUIRE(worker.is_idle());
  119. }