2021-12-02 14:06:35 +00:00
|
|
|
#include "catch2/catch.hpp"
|
|
|
|
|
2021-12-02 14:34:20 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
|
2021-12-02 14:06:35 +00:00
|
|
|
#include "slic3r/GUI/Jobs/BoostThreadWorker.hpp"
|
|
|
|
#include "slic3r/GUI/Jobs/ProgressIndicator.hpp"
|
|
|
|
|
2021-12-02 14:34:20 +00:00
|
|
|
//#include <boost/thread/thread.hpp>
|
|
|
|
|
2021-12-02 14:06:35 +00:00
|
|
|
struct Progress: Slic3r::ProgressIndicator {
|
|
|
|
int range = 100;
|
|
|
|
int pr = 0;
|
|
|
|
std::string statustxt;
|
|
|
|
void set_range(int r) override { range = r; }
|
|
|
|
void set_cancel_callback(CancelFn = CancelFn()) override {}
|
|
|
|
void set_progress(int p) override { pr = p; }
|
|
|
|
void set_status_text(const char *txt) override { statustxt = txt; }
|
|
|
|
int get_range() const override { return range; }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE("nullptr job should be ignored", "[Jobs]") {
|
|
|
|
Slic3r::GUI::BoostThreadWorker worker{std::make_unique<Progress>()};
|
|
|
|
worker.push(nullptr);
|
|
|
|
|
|
|
|
REQUIRE(worker.is_idle());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("State should not be idle while running a job", "[Jobs]") {
|
|
|
|
using namespace Slic3r;
|
|
|
|
using namespace Slic3r::GUI;
|
|
|
|
BoostThreadWorker worker{std::make_unique<Progress>(), "worker_thread"};
|
|
|
|
|
|
|
|
queue_job(worker, [&worker](Job::Ctl &ctl) {
|
|
|
|
ctl.call_on_main_thread([&worker] {
|
|
|
|
REQUIRE(!worker.is_idle());
|
|
|
|
}).wait();
|
|
|
|
});
|
|
|
|
|
2021-12-02 22:05:55 +00:00
|
|
|
worker.wait_for_idle();
|
2021-12-02 14:06:35 +00:00
|
|
|
|
|
|
|
REQUIRE(worker.is_idle());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Status messages should be received by the main thread during job execution", "[Jobs]") {
|
|
|
|
using namespace Slic3r;
|
|
|
|
using namespace Slic3r::GUI;
|
|
|
|
auto pri = std::make_shared<Progress>();
|
|
|
|
BoostThreadWorker worker{pri};
|
|
|
|
|
|
|
|
queue_job(worker, [](Job::Ctl &ctl){
|
|
|
|
for (int s = 0; s <= 100; ++s) {
|
|
|
|
ctl.update_status(s, "Running");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-02 22:05:55 +00:00
|
|
|
worker.wait_for_idle();
|
2021-12-02 14:06:35 +00:00
|
|
|
|
|
|
|
REQUIRE(pri->pr == 100);
|
|
|
|
REQUIRE(pri->statustxt == "Running");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Cancellation should be recognized be the worker", "[Jobs]") {
|
|
|
|
using namespace Slic3r;
|
|
|
|
using namespace Slic3r::GUI;
|
|
|
|
|
|
|
|
auto pri = std::make_shared<Progress>();
|
|
|
|
BoostThreadWorker worker{pri};
|
|
|
|
|
|
|
|
queue_job(
|
|
|
|
worker,
|
|
|
|
[](Job::Ctl &ctl) {
|
|
|
|
for (int s = 0; s <= 100; ++s) {
|
2021-12-02 14:34:20 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
2021-12-02 14:06:35 +00:00
|
|
|
ctl.update_status(s, "Running");
|
|
|
|
if (ctl.was_canceled()) break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[](bool cancelled, std::exception_ptr &) { // finalize
|
|
|
|
REQUIRE(cancelled == true);
|
|
|
|
});
|
|
|
|
|
2021-12-02 14:34:20 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2021-12-02 14:06:35 +00:00
|
|
|
worker.cancel();
|
|
|
|
|
2021-12-02 22:05:55 +00:00
|
|
|
worker.wait_for_current_job();
|
2021-12-02 14:06:35 +00:00
|
|
|
|
|
|
|
REQUIRE(pri->pr != 100);
|
|
|
|
}
|
|
|
|
|
2021-12-02 13:55:52 +00:00
|
|
|
TEST_CASE("cancel_all should remove all pending jobs", "[Jobs]") {
|
2021-12-02 14:06:35 +00:00
|
|
|
using namespace Slic3r;
|
|
|
|
using namespace Slic3r::GUI;
|
|
|
|
|
|
|
|
auto pri = std::make_shared<Progress>();
|
|
|
|
BoostThreadWorker worker{pri};
|
|
|
|
|
2022-01-18 09:54:37 +00:00
|
|
|
std::array<bool, 4> jobres = {false, false, false, false};
|
2021-12-02 14:06:35 +00:00
|
|
|
|
2021-12-02 14:34:20 +00:00
|
|
|
queue_job(worker, [&jobres](Job::Ctl &) {
|
|
|
|
jobres[0] = true;
|
2022-01-18 09:54:37 +00:00
|
|
|
// FIXME: the long wait time is needed to prevent fail in MSVC
|
|
|
|
// where the sleep_for function is behaving stupidly.
|
|
|
|
// see https://developercommunity.visualstudio.com/t/bogus-stdthis-threadsleep-for-implementation/58530
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
2021-12-02 14:34:20 +00:00
|
|
|
});
|
|
|
|
queue_job(worker, [&jobres](Job::Ctl &) {
|
|
|
|
jobres[1] = true;
|
|
|
|
});
|
|
|
|
queue_job(worker, [&jobres](Job::Ctl &) {
|
|
|
|
jobres[2] = true;
|
|
|
|
});
|
|
|
|
queue_job(worker, [&jobres](Job::Ctl &) {
|
|
|
|
jobres[3] = true;
|
|
|
|
});
|
2021-12-02 14:06:35 +00:00
|
|
|
|
2022-01-18 09:54:37 +00:00
|
|
|
// wait until the first job's half time to be sure, the cancel is made
|
|
|
|
// during the first job's execution.
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
2021-12-02 14:06:35 +00:00
|
|
|
worker.cancel_all();
|
|
|
|
|
|
|
|
REQUIRE(jobres[0] == true);
|
|
|
|
REQUIRE(jobres[1] == false);
|
|
|
|
REQUIRE(jobres[2] == false);
|
|
|
|
REQUIRE(jobres[3] == false);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("Exception should be properly forwarded to finalize()", "[Jobs]") {
|
|
|
|
using namespace Slic3r;
|
|
|
|
using namespace Slic3r::GUI;
|
|
|
|
|
|
|
|
auto pri = std::make_shared<Progress>();
|
|
|
|
BoostThreadWorker worker{pri};
|
|
|
|
|
|
|
|
queue_job(
|
|
|
|
worker, [](Job::Ctl &) { throw std::runtime_error("test"); },
|
|
|
|
[](bool /*canceled*/, std::exception_ptr &eptr) {
|
|
|
|
REQUIRE(eptr != nullptr);
|
|
|
|
try {
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
} catch (std::runtime_error &e) {
|
|
|
|
REQUIRE(std::string(e.what()) == "test");
|
|
|
|
}
|
|
|
|
|
|
|
|
eptr = nullptr;
|
|
|
|
});
|
|
|
|
|
2021-12-02 22:05:55 +00:00
|
|
|
worker.wait_for_idle();
|
|
|
|
REQUIRE(worker.is_idle());
|
2021-12-02 14:06:35 +00:00
|
|
|
}
|