Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #include <boost/test/unit_test.hpp> |
| 24 | #include "executor.h" |
| 25 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 26 | #include "logging.h" |
| 27 | |
| 28 | INIT_LOGGER ("Test.Executor"); |
| 29 | |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 30 | using namespace boost; |
| 31 | using namespace std; |
| 32 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 33 | void timeConsumingJob () |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 34 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 35 | _LOG_DEBUG ("Start sleep"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 36 | sleep(1); |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 37 | _LOG_DEBUG ("Finish sleep"); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(TestExecutor) |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 41 | { |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 42 | INIT_LOGGERS (); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 44 | { |
| 45 | Executor executor (3); |
| 46 | Executor::Job job = bind(timeConsumingJob); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 47 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 48 | executor.execute(job); |
| 49 | executor.execute(job); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 51 | usleep(1000); |
| 52 | // both jobs should have been taken care of |
| 53 | BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 55 | usleep(500000); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 57 | // add four jobs while only one thread is idle |
| 58 | executor.execute(job); |
| 59 | executor.execute(job); |
| 60 | executor.execute(job); |
| 61 | executor.execute(job); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 63 | usleep(1000); |
| 64 | // three jobs should remain in queue |
| 65 | BOOST_CHECK_EQUAL(executor.jobQueueSize(), 3); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | ab5dff7 | 2013-01-24 10:25:28 -0800 | [diff] [blame] | 67 | usleep(500000); |
| 68 | // two threads should have finished and |
| 69 | // take care of two queued jobs |
| 70 | BOOST_CHECK_EQUAL(executor.jobQueueSize(), 1); |
| 71 | |
| 72 | // all jobs should have been fetched |
| 73 | usleep(501000); |
| 74 | BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0); |
| 75 | } //separate scope to ensure that destructor is called |
| 76 | |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 77 | |
| 78 | sleep(1); |
Zhenkai Zhu | c8a54ca | 2013-01-18 20:25:41 -0800 | [diff] [blame] | 79 | } |