| /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| /* |
| * Copyright (c) 2012-2014 University of California, Los Angeles |
| * |
| * This file is part of ChronoSync, synchronization library for distributed realtime |
| * applications for NDN. |
| * |
| * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, either |
| * version 3 of the License, or (at your option) any later version. |
| * |
| * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #include <boost/test/unit_test.hpp> |
| #include <boost/test/output_test_stream.hpp> |
| #include <map> |
| using boost::test_tools::output_test_stream; |
| |
| #include <boost/make_shared.hpp> |
| #include "sync-scheduler.h" |
| #include "sync-logic.h" |
| |
| using namespace Sync; |
| using namespace std; |
| using namespace boost; |
| |
| |
| |
| // void funcUpdate (const std::string &, const SeqNo &newSeq, const SeqNo &oldSeq) |
| // { |
| // cout << "funcUpdate\n"; |
| // } |
| |
| // void funcRemove (const std::string &) |
| // { |
| // cout << "funcRemove\n"; |
| // } |
| |
| enum SCHEDULE_LABELS |
| { |
| TEST_LABEL, |
| ANOTHER_LABEL |
| }; |
| |
| struct SchedulerFixture |
| { |
| SchedulerFixture () |
| : counter (0) |
| {} |
| |
| int counter; |
| |
| Scheduler *scheduler; |
| |
| void everySecond () |
| { |
| // cout << "." << flush; |
| counter ++; |
| |
| if (counter < 9) |
| scheduler->schedule (boost::posix_time::milliseconds (100), |
| boost::bind (&SchedulerFixture::everySecond, this), |
| TEST_LABEL); |
| } |
| |
| void setCounterFive () |
| { |
| counter = 5; |
| } |
| |
| void setCounterThree () |
| { |
| counter = 3; |
| } |
| }; |
| |
| |
| #ifdef _DEBUG |
| |
| BOOST_FIXTURE_TEST_SUITE (SchedulerTestSuite, SchedulerFixture) |
| |
| BOOST_AUTO_TEST_CASE (BasicTest) |
| { |
| BOOST_CHECK_NO_THROW (scheduler = new Scheduler ()); |
| |
| scheduler->schedule (posix_time::milliseconds (100), |
| bind (&SchedulerFixture::everySecond, this), |
| TEST_LABEL); |
| |
| sleep (1); |
| // cout << counter << endl; |
| BOOST_CHECK_EQUAL (counter, 9); // generally, should be 9 |
| |
| scheduler->schedule (posix_time::seconds (2), |
| bind (&SchedulerFixture::setCounterFive, this), |
| TEST_LABEL); |
| |
| this_thread::sleep (posix_time::milliseconds (400)); // just in case |
| |
| scheduler->schedule (posix_time::milliseconds (600), |
| bind (&SchedulerFixture::setCounterThree, this), |
| TEST_LABEL); |
| |
| this_thread::sleep (posix_time::milliseconds (500)); |
| BOOST_CHECK_EQUAL (counter, 9); // still 9 |
| |
| this_thread::sleep (posix_time::milliseconds (200)); |
| BOOST_CHECK_EQUAL (counter, 3); |
| |
| this_thread::sleep (posix_time::milliseconds (1000)); |
| BOOST_CHECK_EQUAL (counter, 5); |
| |
| scheduler->schedule (posix_time::milliseconds (100), |
| bind (&SchedulerFixture::setCounterThree, this), |
| ANOTHER_LABEL); |
| this_thread::sleep (posix_time::milliseconds (50)); |
| scheduler->cancel (ANOTHER_LABEL); |
| this_thread::sleep (posix_time::milliseconds (150)); |
| BOOST_CHECK_EQUAL (counter, 5); |
| |
| BOOST_CHECK_NO_THROW (delete scheduler); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END () |
| |
| |
| void funcUpdate( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ ) |
| { |
| } |
| |
| void funcPass( const std::vector<MissingDataInfo> &v) |
| { |
| } |
| |
| void funcRemove( const std::string &/*prefix*/ ) |
| { |
| } |
| |
| BOOST_AUTO_TEST_CASE (SyncLogicSchedulerTest) |
| { |
| SyncLogic *logic = 0; |
| BOOST_CHECK_NO_THROW (logic = new SyncLogic ("/prefix", funcPass, funcRemove)); |
| this_thread::sleep (posix_time::milliseconds (100)); |
| |
| Scheduler &scheduler = logic->getScheduler (); |
| BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
| |
| BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 2); |
| |
| this_thread::sleep (posix_time::milliseconds (100)); // max waiting time |
| BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
| |
| BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e")); |
| BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5); |
| |
| this_thread::sleep (posix_time::milliseconds (19)); // min waiting time is 20 |
| BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5); |
| |
| this_thread::sleep (posix_time::milliseconds (100)); // max waiting time |
| BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1); |
| |
| BOOST_CHECK_NO_THROW (delete logic); |
| } |
| |
| #endif |