blob: a13f373b886dedbc6b966be5b4d027a84045a896 [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);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080046 executor.start ();
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080047 Executor::Job job = bind(timeConsumingJob);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080048
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080049 executor.execute(job);
50 executor.execute(job);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080051
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080052 usleep(2000);
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080053 // both jobs should have been taken care of
54 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080055
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080056 usleep(500000);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080057
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080058 // add four jobs while only one thread is idle
59 executor.execute(job);
60 executor.execute(job);
61 executor.execute(job);
62 executor.execute(job);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080063
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080064 usleep(1000);
65 // three jobs should remain in queue
66 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 3);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080067
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080068 usleep(500000);
69 // two threads should have finished and
70 // take care of two queued jobs
71 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 1);
72
73 // all jobs should have been fetched
74 usleep(501000);
75 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0);
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080076
77 executor.shutdown ();
Alexander Afanasyevab5dff72013-01-24 10:25:28 -080078 } //separate scope to ensure that destructor is called
79
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080080
81 sleep(1);
Zhenkai Zhuc8a54ca2013-01-18 20:25:41 -080082}