blob: 5236234d13846c7f791fd2029c186d7b5e7fe98b [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
26using namespace boost;
27using namespace std;
28
29void timeConsumingJob()
30{
31 cout << "Start sleep" << endl;
32 sleep(1);
33 cout << "finish sleep" << endl;
34}
35
36BOOST_AUTO_TEST_CASE(ExecutorTest)
37{
38 Executor executor(3);
39 Executor::Job job = bind(timeConsumingJob);
40
41 executor.execute(job);
42 executor.execute(job);
43
44 usleep(100);
45 // both jobs should have been taken care of
46 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0);
47
48 usleep(500000);
49
50 // add four jobs while only one thread is idle
51 executor.execute(job);
52 executor.execute(job);
53 executor.execute(job);
54 executor.execute(job);
55
56 usleep(100);
57 // three jobs should remain in queue
58 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 3);
59
60 usleep(500000);
61 // two threads should have finished and
62 // take care of two queued jobs
63 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 1);
64
65 // all jobs should have been fetched
66 usleep(500100);
67 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0);
68
69 sleep(1);
70
71}