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