core: global io_service and scheduler

Scheduler and time are imported from ndn-cpp-dev.
Forwarder is using the new scheduler API.
Face system is not transitioned yet.

refs #1290

Change-Id: I5679cb50bbf9890a105f663b038f13951403c2b6
diff --git a/tests/core/scheduler.cpp b/tests/core/scheduler.cpp
index 77ca1e9..d6b51fa 100644
--- a/tests/core/scheduler.cpp
+++ b/tests/core/scheduler.cpp
@@ -21,7 +21,7 @@
     , count4(0)
   {
   }
-    
+
   void
   event1()
   {
@@ -47,7 +47,7 @@
   {
     ++count4;
   }
-  
+
   int count1;
   int count2;
   int count3;
@@ -56,23 +56,23 @@
 
 BOOST_FIXTURE_TEST_CASE(Events, SchedulerFixture)
 {
-  boost::asio::io_service io; 
+  resetGlobalIoService();
 
-  Scheduler scheduler(io);
-  scheduler.scheduleEvent(time::seconds(0.5), bind(&SchedulerFixture::event1, this));
-  
-  EventId i = scheduler.scheduleEvent(time::seconds(1.0), bind(&SchedulerFixture::event2, this));
-  scheduler.cancelEvent(i);
+  scheduler::schedule(time::seconds(0.5), bind(&SchedulerFixture::event1, this));
 
-  scheduler.scheduleEvent(time::seconds(0.25), bind(&SchedulerFixture::event3, this));
+  EventId i = scheduler::schedule(time::seconds(1.0), bind(&SchedulerFixture::event2, this));
+  scheduler::cancel(i);
 
-  i = scheduler.scheduleEvent(time::seconds(0.05), bind(&SchedulerFixture::event2, this));
-  scheduler.cancelEvent(i);
+  scheduler::schedule(time::seconds(0.25), bind(&SchedulerFixture::event3, this));
 
-  i = scheduler.schedulePeriodicEvent(time::seconds(0.3), time::seconds(0.1), bind(&SchedulerFixture::event4, this));
-  scheduler.scheduleEvent(time::seconds(1), bind(&Scheduler::cancelEvent, &scheduler, i));
-  
-  io.run();
+  i = scheduler::schedule(time::seconds(0.05), bind(&SchedulerFixture::event2, this));
+  scheduler::cancel(i);
+
+  // TODO deprecate periodic event
+  i = scheduler::getGlobalScheduler().schedulePeriodicEvent(time::seconds(0.3), time::seconds(0.1), bind(&SchedulerFixture::event4, this));
+  scheduler::schedule(time::seconds(1), bind(&scheduler::cancel, i));
+
+  getGlobalIoService().run();
 
   BOOST_CHECK_EQUAL(count1, 1);
   BOOST_CHECK_EQUAL(count2, 0);
@@ -82,37 +82,29 @@
 
 BOOST_AUTO_TEST_CASE(CancelEmptyEvent)
 {
-  boost::asio::io_service io; 
-  Scheduler scheduler(io);
-  
   EventId i;
-  scheduler.cancelEvent(i);
+  scheduler::cancel(i);
 }
 
 struct SelfCancelFixture
 {
-  SelfCancelFixture()
-    : m_scheduler(m_io)
-  {
-  }
-  
   void
   cancelSelf()
   {
-    m_scheduler.cancelEvent(m_selfEventId);
+    scheduler::cancel(m_selfEventId);
   }
-  
-  boost::asio::io_service m_io; 
-  Scheduler m_scheduler;
+
   EventId m_selfEventId;
 };
 
 BOOST_FIXTURE_TEST_CASE(SelfCancel, SelfCancelFixture)
 {
-  m_selfEventId = m_scheduler.scheduleEvent(time::seconds(0.1),
-                                            bind(&SelfCancelFixture::cancelSelf, this));
-  
-  BOOST_REQUIRE_NO_THROW(m_io.run());
+  resetGlobalIoService();
+
+  m_selfEventId = scheduler::schedule(time::seconds(0.1),
+                                      bind(&SelfCancelFixture::cancelSelf, this));
+
+  BOOST_REQUIRE_NO_THROW(getGlobalIoService().run());
 }
 
 BOOST_AUTO_TEST_SUITE_END()
diff --git a/tests/core/time.cpp b/tests/core/time.cpp
deleted file mode 100644
index 6d0db3f..0000000
--- a/tests/core/time.cpp
+++ /dev/null
@@ -1,47 +0,0 @@
-/* -*- 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/time.hpp"
-
-#include <boost/test/unit_test.hpp>
-
-namespace nfd {
-
-BOOST_AUTO_TEST_SUITE(CoreTime)
-
-BOOST_AUTO_TEST_CASE(Now)
-{
-  time::Point p1 = time::now();
-  time::Point p2 = time::now();
-  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 nfd
diff --git a/tests/fw/broadcast-strategy.cpp b/tests/fw/broadcast-strategy.cpp
index 3976f62..15928be 100644
--- a/tests/fw/broadcast-strategy.cpp
+++ b/tests/fw/broadcast-strategy.cpp
@@ -16,8 +16,8 @@
 
 BOOST_AUTO_TEST_CASE(ForwardTwo)
 {
-  boost::asio::io_service io;
-  Forwarder forwarder(io);
+  resetGlobalIoService();
+  Forwarder forwarder;
   typedef StrategyTester<fw::BroadcastStrategy> BroadcastStrategyTester;
   BroadcastStrategyTester strategy(forwarder);
 
@@ -60,8 +60,8 @@
 
 BOOST_AUTO_TEST_CASE(Rebuff)
 {
-  boost::asio::io_service io;
-  Forwarder forwarder(io);
+  resetGlobalIoService();
+  Forwarder forwarder;
   typedef StrategyTester<fw::BroadcastStrategy> BroadcastStrategyTester;
   BroadcastStrategyTester strategy(forwarder);
 
diff --git a/tests/fw/forwarder.cpp b/tests/fw/forwarder.cpp
index e066f52..daa80c7 100644
--- a/tests/fw/forwarder.cpp
+++ b/tests/fw/forwarder.cpp
@@ -18,14 +18,14 @@
     : m_ioService(ioService)
   {
   }
-  
+
   virtual void
   sendInterest(const Interest& interest)
   {
     m_sentInterests.push_back(interest);
     m_ioService.stop();
   }
-  
+
   virtual void
   sendData(const Data& data)
   {
@@ -43,7 +43,7 @@
   {
     onReceiveInterest(interest);
   }
-  
+
   void
   receiveData(const Data& data)
   {
@@ -62,15 +62,15 @@
 
 BOOST_AUTO_TEST_CASE(AddRemoveFace)
 {
-  boost::asio::io_service io;
-  Forwarder forwarder(io);
-  
+  resetGlobalIoService();
+  Forwarder forwarder;
+
   shared_ptr<Face> face1 = make_shared<DummyFace>();
   shared_ptr<Face> face2 = make_shared<DummyFace>();
-  
+
   BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
   BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
-  
+
   forwarder.addFace(face1);
   forwarder.addFace(face2);
 
@@ -87,34 +87,35 @@
 
 BOOST_AUTO_TEST_CASE(SimpleExchange)
 {
+  resetGlobalIoService();
+  boost::asio::io_service& io = getGlobalIoService();
+  Forwarder forwarder;
+
   Name nameA  ("ndn:/A");
   Name nameAB ("ndn:/A/B");
   Name nameABC("ndn:/A/B/C");
   Interest interestAB(nameAB);
   interestAB.setInterestLifetime(4000);
   Data dataABC(nameABC);
-  
-  boost::asio::io_service io;
-  Forwarder forwarder(io);
 
   shared_ptr<ForwarderTestFace> face1 = make_shared<ForwarderTestFace>(boost::ref(io));
   shared_ptr<ForwarderTestFace> face2 = make_shared<ForwarderTestFace>(boost::ref(io));
   forwarder.addFace(face1);
   forwarder.addFace(face2);
-  
+
   Fib& fib = forwarder.getFib();
   std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
     fib.insert(Name("ndn:/A"));
   shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
   fibEntry->addNextHop(face2, 0);
-  
+
   face1->receiveInterest(interestAB);
   io.run();
   io.reset();
   BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
   BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
   BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
-  
+
   face2->receiveData(dataABC);
   io.run();
   io.reset();
@@ -126,9 +127,7 @@
 class ScopeLocalhostTestForwarder : public Forwarder
 {
 public:
-  explicit
-  ScopeLocalhostTestForwarder(boost::asio::io_service& ioService)
-    : Forwarder(ioService)
+  ScopeLocalhostTestForwarder()
   {
   }
 
@@ -155,33 +154,33 @@
 
 BOOST_AUTO_TEST_CASE(ScopeLocalhost)
 {
-  boost::asio::io_service io;
-  ScopeLocalhostTestForwarder forwarder(io);
+  resetGlobalIoService();
+  ScopeLocalhostTestForwarder forwarder;
   shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>();
   shared_ptr<DummyFace>      face2 = make_shared<DummyFace>();
   forwarder.addFace(face1);
   forwarder.addFace(face2);
-  
+
   // local face, /localhost: OK
   forwarder.m_dispatchToStrategy_count = 0;
   forwarder.onIncomingInterest(*face1, Interest(Name("/localhost/A1")));
   BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
-  
+
   // non-local face, /localhost: violate
   forwarder.m_dispatchToStrategy_count = 0;
   forwarder.onIncomingInterest(*face2, Interest(Name("/localhost/A2")));
   BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
-  
+
   // local face, non-/localhost: OK
   forwarder.m_dispatchToStrategy_count = 0;
   forwarder.onIncomingInterest(*face1, Interest(Name("/A3")));
   BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
-  
+
   // non-local face, non-/localhost: OK
   forwarder.m_dispatchToStrategy_count = 0;
   forwarder.onIncomingInterest(*face2, Interest(Name("/A4")));
   BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
-  
+
   // local face, /localhost: OK
   forwarder.m_onDataUnsolicited_count = 0;
   forwarder.onIncomingData(*face1, Data(Name("/localhost/B1")));
@@ -191,7 +190,7 @@
   forwarder.m_onDataUnsolicited_count = 0;
   forwarder.onIncomingData(*face2, Data(Name("/localhost/B2")));
   BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
-  
+
   // local face, non-/localhost: OK
   forwarder.m_onDataUnsolicited_count = 0;
   forwarder.onIncomingData(*face1, Data(Name("/B3")));
diff --git a/tests/table/measurements-accessor.cpp b/tests/table/measurements-accessor.cpp
index 789d02a..9738701 100644
--- a/tests/table/measurements-accessor.cpp
+++ b/tests/table/measurements-accessor.cpp
@@ -21,10 +21,10 @@
     : Strategy(forwarder)
   {
   }
-  
+
   virtual
   ~MeasurementsAccessorTestStrategy()
-  
+
   {
   }
 
@@ -36,7 +36,7 @@
   {
     BOOST_ASSERT(false);
   }
-  
+
 public: // accessors
   MeasurementsAccessor&
   getMeasurements_accessor()
@@ -47,36 +47,37 @@
 
 BOOST_AUTO_TEST_CASE(Access)
 {
-  boost::asio::io_service ioService;
-  Forwarder forwarder(ioService);
+  resetGlobalIoService();
+  Forwarder forwarder;
+
   shared_ptr<MeasurementsAccessorTestStrategy> strategy1 =
     make_shared<MeasurementsAccessorTestStrategy>(boost::ref(forwarder));
   shared_ptr<MeasurementsAccessorTestStrategy> strategy2 =
     make_shared<MeasurementsAccessorTestStrategy>(boost::ref(forwarder));
-  
+
   Name nameRoot("ndn:/");
   Name nameA   ("ndn:/A");
   Name nameAB  ("ndn:/A/B");
   Name nameABC ("ndn:/A/B/C");
   Name nameAD  ("ndn:/A/D");
-  
+
   Fib& fib = forwarder.getFib();
   fib.insert(nameRoot).first->setStrategy(strategy1);
   fib.insert(nameA   ).first->setStrategy(strategy2);
   fib.insert(nameAB  ).first->setStrategy(strategy1);
-  
+
   MeasurementsAccessor& accessor1 = strategy1->getMeasurements_accessor();
   MeasurementsAccessor& accessor2 = strategy2->getMeasurements_accessor();
-  
+
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameRoot)), true);
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameA   )), false);
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameAB  )), true);
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameABC )), true);
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor1.get(nameAD  )), false);
-  
+
   shared_ptr<measurements::Entry> entryRoot = forwarder.getMeasurements().get(nameRoot);
   BOOST_CHECK_NO_THROW(accessor1.getParent(entryRoot));
-  
+
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameRoot)), false);
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameA   )), true);
   BOOST_CHECK_EQUAL(static_cast<bool>(accessor2.get(nameAB  )), false);
diff --git a/tests/table/measurements.cpp b/tests/table/measurements.cpp
index 09874a0..4643fb8 100644
--- a/tests/table/measurements.cpp
+++ b/tests/table/measurements.cpp
@@ -14,13 +14,13 @@
 
 BOOST_AUTO_TEST_CASE(Get_Parent)
 {
+  resetGlobalIoService();
+  Measurements measurements;
+
   Name name0;
   Name nameA ("ndn:/A");
   Name nameAB("ndn:/A/B");
 
-  boost::asio::io_service ioService;
-  Measurements measurements(ioService);
-
   shared_ptr<measurements::Entry> entryAB = measurements.get(nameAB);
   BOOST_REQUIRE(static_cast<bool>(entryAB));
   BOOST_CHECK_EQUAL(entryAB->getName(), nameAB);