blob: 2a53a229afba10800e2c57873e4f651e0d1c8227 [file] [log] [blame]
Alexander Afanasyev45fba082012-03-12 18:05:24 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Alexander Afanasyev45fba082012-03-12 18:05:24 -07004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Alexander Afanasyev45fba082012-03-12 18:05:24 -07007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * 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 Afanasyev45fba082012-03-12 18:05:24 -070011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * 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 Afanasyev45fba082012-03-12 18:05:24 -070015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * 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 Afanasyev45fba082012-03-12 18:05:24 -070018 */
19
20#include <boost/test/unit_test.hpp>
Alexander Afanasyev8722d872014-07-02 13:00:29 -070021#include <boost/test/output_test_stream.hpp>
Alexander Afanasyev45fba082012-03-12 18:05:24 -070022#include <map>
23using boost::test_tools::output_test_stream;
24
25#include <boost/make_shared.hpp>
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070026#include "sync-scheduler.h"
27#include "sync-logic.h"
Alexander Afanasyev45fba082012-03-12 18:05:24 -070028
29using namespace Sync;
30using namespace std;
31using 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 Afanasyevbf2b4362012-03-12 23:55:09 -070045enum SCHEDULE_LABELS
46 {
47 TEST_LABEL,
48 ANOTHER_LABEL
49 };
50
Alexander Afanasyev45fba082012-03-12 18:05:24 -070051struct SchedulerFixture
52{
53 SchedulerFixture ()
54 : counter (0)
55 {}
56
57 int counter;
Alexander Afanasyev8722d872014-07-02 13:00:29 -070058
Alexander Afanasyev45fba082012-03-12 18:05:24 -070059 Scheduler *scheduler;
60
61 void everySecond ()
62 {
63 // cout << "." << flush;
64 counter ++;
65
66 if (counter < 9)
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070067 scheduler->schedule (boost::posix_time::milliseconds (100),
68 boost::bind (&SchedulerFixture::everySecond, this),
69 TEST_LABEL);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070070 }
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
86BOOST_FIXTURE_TEST_SUITE (SchedulerTestSuite, SchedulerFixture)
87
88BOOST_AUTO_TEST_CASE (BasicTest)
89{
90 BOOST_CHECK_NO_THROW (scheduler = new Scheduler ());
91
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070092 scheduler->schedule (posix_time::milliseconds (100),
93 bind (&SchedulerFixture::everySecond, this),
94 TEST_LABEL);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070095
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 Afanasyevbf2b4362012-03-12 23:55:09 -0700101 bind (&SchedulerFixture::setCounterFive, this),
102 TEST_LABEL);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700103
104 this_thread::sleep (posix_time::milliseconds (400)); // just in case
105
106 scheduler->schedule (posix_time::milliseconds (600),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700107 bind (&SchedulerFixture::setCounterThree, this),
108 TEST_LABEL);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700109
110 this_thread::sleep (posix_time::milliseconds (500));
111 BOOST_CHECK_EQUAL (counter, 9); // still 9
Alexander Afanasyev8722d872014-07-02 13:00:29 -0700112
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700113 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 Afanasyevbf2b4362012-03-12 23:55:09 -0700118
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 Afanasyev8722d872014-07-02 13:00:29 -0700126
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700127 BOOST_CHECK_NO_THROW (delete scheduler);
128}
129
130BOOST_AUTO_TEST_SUITE_END ()
131
132
133void funcUpdate( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ )
134{
135}
136
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700137void funcPass( const std::vector<MissingDataInfo> &v)
138{
139}
140
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700141void funcRemove( const std::string &/*prefix*/ )
142{
143}
144
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700145BOOST_AUTO_TEST_CASE (SyncLogicSchedulerTest)
Alexander Afanasyev8722d872014-07-02 13:00:29 -0700146{
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700147 SyncLogic *logic = 0;
Zhenkai Zhua30e1782012-06-01 11:52:57 -0700148 BOOST_CHECK_NO_THROW (logic = new SyncLogic ("/prefix", funcPass, funcRemove));
Alexander Afanasyevd3c501e2012-03-15 17:52:34 -0700149 this_thread::sleep (posix_time::milliseconds (100));
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700150
151 Scheduler &scheduler = logic->getScheduler ();
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700152 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1);
153
Alexander Afanasyev8722d872014-07-02 13:00:29 -0700154 BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
Alexander Afanasyevf7417d92012-03-13 13:43:55 -0700155 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 2);
156
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700157 this_thread::sleep (posix_time::milliseconds (100)); // max waiting time
Alexander Afanasyevf7417d92012-03-13 13:43:55 -0700158 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700159
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700160 BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
Alexander Afanasyev8722d872014-07-02 13:00:29 -0700161 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 Afanasyev45fba082012-03-12 18:05:24 -0700165
166 this_thread::sleep (posix_time::milliseconds (19)); // min waiting time is 20
Alexander Afanasyev8722d872014-07-02 13:00:29 -0700167 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700168
169 this_thread::sleep (posix_time::milliseconds (100)); // max waiting time
Alexander Afanasyevf7417d92012-03-13 13:43:55 -0700170 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1);
Alexander Afanasyev8722d872014-07-02 13:00:29 -0700171
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700172 BOOST_CHECK_NO_THROW (delete logic);
173}
174
175#endif