Introducing a separate Scheduler class
SyncLogic almost works...
diff --git a/test/test_scheduler.cc b/test/test_scheduler.cc
new file mode 100644
index 0000000..9a72251
--- /dev/null
+++ b/test/test_scheduler.cc
@@ -0,0 +1,153 @@
+/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
+/*
+ * Copyright (c) 2012 University of California, Los Angeles
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
+ * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
+ * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ */
+
+#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 "../model/sync-scheduler.h"
+#include "../model/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";
+// }
+
+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));
+ }
+
+ 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));
+
+ sleep (1);
+ // cout << counter << endl;
+ BOOST_CHECK_EQUAL (counter, 9); // generally, should be 9
+
+ scheduler->schedule (posix_time::seconds (2),
+ bind (&SchedulerFixture::setCounterFive, this));
+
+ this_thread::sleep (posix_time::milliseconds (400)); // just in case
+
+ scheduler->schedule (posix_time::milliseconds (600),
+ bind (&SchedulerFixture::setCounterThree, this));
+
+ 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);
+
+ BOOST_CHECK_NO_THROW (delete scheduler);
+}
+
+BOOST_AUTO_TEST_SUITE_END ()
+
+
+void funcUpdate( const std::string &/*prefix*/, const SeqNo &/*newSeq*/, const SeqNo &/*oldSeq*/ )
+{
+}
+
+void funcRemove( const std::string &/*prefix*/ )
+{
+}
+
+BOOST_AUTO_TEST_CASE (SyncLogicTest)
+{
+ SyncLogic *logic = 0;
+ BOOST_CHECK_NO_THROW (logic = new SyncLogic ("/prefix", funcUpdate, funcRemove, make_shared<CcnxWrapper> ()));
+
+ Scheduler &scheduler = logic->getScheduler ();
+ BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 0);
+
+ BOOST_CHECK_NO_THROW (logic->respondSyncInterest ("/prefix/e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e"));
+ BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 1);
+
+ this_thread::sleep (posix_time::milliseconds (100)); // max waiting time
+ BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 0);
+
+ 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 (), 4);
+
+ this_thread::sleep (posix_time::milliseconds (19)); // min waiting time is 20
+ BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 4);
+
+ this_thread::sleep (posix_time::milliseconds (100)); // max waiting time
+ BOOST_CHECK_EQUAL (scheduler.getEventsSize (), 0);
+
+ BOOST_CHECK_NO_THROW (delete logic);
+}
+
+#endif