fs-watcher: Fixed fs-watcher multiple onChange callback

Change-Id: Ied8c876e4fee238c960e091466e275823e83e899
diff --git a/fs-watcher/fs-watcher.cc b/fs-watcher/fs-watcher.cc
index 06e849f..2c83f09 100644
--- a/fs-watcher/fs-watcher.cc
+++ b/fs-watcher/fs-watcher.cc
@@ -124,9 +124,10 @@
       m_watcher->removePath (absFilePath);
       m_watcher->addPath (absFilePath);
 
-      Scheduler::scheduleOneTimeTask (m_scheduler, 0.5,
+      Scheduler::scheduleDelayOneTimeTask (m_scheduler, 0.5,
                                       bind (m_onChange, triggeredFile.relative_path ()),
                                       triggeredFile.relative_path ().string());
+
     }
   else
     {
diff --git a/scheduler/scheduler.cc b/scheduler/scheduler.cc
index 1e9aee7..209e3d1 100644
--- a/scheduler/scheduler.cc
+++ b/scheduler/scheduler.cc
@@ -175,6 +175,20 @@
     return TaskPtr ();
 }
 
+TaskPtr
+Scheduler::scheduleDelayOneTimeTask (SchedulerPtr scheduler, double delay,
+                                const Task::Callback &callback, const Task::Tag &tag)
+{
+  TaskPtr task = make_shared<OneTimeTask> (callback, tag, scheduler, delay);
+  if (scheduler->addTask (task))
+    return task;
+  else{
+    _LOG_ERROR ("reschedule task for " << tag);
+    scheduler->rescheduleTask(tag);
+    return TaskPtr ();
+  }
+}
+
 bool
 Scheduler::addTask(TaskPtr newTask, bool reset/* = true*/)
 {
diff --git a/scheduler/scheduler.h b/scheduler/scheduler.h
index 096be98..a52649a 100644
--- a/scheduler/scheduler.h
+++ b/scheduler/scheduler.h
@@ -70,6 +70,10 @@
   schedulePeriodicTask (SchedulerPtr scheduler, IntervalGeneratorPtr delayGenerator,
                         const Task::Callback &callback, const Task::Tag &tag);
 
+  // helper method to schedule/reschedule one-time task
+  static TaskPtr
+  scheduleDelayOneTimeTask (SchedulerPtr scheduler, double delay, const Task::Callback &callback, const Task::Tag &tag);
+
   // if task with the same tag exists, the task is not added and return false
   virtual bool
   addTask(TaskPtr task, bool reset = true);
diff --git a/test/test-fetch-manager.cc b/test/test-fetch-manager.cc
index 5abe47d..42d4b48 100644
--- a/test/test-fetch-manager.cc
+++ b/test/test-fetch-manager.cc
@@ -188,6 +188,45 @@
   executor->shutdown ();
 }
 
+
+BOOST_AUTO_TEST_CASE (TestFetcher2)
+{
+  INIT_LOGGERS ();
+
+  CcnxWrapperPtr ccnx = make_shared<CcnxWrapper> ();
+
+  Name baseName ("/base");
+  Name deviceName ("/device");
+  int i = 0;
+
+  ccnx->publishData (Name (baseName)(i), reinterpret_cast<const unsigned char*> (&i), sizeof(int), 30);
+
+
+  FetcherTestData data;
+  ExecutorPtr executor = make_shared<Executor>(1);
+  executor->start ();
+
+  Fetcher fetcher (ccnx,
+                   executor,
+                   bind (&FetcherTestData::onData, &data, _1, _2, _3, _4),
+                   bind (&FetcherTestData::finish, &data, _1, _2),
+                   bind (&FetcherTestData::onComplete, &data, _1),
+                   bind (&FetcherTestData::onFail, &data, _1),
+                   deviceName, Name ("/base"), 1, 1,
+                   boost::posix_time::seconds (5)); // this time is not precise
+
+  BOOST_CHECK_EQUAL (fetcher.IsActive (), false);
+  fetcher.RestartPipeline ();
+  BOOST_CHECK_EQUAL (fetcher.IsActive (), true);
+
+  usleep(7000000);
+  BOOST_CHECK_EQUAL (data.m_failed, true);
+
+  executor->shutdown ();
+}
+
+
+
 // BOOST_AUTO_TEST_CASE (CcnxWrapperSelector)
 // {
 
diff --git a/test/test-fs-watcher-delay.cc b/test/test-fs-watcher-delay.cc
index e658e2a..162d1d4 100644
--- a/test/test-fs-watcher-delay.cc
+++ b/test/test-fs-watcher-delay.cc
@@ -69,13 +69,15 @@
   
   
 
-  usleep(2000000);
+  usleep(10000000);
 
   // cleanup
   if (fs::exists(dir))
   {
     fs::remove_all(dir);
   }
+
+  usleep(1000000);
 }
 
 BOOST_AUTO_TEST_SUITE_END()