fs-watcher: Add fs-watcher delay test
Change-Id: I2a367b9695a5343426f3628c67a98c927cb0d38f
diff --git a/fs-watcher/fs-watcher.cc b/fs-watcher/fs-watcher.cc
index 747bd30..06e849f 100644
--- a/fs-watcher/fs-watcher.cc
+++ b/fs-watcher/fs-watcher.cc
@@ -75,7 +75,7 @@
void
FsWatcher::DidDirectoryChanged (QString dirPath)
{
- _LOG_DEBUG ("Triggered DirPath: " << dirPath.toStdString ());
+ _LOG_DEBUG ("Triggered DirPath(DidDirectoryChanged): " << dirPath.toStdString ());
filesystem::path absPathTriggeredDir (dirPath.toStdString ());
if (!filesystem::exists (filesystem::path (absPathTriggeredDir)))
@@ -145,7 +145,7 @@
void
FsWatcher::ScanDirectory_NotifyUpdates_Execute (QString dirPath)
{
- _LOG_TRACE (" >> ScanDirectory_NotifyUpdates_Execute");
+ _LOG_TRACE (" >> ScanDirectory_NotifyUpdates_Execute " << dirPath.toStdString());
// exclude working only on last component, not the full path; iterating through all directories, even excluded from monitoring
QRegExp exclude ("^(\\.|\\.\\.|\\.chronoshare|.*~|.*\\.swp)$");
diff --git a/test/test-fs-watcher-delay.cc b/test/test-fs-watcher-delay.cc
new file mode 100644
index 0000000..e658e2a
--- /dev/null
+++ b/test/test-fs-watcher-delay.cc
@@ -0,0 +1,81 @@
+#include "fs-watcher.h"
+#include <boost/make_shared.hpp>
+#include <boost/filesystem.hpp>
+#include <boost/test/unit_test.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/bind.hpp>
+#include <boost/lexical_cast.hpp>
+#include <boost/filesystem/fstream.hpp>
+#include <fstream>
+#include <set>
+#include <QtGui>
+#include <iostream>
+
+using namespace std;
+using namespace boost;
+namespace fs = boost::filesystem;
+
+BOOST_AUTO_TEST_SUITE(TestFsWatcherDelay)
+
+void
+onChange(const fs::path &file)
+{
+ cerr << "onChange called" << endl;
+}
+
+void
+onDelete(const fs::path &file)
+{
+ cerr << "onDelete called" << endl;
+}
+
+void run(fs::path dir, FsWatcher::LocalFile_Change_Callback c, FsWatcher::LocalFile_Change_Callback d)
+{
+ int x = 0;
+ QCoreApplication app (x, 0);
+ FsWatcher watcher (dir.string().c_str(), c, d);
+ app.exec();
+ sleep(100);
+}
+
+void SlowWrite(fs::path & file)
+{
+ fs::ofstream off(file, std::ios::out);
+
+ for (int i = 0; i < 10; i++){
+ off << i << endl;
+ usleep(200000);
+ }
+}
+
+BOOST_AUTO_TEST_CASE (TestFsWatcherDelay)
+{
+ fs::path dir = fs::absolute(fs::path("TestFsWatcher"));
+ if (fs::exists(dir))
+ {
+ fs::remove_all(dir);
+ }
+
+ fs::create_directory(dir);
+
+ FsWatcher::LocalFile_Change_Callback fileChange = boost::bind(onChange, _1);
+ FsWatcher::LocalFile_Change_Callback fileDelete = boost::bind(onDelete, _1);
+
+ fs::path file = dir / "test.text";
+
+ thread watcherThread(run, dir, fileChange, fileDelete);
+
+ thread writeThread(SlowWrite, file);
+
+
+
+ usleep(2000000);
+
+ // cleanup
+ if (fs::exists(dir))
+ {
+ fs::remove_all(dir);
+ }
+}
+
+BOOST_AUTO_TEST_SUITE_END()
diff --git a/test/test-fs-watcher.cc b/test/test-fs-watcher.cc
index 92fb516..d943903 100644
--- a/test/test-fs-watcher.cc
+++ b/test/test-fs-watcher.cc
@@ -8,6 +8,7 @@
#include <fstream>
#include <set>
#include <QtGui>
+#include <iostream>
using namespace std;
using namespace boost;
@@ -18,6 +19,7 @@
void
onChange(set<string> &files, const fs::path &file)
{
+ cerr << "onChange called" << endl;
files.insert(file.string());
}
@@ -70,7 +72,7 @@
// ============ check create file detection ================
create_file(dir / "test.txt", "hello");
// have to at least wait 0.5 seconds
- usleep(600000);
+ usleep(1000000);
// test.txt
BOOST_CHECK_EQUAL(files.size(), 1);
BOOST_CHECK(files.find("test.txt") != files.end());