blob: abbbf5c4bea6aad179bf72d7171b75b467f87657 (
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
152
153
154
155
156
157
158
159
160
161
162
163
|
/*
Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
This file is part of DCP-o-matic.
DCP-o-matic is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
DCP-o-matic is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file test/job_manager_test.cc
* @brief Test JobManager.
* @ingroup selfcontained
*/
#include "lib/cross.h"
#include "lib/job.h"
#include "lib/job_manager.h"
#include <boost/test/unit_test.hpp>
using std::make_shared;
using std::shared_ptr;
using std::string;
using std::vector;
class TestJob : public Job
{
public:
explicit TestJob (shared_ptr<Film> film)
: Job (film)
{
}
~TestJob ()
{
stop_thread ();
}
void set_finished_ok () {
set_state (FINISHED_OK);
}
void set_finished_error () {
set_state (FINISHED_ERROR);
}
void run () override
{
while (true) {
if (finished ()) {
return;
}
boost::this_thread::interruption_point();
}
}
string name () const override {
return "";
}
string json_name () const override {
return "";
}
};
BOOST_AUTO_TEST_CASE (job_manager_test1)
{
shared_ptr<Film> film;
/* Single job */
auto a = make_shared<TestJob>(film);
JobManager::instance()->add (a);
dcpomatic_sleep_seconds (1);
BOOST_CHECK (a->running());
a->set_finished_ok ();
dcpomatic_sleep_seconds (2);
BOOST_CHECK (a->finished_ok());
}
BOOST_AUTO_TEST_CASE (job_manager_test2)
{
shared_ptr<Film> film;
vector<shared_ptr<TestJob>> jobs;
for (int i = 0; i < 16; ++i) {
auto job = make_shared<TestJob>(film);
jobs.push_back (job);
JobManager::instance()->add (job);
}
dcpomatic_sleep_seconds (1);
BOOST_CHECK (jobs[0]->running());
jobs[0]->set_finished_ok();
dcpomatic_sleep_seconds (1);
BOOST_CHECK (!jobs[0]->running());
BOOST_CHECK (jobs[1]->running());
/* Push our jobs[5] to the top of the list */
for (int i = 0; i < 5; ++i) {
JobManager::instance()->increase_priority(jobs[5]);
}
dcpomatic_sleep_seconds (1);
for (int i = 0; i < 16; ++i) {
BOOST_CHECK (i == 5 ? jobs[i]->running() : !jobs[i]->running());
}
/* Set any jobs that are started to be finished, until they're all finished */
while (true) {
if (std::find_if(jobs.begin(), jobs.end(), [](shared_ptr<Job> job) { return !job->finished_ok(); }) == jobs.end()) {
break;
}
for (auto job: jobs) {
if (job->running()) {
job->set_finished_ok();
}
}
}
BOOST_REQUIRE (!wait_for_jobs());
}
BOOST_AUTO_TEST_CASE(cancel_job_test)
{
shared_ptr<Film> film;
vector<shared_ptr<TestJob>> jobs;
for (int i = 0; i < 2; ++i) {
auto job = make_shared<TestJob>(film);
jobs.push_back(job);
JobManager::instance()->add(job);
}
jobs[1]->cancel();
jobs[0]->cancel();
dcpomatic_sleep_seconds(5);
BOOST_CHECK(jobs[0]->finished_cancelled());
BOOST_CHECK(jobs[1]->finished_cancelled());
}
|