blob: 9bdd43a4a129f682bfcf65eb68cee28c978d95ba [file] [log] [blame]
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -08001/* -*- 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 Afanasyevab5dff72013-01-24 10:25:28 -080026#include "logging.h"
27
28INIT_LOGGER ("Test.Executor");
29
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080030using namespace boost;
31using namespace std;
32
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080033void timeConsumingJob ()
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080034{
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080035 _LOG_DEBUG ("Start sleep");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080036 sleep(1);
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080037 _LOG_DEBUG ("Finish sleep");
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080038}
39
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080040BOOST_AUTO_TEST_CASE(TestExecutor)
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080041{
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080042 INIT_LOGGERS ();
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080043
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080044 {
45 Executor executor (3);
46 Executor::Job job = bind(timeConsumingJob);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080047
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080048 executor.execute(job);
49 executor.execute(job);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080050
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080051 usleep(1000);
52 // both jobs should have been taken care of
53 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080054
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080055 usleep(500000);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080056
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080057 // 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 Zhuc8a54ca2013-01-18 20:25:41 -080062
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080063 usleep(1000);
64 // three jobs should remain in queue
65 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 3);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080066
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080067 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 Zhuc8a54ca2013-01-18 20:25:41 -080077
78 sleep(1);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080079}