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