blob: 59687e4d5160cce2976f6a476c35ef09e5c0ba4f [file] [log] [blame]
Alexander Afanasyev45fba082012-03-12 18:05:24 -07001/* -*- 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 Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev45fba082012-03-12 18:05:24 -070020 * 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>
26using boost::test_tools::output_test_stream;
27
28#include <boost/make_shared.hpp>
Alexander Afanasyev158ec0d2012-04-05 13:48:55 -070029#include "sync-scheduler.h"
30#include "sync-logic.h"
Alexander Afanasyev45fba082012-03-12 18:05:24 -070031
32using namespace Sync;
33using namespace std;
34using 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 Afanasyevbf2b4362012-03-12 23:55:09 -070048enum SCHEDULE_LABELS
49 {
50 TEST_LABEL,
51 ANOTHER_LABEL
52 };
53
Alexander Afanasyev45fba082012-03-12 18:05:24 -070054struct 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 Afanasyevbf2b4362012-03-12 23:55:09 -070070 scheduler->schedule (boost::posix_time::milliseconds (100),
71 boost::bind (&SchedulerFixture::everySecond, this),
72 TEST_LABEL);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070073 }
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
89BOOST_FIXTURE_TEST_SUITE (SchedulerTestSuite, SchedulerFixture)
90
91BOOST_AUTO_TEST_CASE (BasicTest)
92{
93 BOOST_CHECK_NO_THROW (scheduler = new Scheduler ());
94
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -070095 scheduler->schedule (posix_time::milliseconds (100),
96 bind (&SchedulerFixture::everySecond, this),
97 TEST_LABEL);
Alexander Afanasyev45fba082012-03-12 18:05:24 -070098
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 Afanasyevbf2b4362012-03-12 23:55:09 -0700104 bind (&SchedulerFixture::setCounterFive, this),
105 TEST_LABEL);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700106
107 this_thread::sleep (posix_time::milliseconds (400)); // just in case
108
109 scheduler->schedule (posix_time::milliseconds (600),
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700110 bind (&SchedulerFixture::setCounterThree, this),
111 TEST_LABEL);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700112
113 this_thread::sleep (posix_time::milliseconds (500));
114 BOOST_CHECK_EQUAL (counter, 9); // still 9
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700115
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700116 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 Afanasyevbf2b4362012-03-12 23:55:09 -0700121
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 Afanasyev45fba082012-03-12 18:05:24 -0700129
130 BOOST_CHECK_NO_THROW (delete scheduler);
131}
132
133BOOST_AUTO_TEST_SUITE_END ()
134
135
136void funcUpdate( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ )
137{
138}
139
140void funcRemove( const std::string &/*prefix*/ )
141{
142}
143
Alexander Afanasyevbf2b4362012-03-12 23:55:09 -0700144BOOST_AUTO_TEST_CASE (SyncLogicSchedulerTest)
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700145{
146 SyncLogic *logic = 0;
Zhenkai Zhuce66e212012-03-12 22:27:19 -0700147 BOOST_CHECK_NO_THROW (logic = new SyncLogic ("/prefix", funcUpdate, funcRemove));
Alexander Afanasyevd3c501e2012-03-15 17:52:34 -0700148 this_thread::sleep (posix_time::milliseconds (100));
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700149
150 Scheduler &scheduler = logic->getScheduler ();
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700151 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1);
152
Alexander Afanasyevf7417d92012-03-13 13:43:55 -0700153 BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
154 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 2);
155
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700156 this_thread::sleep (posix_time::milliseconds (100)); // max waiting time
Alexander Afanasyevf7417d92012-03-13 13:43:55 -0700157 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700158
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 Afanasyevf7417d92012-03-13 13:43:55 -0700163 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700164
165 this_thread::sleep (posix_time::milliseconds (19)); // min waiting time is 20
Alexander Afanasyevf7417d92012-03-13 13:43:55 -0700166 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 5);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700167
168 this_thread::sleep (posix_time::milliseconds (100)); // max waiting time
Alexander Afanasyevf7417d92012-03-13 13:43:55 -0700169 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1);
Alexander Afanasyev45fba082012-03-12 18:05:24 -0700170
171 BOOST_CHECK_NO_THROW (delete logic);
172}
173
174#endif