3405 |
06 Apr 15 |
peter |
// $Id$ |
3405 |
06 Apr 15 |
peter |
2 |
|
3405 |
06 Apr 15 |
peter |
3 |
/* |
4204 |
26 Aug 22 |
peter |
Copyright (C) 2015, 2019, 2022 Peter Johansson |
3405 |
06 Apr 15 |
peter |
5 |
|
3405 |
06 Apr 15 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
3405 |
06 Apr 15 |
peter |
7 |
|
3405 |
06 Apr 15 |
peter |
The yat library is free software; you can redistribute it and/or |
3405 |
06 Apr 15 |
peter |
modify it under the terms of the GNU General Public License as |
3405 |
06 Apr 15 |
peter |
published by the Free Software Foundation; either version 3 of the |
3405 |
06 Apr 15 |
peter |
License, or (at your option) any later version. |
3405 |
06 Apr 15 |
peter |
12 |
|
3405 |
06 Apr 15 |
peter |
The yat library is distributed in the hope that it will be useful, |
3405 |
06 Apr 15 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3405 |
06 Apr 15 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3405 |
06 Apr 15 |
peter |
General Public License for more details. |
3405 |
06 Apr 15 |
peter |
17 |
|
3405 |
06 Apr 15 |
peter |
You should have received a copy of the GNU General Public License |
3405 |
06 Apr 15 |
peter |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
3405 |
06 Apr 15 |
peter |
20 |
*/ |
3405 |
06 Apr 15 |
peter |
21 |
|
3405 |
06 Apr 15 |
peter |
// test Scheduler and a case in which a Job throws an exception which |
3405 |
06 Apr 15 |
peter |
// is caught after Scheduler has been destructed. |
3405 |
06 Apr 15 |
peter |
24 |
|
3405 |
06 Apr 15 |
peter |
25 |
|
3405 |
06 Apr 15 |
peter |
26 |
#include <config.h> |
3405 |
06 Apr 15 |
peter |
27 |
|
3405 |
06 Apr 15 |
peter |
28 |
#include "Suite.h" |
3405 |
06 Apr 15 |
peter |
29 |
|
3405 |
06 Apr 15 |
peter |
30 |
#include "yat/utility/Exception.h" |
3405 |
06 Apr 15 |
peter |
31 |
#include "yat/utility/Scheduler.h" |
3405 |
06 Apr 15 |
peter |
32 |
|
3405 |
06 Apr 15 |
peter |
33 |
#include <boost/shared_ptr.hpp> |
3405 |
06 Apr 15 |
peter |
34 |
|
4204 |
26 Aug 22 |
peter |
35 |
#include <chrono> |
3405 |
06 Apr 15 |
peter |
36 |
#include <iostream> |
3405 |
06 Apr 15 |
peter |
37 |
|
3405 |
06 Apr 15 |
peter |
38 |
using namespace theplu::yat; |
3405 |
06 Apr 15 |
peter |
39 |
using namespace theplu::yat::utility; |
3405 |
06 Apr 15 |
peter |
40 |
|
3405 |
06 Apr 15 |
peter |
41 |
class Sleeper : public Scheduler::Job |
3405 |
06 Apr 15 |
peter |
42 |
{ |
3405 |
06 Apr 15 |
peter |
43 |
public: |
3405 |
06 Apr 15 |
peter |
44 |
Sleeper(int x) : time_(x) {} |
3405 |
06 Apr 15 |
peter |
45 |
void operator()(void) |
3405 |
06 Apr 15 |
peter |
46 |
{ |
3405 |
06 Apr 15 |
peter |
47 |
if (time_==3) |
3405 |
06 Apr 15 |
peter |
48 |
throw utility::runtime_error("some message"); |
4204 |
26 Aug 22 |
peter |
49 |
std::this_thread::sleep_for(std::chrono::seconds(time_)); |
3405 |
06 Apr 15 |
peter |
50 |
std::cerr << "sleeping " << time_ << " seconds\n"; |
3405 |
06 Apr 15 |
peter |
51 |
} |
3405 |
06 Apr 15 |
peter |
52 |
private: |
3405 |
06 Apr 15 |
peter |
53 |
int time_; |
3405 |
06 Apr 15 |
peter |
54 |
}; |
3405 |
06 Apr 15 |
peter |
55 |
|
3405 |
06 Apr 15 |
peter |
56 |
|
4204 |
26 Aug 22 |
peter |
57 |
void run(test::Suite& suite) |
3405 |
06 Apr 15 |
peter |
58 |
{ |
3405 |
06 Apr 15 |
peter |
59 |
Scheduler scheduler(2); |
3405 |
06 Apr 15 |
peter |
60 |
std::vector<boost::shared_ptr<Sleeper> > task; |
4204 |
26 Aug 22 |
peter |
61 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(4))); |
3405 |
06 Apr 15 |
peter |
62 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(2))); |
3405 |
06 Apr 15 |
peter |
63 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(3))); |
4204 |
26 Aug 22 |
peter |
64 |
for (size_t i=0; i<100; ++i) |
4204 |
26 Aug 22 |
peter |
65 |
task.push_back(boost::shared_ptr<Sleeper>(new Sleeper(1))); |
4204 |
26 Aug 22 |
peter |
66 |
|
3405 |
06 Apr 15 |
peter |
67 |
for (size_t i=0; i<task.size(); ++i) |
3405 |
06 Apr 15 |
peter |
68 |
scheduler.submit(task[i]); |
4204 |
26 Aug 22 |
peter |
69 |
while (scheduler.jobs()) { |
4204 |
26 Aug 22 |
peter |
// churn to trigger ::throw_if_error |
4204 |
26 Aug 22 |
peter |
71 |
scheduler.threads(2); |
4204 |
26 Aug 22 |
peter |
72 |
} |
4204 |
26 Aug 22 |
peter |
73 |
|
3405 |
06 Apr 15 |
peter |
74 |
scheduler.wait(); |
3405 |
06 Apr 15 |
peter |
75 |
} |
3405 |
06 Apr 15 |
peter |
76 |
|
3405 |
06 Apr 15 |
peter |
77 |
|
3405 |
06 Apr 15 |
peter |
78 |
int main(int argc, char* argv[]) |
3405 |
06 Apr 15 |
peter |
79 |
{ |
3405 |
06 Apr 15 |
peter |
80 |
test::Suite suite(argc, argv); |
3405 |
06 Apr 15 |
peter |
81 |
|
3405 |
06 Apr 15 |
peter |
82 |
try { |
4204 |
26 Aug 22 |
peter |
83 |
run(suite); |
3848 |
23 Sep 19 |
peter |
84 |
suite.err() << "error: no exception thrown\n"; |
3405 |
06 Apr 15 |
peter |
85 |
suite.add(false); |
3405 |
06 Apr 15 |
peter |
86 |
} |
3405 |
06 Apr 15 |
peter |
87 |
catch (std::runtime_error& e) { |
3405 |
06 Apr 15 |
peter |
88 |
suite.out() << "expected exception caught with what: " << e.what() << "\n"; |
3405 |
06 Apr 15 |
peter |
89 |
} |
3405 |
06 Apr 15 |
peter |
90 |
return suite.return_value(); |
3405 |
06 Apr 15 |
peter |
91 |
} |