akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 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 | * Chaoyi Bian <bcy@pku.edu.cn> |
| 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include <boost/test/unit_test.hpp> |
| 24 | #include <boost/test/output_test_stream.hpp> |
| 25 | #include <map> |
| 26 | using boost::test_tools::output_test_stream; |
| 27 | |
| 28 | #include <boost/make_shared.hpp> |
| 29 | #include "sync-scheduler.h" |
| 30 | #include "sync-logic.h" |
| 31 | |
| 32 | using namespace Sync; |
| 33 | using namespace std; |
| 34 | using namespace boost; |
| 35 | |
| 36 | |
| 37 | |
| 38 | // void funcUpdate (const std::string &, const SeqNo &newSeq, const SeqNo &oldSeq) |
| 39 | // { |
| 40 | // cout << "funcUpdate\n"; |
| 41 | // } |
| 42 | |
| 43 | // void funcRemove (const std::string &) |
| 44 | // { |
| 45 | // cout << "funcRemove\n"; |
| 46 | // } |
| 47 | |
| 48 | enum SCHEDULE_LABELS |
| 49 | { |
| 50 | TEST_LABEL, |
| 51 | ANOTHER_LABEL |
| 52 | }; |
| 53 | |
| 54 | struct SchedulerFixture |
| 55 | { |
| 56 | SchedulerFixture () |
| 57 | : counter (0) |
| 58 | {} |
| 59 | |
| 60 | int counter; |
| 61 | |
| 62 | Scheduler *scheduler; |
| 63 | |
| 64 | void everySecond () |
| 65 | { |
| 66 | // cout << "." << flush; |
| 67 | counter ++; |
| 68 | |
| 69 | if (counter < 9) |
| 70 | scheduler->schedule (boost::posix_time::milliseconds (100), |
| 71 | boost::bind (&SchedulerFixture::everySecond, this), |
| 72 | TEST_LABEL); |
| 73 | } |
| 74 | |
| 75 | void setCounterFive () |
| 76 | { |
| 77 | counter = 5; |
| 78 | } |
| 79 | |
| 80 | void setCounterThree () |
| 81 | { |
| 82 | counter = 3; |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | #ifdef _DEBUG |
| 88 | |
| 89 | BOOST_FIXTURE_TEST_SUITE (SchedulerTestSuite, SchedulerFixture) |
| 90 | |
| 91 | BOOST_AUTO_TEST_CASE (BasicTest) |
| 92 | { |
| 93 | BOOST_CHECK_NO_THROW (scheduler = new Scheduler ()); |
| 94 | |
| 95 | scheduler->schedule (posix_time::milliseconds (100), |
| 96 | bind (&SchedulerFixture::everySecond, this), |
| 97 | TEST_LABEL); |
| 98 | |
| 99 | sleep (1); |
| 100 | // cout << counter << endl; |
| 101 | BOOST_CHECK_EQUAL (counter, 9); // generally, should be 9 |
| 102 | |
| 103 | scheduler->schedule (posix_time::seconds (2), |
| 104 | bind (&SchedulerFixture::setCounterFive, this), |
| 105 | TEST_LABEL); |
| 106 | |
| 107 | this_thread::sleep (posix_time::milliseconds (400)); // just in case |
| 108 | |
| 109 | scheduler->schedule (posix_time::milliseconds (600), |
| 110 | bind (&SchedulerFixture::setCounterThree, this), |
| 111 | TEST_LABEL); |
| 112 | |
| 113 | this_thread::sleep (posix_time::milliseconds (500)); |
| 114 | BOOST_CHECK_EQUAL (counter, 9); // still 9 |
| 115 | |
| 116 | this_thread::sleep (posix_time::milliseconds (200)); |
| 117 | BOOST_CHECK_EQUAL (counter, 3); |
| 118 | |
| 119 | this_thread::sleep (posix_time::milliseconds (1000)); |
| 120 | BOOST_CHECK_EQUAL (counter, 5); |
| 121 | |
| 122 | scheduler->schedule (posix_time::milliseconds (100), |
| 123 | bind (&SchedulerFixture::setCounterThree, this), |
| 124 | ANOTHER_LABEL); |
| 125 | this_thread::sleep (posix_time::milliseconds (50)); |
| 126 | scheduler->cancel (ANOTHER_LABEL); |
| 127 | this_thread::sleep (posix_time::milliseconds (150)); |
| 128 | BOOST_CHECK_EQUAL (counter, 5); |
| 129 | |
| 130 | BOOST_CHECK_NO_THROW (delete scheduler); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END () |
| 134 | |
| 135 | |
| 136 | void funcUpdate( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | void funcPass( const std::vector<MissingDataInfo> &v) |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | void funcRemove( const std::string &/*prefix*/ ) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_CASE (SyncLogicSchedulerTest) |
| 149 | { |
| 150 | SyncLogic *logic = 0; |
| 151 | BOOST_CHECK_NO_THROW (logic = new SyncLogic ("/prefix", funcPass, funcRemove)); |
| 152 | this_thread::sleep (posix_time::milliseconds (100)); |
| 153 | |
| 154 | Scheduler &scheduler = logic->getScheduler (); |
| 155 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
| 156 | |
| 157 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 158 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 2); |
| 159 | |
| 160 | this_thread::sleep (posix_time::milliseconds (100)); // max waiting time |
| 161 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
| 162 | |
| 163 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 164 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 165 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 166 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 167 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5); |
| 168 | |
| 169 | this_thread::sleep (posix_time::milliseconds (19)); // min waiting time is 20 |
| 170 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5); |
| 171 | |
| 172 | this_thread::sleep (posix_time::milliseconds (100)); // max waiting time |
| 173 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
| 174 | |
| 175 | BOOST_CHECK_NO_THROW (delete logic); |
| 176 | } |
| 177 | |
| 178 | #endif |