Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 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> |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 29 | #include "sync-scheduler.h" |
| 30 | #include "sync-logic.h" |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 48 | enum SCHEDULE_LABELS |
| 49 | { |
| 50 | TEST_LABEL, |
| 51 | ANOTHER_LABEL |
| 52 | }; |
| 53 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 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) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 70 | scheduler->schedule (boost::posix_time::milliseconds (100), |
| 71 | boost::bind (&SchedulerFixture::everySecond, this), |
| 72 | TEST_LABEL); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 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 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 95 | scheduler->schedule (posix_time::milliseconds (100), |
| 96 | bind (&SchedulerFixture::everySecond, this), |
| 97 | TEST_LABEL); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 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), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 104 | bind (&SchedulerFixture::setCounterFive, this), |
| 105 | TEST_LABEL); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 106 | |
| 107 | this_thread::sleep (posix_time::milliseconds (400)); // just in case |
| 108 | |
| 109 | scheduler->schedule (posix_time::milliseconds (600), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 110 | bind (&SchedulerFixture::setCounterThree, this), |
| 111 | TEST_LABEL); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 112 | |
| 113 | this_thread::sleep (posix_time::milliseconds (500)); |
| 114 | BOOST_CHECK_EQUAL (counter, 9); // still 9 |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 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); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 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); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 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 funcRemove( const std::string &/*prefix*/ ) |
| 141 | { |
| 142 | } |
| 143 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 144 | BOOST_AUTO_TEST_CASE (SyncLogicSchedulerTest) |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 145 | { |
| 146 | SyncLogic *logic = 0; |
Zhenkai Zhu | ce66e21 | 2012-03-12 22:27:19 -0700 | [diff] [blame] | 147 | BOOST_CHECK_NO_THROW (logic = new SyncLogic ("/prefix", funcUpdate, funcRemove)); |
Alexander Afanasyev | d3c501e | 2012-03-15 17:52:34 -0700 | [diff] [blame] | 148 | this_thread::sleep (posix_time::milliseconds (100)); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 149 | |
| 150 | Scheduler &scheduler = logic->getScheduler (); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 151 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
| 152 | |
Alexander Afanasyev | f7417d9 | 2012-03-13 13:43:55 -0700 | [diff] [blame] | 153 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 154 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 2); |
| 155 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 156 | this_thread::sleep (posix_time::milliseconds (100)); // max waiting time |
Alexander Afanasyev | f7417d9 | 2012-03-13 13:43:55 -0700 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 158 | |
| 159 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 160 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 161 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 162 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
Alexander Afanasyev | f7417d9 | 2012-03-13 13:43:55 -0700 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 164 | |
| 165 | this_thread::sleep (posix_time::milliseconds (19)); // min waiting time is 20 |
Alexander Afanasyev | f7417d9 | 2012-03-13 13:43:55 -0700 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 167 | |
| 168 | this_thread::sleep (posix_time::milliseconds (100)); // max waiting time |
Alexander Afanasyev | f7417d9 | 2012-03-13 13:43:55 -0700 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 170 | |
| 171 | BOOST_CHECK_NO_THROW (delete logic); |
| 172 | } |
| 173 | |
| 174 | #endif |