Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <boost/test/unit_test.hpp> |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 21 | #include <boost/test/output_test_stream.hpp> |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 22 | #include <map> |
| 23 | using boost::test_tools::output_test_stream; |
| 24 | |
| 25 | #include <boost/make_shared.hpp> |
Alexander Afanasyev | 158ec0d | 2012-04-05 13:48:55 -0700 | [diff] [blame] | 26 | #include "sync-scheduler.h" |
| 27 | #include "sync-logic.h" |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 28 | |
| 29 | using namespace Sync; |
| 30 | using namespace std; |
| 31 | using namespace boost; |
| 32 | |
| 33 | |
| 34 | |
| 35 | // void funcUpdate (const std::string &, const SeqNo &newSeq, const SeqNo &oldSeq) |
| 36 | // { |
| 37 | // cout << "funcUpdate\n"; |
| 38 | // } |
| 39 | |
| 40 | // void funcRemove (const std::string &) |
| 41 | // { |
| 42 | // cout << "funcRemove\n"; |
| 43 | // } |
| 44 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 45 | enum SCHEDULE_LABELS |
| 46 | { |
| 47 | TEST_LABEL, |
| 48 | ANOTHER_LABEL |
| 49 | }; |
| 50 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 51 | struct SchedulerFixture |
| 52 | { |
| 53 | SchedulerFixture () |
| 54 | : counter (0) |
| 55 | {} |
| 56 | |
| 57 | int counter; |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 58 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 59 | Scheduler *scheduler; |
| 60 | |
| 61 | void everySecond () |
| 62 | { |
| 63 | // cout << "." << flush; |
| 64 | counter ++; |
| 65 | |
| 66 | if (counter < 9) |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 67 | scheduler->schedule (boost::posix_time::milliseconds (100), |
| 68 | boost::bind (&SchedulerFixture::everySecond, this), |
| 69 | TEST_LABEL); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void setCounterFive () |
| 73 | { |
| 74 | counter = 5; |
| 75 | } |
| 76 | |
| 77 | void setCounterThree () |
| 78 | { |
| 79 | counter = 3; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | #ifdef _DEBUG |
| 85 | |
| 86 | BOOST_FIXTURE_TEST_SUITE (SchedulerTestSuite, SchedulerFixture) |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE (BasicTest) |
| 89 | { |
| 90 | BOOST_CHECK_NO_THROW (scheduler = new Scheduler ()); |
| 91 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 92 | scheduler->schedule (posix_time::milliseconds (100), |
| 93 | bind (&SchedulerFixture::everySecond, this), |
| 94 | TEST_LABEL); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 95 | |
| 96 | sleep (1); |
| 97 | // cout << counter << endl; |
| 98 | BOOST_CHECK_EQUAL (counter, 9); // generally, should be 9 |
| 99 | |
| 100 | scheduler->schedule (posix_time::seconds (2), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 101 | bind (&SchedulerFixture::setCounterFive, this), |
| 102 | TEST_LABEL); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 103 | |
| 104 | this_thread::sleep (posix_time::milliseconds (400)); // just in case |
| 105 | |
| 106 | scheduler->schedule (posix_time::milliseconds (600), |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 107 | bind (&SchedulerFixture::setCounterThree, this), |
| 108 | TEST_LABEL); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 109 | |
| 110 | this_thread::sleep (posix_time::milliseconds (500)); |
| 111 | BOOST_CHECK_EQUAL (counter, 9); // still 9 |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 113 | this_thread::sleep (posix_time::milliseconds (200)); |
| 114 | BOOST_CHECK_EQUAL (counter, 3); |
| 115 | |
| 116 | this_thread::sleep (posix_time::milliseconds (1000)); |
| 117 | BOOST_CHECK_EQUAL (counter, 5); |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 118 | |
| 119 | scheduler->schedule (posix_time::milliseconds (100), |
| 120 | bind (&SchedulerFixture::setCounterThree, this), |
| 121 | ANOTHER_LABEL); |
| 122 | this_thread::sleep (posix_time::milliseconds (50)); |
| 123 | scheduler->cancel (ANOTHER_LABEL); |
| 124 | this_thread::sleep (posix_time::milliseconds (150)); |
| 125 | BOOST_CHECK_EQUAL (counter, 5); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 127 | BOOST_CHECK_NO_THROW (delete scheduler); |
| 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_SUITE_END () |
| 131 | |
| 132 | |
| 133 | void funcUpdate( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) |
| 134 | { |
| 135 | } |
| 136 | |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 137 | void funcPass( const std::vector<MissingDataInfo> &v) |
| 138 | { |
| 139 | } |
| 140 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 141 | void funcRemove( const std::string &/*prefix*/ ) |
| 142 | { |
| 143 | } |
| 144 | |
Alexander Afanasyev | bf2b436 | 2012-03-12 23:55:09 -0700 | [diff] [blame] | 145 | BOOST_AUTO_TEST_CASE (SyncLogicSchedulerTest) |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 146 | { |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 147 | SyncLogic *logic = 0; |
Zhenkai Zhu | a30e178 | 2012-06-01 11:52:57 -0700 | [diff] [blame] | 148 | BOOST_CHECK_NO_THROW (logic = new SyncLogic ("/prefix", funcPass, funcRemove)); |
Alexander Afanasyev | d3c501e | 2012-03-15 17:52:34 -0700 | [diff] [blame] | 149 | this_thread::sleep (posix_time::milliseconds (100)); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 150 | |
| 151 | Scheduler &scheduler = logic->getScheduler (); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 152 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
| 153 | |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 154 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
Alexander Afanasyev | f7417d9 | 2012-03-13 13:43:55 -0700 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 2); |
| 156 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 157 | this_thread::sleep (posix_time::milliseconds (100)); // max waiting time |
Alexander Afanasyev | f7417d9 | 2012-03-13 13:43:55 -0700 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 159 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 160 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 161 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 162 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 163 | BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| 164 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 165 | |
| 166 | this_thread::sleep (posix_time::milliseconds (19)); // min waiting time is 20 |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 167 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5); |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 168 | |
| 169 | this_thread::sleep (posix_time::milliseconds (100)); // max waiting time |
Alexander Afanasyev | f7417d9 | 2012-03-13 13:43:55 -0700 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 171 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 172 | BOOST_CHECK_NO_THROW (delete logic); |
| 173 | } |
| 174 | |
| 175 | #endif |