core: Basic implementation of Scheduler
refs #1119 (http://redmine.named-data.net/issues/1119)
Change-Id: I1783857452941794a2538534d3492d30ac58dd25
diff --git a/tests/core/scheduler.cpp b/tests/core/scheduler.cpp
new file mode 100644
index 0000000..8a82426
--- /dev/null
+++ b/tests/core/scheduler.cpp
@@ -0,0 +1,85 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "core/scheduler.hpp"
+
+#include <boost/test/unit_test.hpp>
+
+namespace ndn {
+
+BOOST_AUTO_TEST_SUITE(CoreScheduler)
+
+struct SchedulerFixture
+{
+ SchedulerFixture()
+ : count1(0)
+ , count2(0)
+ , count3(0)
+ , count4(0)
+ {
+ }
+
+ void
+ event1()
+ {
+ BOOST_CHECK_EQUAL(count3, 1);
+ ++count1;
+ }
+
+ void
+ event2()
+ {
+ ++count2;
+ }
+
+ void
+ event3()
+ {
+ BOOST_CHECK_EQUAL(count1, 0);
+ ++count3;
+ }
+
+ void
+ event4()
+ {
+ ++count4;
+ }
+
+ int count1;
+ int count2;
+ int count3;
+ int count4;
+};
+
+BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
+{
+ boost::asio::io_service io;
+
+ Scheduler scheduler(io);
+ scheduler.scheduleEvent(time::seconds(0.1), bind(&SchedulerFixture::event1, this));
+
+ EventId i = scheduler.scheduleEvent(time::seconds(0.2), bind(&SchedulerFixture::event2, this));
+ scheduler.cancelEvent(i);
+
+ scheduler.scheduleEvent(time::seconds(0.05), bind(&SchedulerFixture::event3, this));
+
+ i = scheduler.scheduleEvent(time::seconds(0.01), bind(&SchedulerFixture::event2, this));
+ scheduler.cancelEvent(i);
+
+ i = scheduler.schedulePeriodicEvent(time::seconds(0.3), time::seconds(0.1), bind(&SchedulerFixture::event4, this));
+ scheduler.scheduleEvent(time::seconds(0.69), bind(&Scheduler::cancelEvent, &scheduler, i));
+
+ io.run();
+
+ BOOST_CHECK_EQUAL(count1, 1);
+ BOOST_CHECK_EQUAL(count2, 0);
+ BOOST_CHECK_EQUAL(count3, 1);
+ BOOST_CHECK_EQUAL(count4, 4);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace ndn
diff --git a/tests/core/time.cpp b/tests/core/time.cpp
index caae6d0..64ad830 100644
--- a/tests/core/time.cpp
+++ b/tests/core/time.cpp
@@ -19,6 +19,29 @@
BOOST_CHECK_LE(p1, p2);
}
+BOOST_AUTO_TEST_CASE(Operations)
+{
+ // helpers
+ BOOST_CHECK_GT(time::seconds(1), time::milliseconds(1));
+ BOOST_CHECK_GT(time::milliseconds(1), time::microseconds(1));
+ BOOST_CHECK_GT(time::microseconds(1), time::nanoseconds(1));
+
+ // duration operations + helpers
+ BOOST_CHECK_EQUAL(time::seconds(8) + time::microseconds(101), time::nanoseconds(8000101000));
+ BOOST_CHECK_EQUAL(time::seconds(7) - time::milliseconds(234), time::microseconds(6766000));
+
+ // point operations
+ time::Point p1 = time::now();
+ time::Point p2 = p1 + time::milliseconds(10000);
+ time::Point p3 = p2 - time::microseconds(5000000);
+ BOOST_CHECK_LE(p1, p2);
+ BOOST_CHECK_LE(p1, p3);
+ BOOST_CHECK_LE(p3, p2);
+
+ BOOST_CHECK_EQUAL(p2 - p1, time::seconds(10));
+ BOOST_CHECK_EQUAL(p3 - p1, time::seconds(5));
+}
+
BOOST_AUTO_TEST_SUITE_END()
} // namespace ndn