| #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(10000000); |
| |
| // cleanup |
| if (fs::exists(dir)) |
| { |
| fs::remove_all(dir); |
| } |
| |
| usleep(1000000); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() |