source: trunk/test/scheduler.cc

Last change on this file was 3851, checked in by Peter, 22 months ago

merge release 0.16.3 into trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1// $Id: scheduler.cc 3851 2019-12-12 00:44:06Z peter $
2
3/*
4  Copyright (C) 2014, 2015, 2017, 2019 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#include <config.h>
23
24#include "Suite.h"
25
26#include "yat/utility/Scheduler.h"
27
28#include <boost/make_shared.hpp>
29#include <boost/shared_ptr.hpp>
30
31#include <cstdlib>
32#include <iostream>
33#include <set>
34#include <unistd.h>
35
36using namespace theplu::yat;
37using namespace theplu::yat::utility;
38
39class Sleeper : public Scheduler::Job
40{
41public:
42  Sleeper(int t) : time_(t) {}
43  void operator()(void)
44  {
45    sleep(time_);
46    std::cout << "Have slept for " << time_ << " seconds" << std::endl;
47  }
48private:
49  int time_;
50};
51
52
53int main(int argc, char* argv[])
54{
55  test::Suite suite(argc, argv);
56
57  Scheduler scheduler(2);
58  int n_threads = scheduler.threads();
59  if (n_threads != 2) {
60    suite.add(false);
61    suite.err() << "threads(): " << n_threads << " - expected: 2\n";
62  }
63  for (size_t i=0; i<10; ++i)
64    scheduler.submit(boost::make_shared<Sleeper>(i < 4 ? 1 : 0));
65  // sleep a sec to give the Scheduler chance to process submitted jobs
66  sleep(1);
67  int n = scheduler.jobs();
68  suite.out() << "jobs: " << n << "\n";
69  if (n < 8 || n > 10) {
70    suite.add(false);
71    suite.err() << "error: jobs() returned: " << n << "; expected 8-10\n";
72  }
73
74  std::vector<boost::shared_ptr<Sleeper> > sleeper;
75  sleeper.reserve(4);
76  sleeper.push_back(boost::make_shared<Sleeper>(1));
77  sleeper.push_back(boost::make_shared<Sleeper>(10));
78  scheduler.submit(sleeper[1]);
79  sleeper.push_back(boost::make_shared<Sleeper>(2));
80  scheduler.add_dependency(sleeper[2], sleeper[0]);
81  sleeper.push_back(boost::make_shared<Sleeper>(3));
82
83  // not strictly needed since sleeper2 depends on sleeper0,
84  // but... this is a test
85  scheduler.add_dependency(sleeper[3], sleeper[0]);
86  scheduler.add_dependency(sleeper[3], sleeper[1]);
87  scheduler.add_dependency(sleeper[3], sleeper[2]);
88
89  scheduler.submit(sleeper[3]);
90  scheduler.wait();
91  return suite.return_value();
92}
Note: See TracBrowser for help on using the repository browser.