fs-watcher: Add fs-watcher delay test
Change-Id: I2a367b9695a5343426f3628c67a98c927cb0d38f
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()