blob: 273f0c2a04c06ccb5e49a71cded2e3eed21165bc [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 * Zhenkai Zhu
6 *
7 * BSD license, See the LICENSE file for more information
8 *
9 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
10 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
11 */
12
13#include <boost/test/unit_test.hpp>
14#include "executor/executor.h"
15
16#include "logging.h"
17
18INIT_LOGGER ("Test.Executor");
19
20using namespace boost;
21using namespace std;
22
23void timeConsumingJob ()
24{
25 _LOG_DEBUG ("Start sleep");
26 sleep(1);
27 _LOG_DEBUG ("Finish sleep");
28}
29
30BOOST_AUTO_TEST_CASE(TestExecutor)
31{
32 INIT_LOGGERS ();
33
34 {
35 Executor executor (3);
36 executor.start ();
37 Executor::Job job = bind(timeConsumingJob);
38
39 executor.execute(job);
40 executor.execute(job);
41
42 usleep(2000);
43 // both jobs should have been taken care of
44 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0);
45
46 usleep(500000);
47
48 // add four jobs while only one thread is idle
49 executor.execute(job);
50 executor.execute(job);
51 executor.execute(job);
52 executor.execute(job);
53
54 usleep(1000);
55 // three jobs should remain in queue
56 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 3);
57
58 usleep(500000);
59 // two threads should have finished and
60 // take care of two queued jobs
61 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 1);
62
63 // all jobs should have been fetched
64 usleep(501000);
65 BOOST_CHECK_EQUAL(executor.jobQueueSize(), 0);
66
67 executor.shutdown ();
68 } //separate scope to ensure that destructor is called
69
70
71 sleep(1);
72}