Welcome to mirror list, hosted at ThFree Co, Russian Federation.

slic3r_jobs_tests.cpp « slic3rutils « tests - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 56c2fab431b63ca9628f6022152166fa1cb9d391 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "catch2/catch.hpp"

#include <chrono>
#include <thread>

#include "slic3r/GUI/Jobs/BoostThreadWorker.hpp"
#include "slic3r/GUI/Jobs/ProgressIndicator.hpp"

//#include <boost/thread/thread.hpp>

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();
    });

    while (!worker.is_idle())
        worker.process_events();

    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");
        }
    });

    while (!worker.is_idle())
        worker.process_events();

    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) {
                std::this_thread::sleep_for(std::chrono::milliseconds(10));
                ctl.update_status(s, "Running");
                if (ctl.was_canceled()) break;
            }
        },
        [](bool cancelled, std::exception_ptr &) { // finalize
            REQUIRE(cancelled == true);
        });

    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    worker.cancel();

    while (!worker.is_idle())
        worker.process_events();

    REQUIRE(pri->pr != 100);
}

TEST_CASE("cancel_all should remove all pending jobs", "[Jobs]") {
    using namespace Slic3r;
    using namespace Slic3r::GUI;

    auto pri = std::make_shared<Progress>();
    BoostThreadWorker worker{pri};

    std::array<bool, 4> jobres = {false};

    queue_job(worker, [&jobres](Job::Ctl &) {
        jobres[0] = true;
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    });
    queue_job(worker, [&jobres](Job::Ctl &) {
        jobres[1] = true;
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    });
    queue_job(worker, [&jobres](Job::Ctl &) {
        jobres[2] = true;
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    });
    queue_job(worker, [&jobres](Job::Ctl &) {
        jobres[3] = true;
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
    });

    std::this_thread::sleep_for(std::chrono::microseconds(500));
    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;
        });

    while (!worker.is_idle())
        worker.process_events();
}