blob: 9a7225181b9c3fb70c6d954e0a9b2559dfc798f6 [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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
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>
26using boost::test_tools::output_test_stream;
27
28#include <boost/make_shared.hpp>
29#include "../model/sync-scheduler.h"
30#include "../model/sync-logic.h"
31
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
48struct SchedulerFixture
49{
50 SchedulerFixture ()
51 : counter (0)
52 {}
53
54 int counter;
55
56 Scheduler *scheduler;
57
58 void everySecond ()
59 {
60 // cout << "." << flush;
61 counter ++;
62
63 if (counter < 9)
64 scheduler->schedule (boost::posix_time::milliseconds (100), boost::bind (&SchedulerFixture::everySecond, this));
65 }
66
67 void setCounterFive ()
68 {
69 counter = 5;
70 }
71
72 void setCounterThree ()
73 {
74 counter = 3;
75 }
76};
77
78
79#ifdef _DEBUG
80
81BOOST_FIXTURE_TEST_SUITE (SchedulerTestSuite, SchedulerFixture)
82
83BOOST_AUTO_TEST_CASE (BasicTest)
84{
85 BOOST_CHECK_NO_THROW (scheduler = new Scheduler ());
86
87 scheduler->schedule (posix_time::milliseconds (100), bind (&SchedulerFixture::everySecond, this));
88
89 sleep (1);
90 // cout << counter << endl;
91 BOOST_CHECK_EQUAL (counter, 9); // generally, should be 9
92
93 scheduler->schedule (posix_time::seconds (2),
94 bind (&SchedulerFixture::setCounterFive, this));
95
96 this_thread::sleep (posix_time::milliseconds (400)); // just in case
97
98 scheduler->schedule (posix_time::milliseconds (600),
99 bind (&SchedulerFixture::setCounterThree, this));
100
101 this_thread::sleep (posix_time::milliseconds (500));
102 BOOST_CHECK_EQUAL (counter, 9); // still 9
103
104 this_thread::sleep (posix_time::milliseconds (200));
105 BOOST_CHECK_EQUAL (counter, 3);
106
107 this_thread::sleep (posix_time::milliseconds (1000));
108 BOOST_CHECK_EQUAL (counter, 5);
109
110 BOOST_CHECK_NO_THROW (delete scheduler);
111}
112
113BOOST_AUTO_TEST_SUITE_END ()
114
115
116void funcUpdate( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ )
117{
118}
119
120void funcRemove( const std::string &/*prefix*/ )
121{
122}
123
124BOOST_AUTO_TEST_CASE (SyncLogicTest)
125{
126 SyncLogic *logic = 0;
127 BOOST_CHECK_NO_THROW (logic = new SyncLogic ("/prefix", funcUpdate, funcRemove, make_shared<CcnxWrapper> ()));
128
129 Scheduler &scheduler = logic->getScheduler ();
130 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 0);
131
132 BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
133 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1);
134
135 this_thread::sleep (posix_time::milliseconds (100)); // max waiting time
136 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 0);
137
138 BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
139 BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
140 BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
141 BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
142 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 4);
143
144 this_thread::sleep (posix_time::milliseconds (19)); // min waiting time is 20
145 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 4);
146
147 this_thread::sleep (posix_time::milliseconds (100)); // max waiting time
148 BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 0);
149
150 BOOST_CHECK_NO_THROW (delete logic);
151}
152
153#endif