Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 1 | #include "fs-watcher.h" |
| 2 | #include <boost/make_shared.hpp> |
| 3 | #include <boost/filesystem.hpp> |
| 4 | #include <boost/test/unit_test.hpp> |
| 5 | #include <boost/thread/thread.hpp> |
| 6 | #include <boost/bind.hpp> |
| 7 | #include <boost/lexical_cast.hpp> |
| 8 | #include <boost/filesystem/fstream.hpp> |
| 9 | #include <fstream> |
| 10 | #include <set> |
| 11 | #include <QtGui> |
| 12 | #include <iostream> |
| 13 | |
| 14 | using namespace std; |
| 15 | using namespace boost; |
| 16 | namespace fs = boost::filesystem; |
| 17 | |
| 18 | BOOST_AUTO_TEST_SUITE(TestFsWatcherDelay) |
| 19 | |
| 20 | void |
| 21 | onChange(const fs::path &file) |
| 22 | { |
| 23 | cerr << "onChange called" << endl; |
| 24 | } |
| 25 | |
| 26 | void |
| 27 | onDelete(const fs::path &file) |
| 28 | { |
| 29 | cerr << "onDelete called" << endl; |
| 30 | } |
| 31 | |
| 32 | void run(fs::path dir, FsWatcher::LocalFile_Change_Callback c, FsWatcher::LocalFile_Change_Callback d) |
| 33 | { |
| 34 | int x = 0; |
| 35 | QCoreApplication app (x, 0); |
| 36 | FsWatcher watcher (dir.string().c_str(), c, d); |
| 37 | app.exec(); |
| 38 | sleep(100); |
| 39 | } |
| 40 | |
| 41 | void SlowWrite(fs::path & file) |
| 42 | { |
| 43 | fs::ofstream off(file, std::ios::out); |
| 44 | |
| 45 | for (int i = 0; i < 10; i++){ |
| 46 | off << i << endl; |
| 47 | usleep(200000); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE (TestFsWatcherDelay) |
| 52 | { |
| 53 | fs::path dir = fs::absolute(fs::path("TestFsWatcher")); |
| 54 | if (fs::exists(dir)) |
| 55 | { |
| 56 | fs::remove_all(dir); |
| 57 | } |
| 58 | |
| 59 | fs::create_directory(dir); |
| 60 | |
| 61 | FsWatcher::LocalFile_Change_Callback fileChange = boost::bind(onChange, _1); |
| 62 | FsWatcher::LocalFile_Change_Callback fileDelete = boost::bind(onDelete, _1); |
| 63 | |
| 64 | fs::path file = dir / "test.text"; |
| 65 | |
| 66 | thread watcherThread(run, dir, fileChange, fileDelete); |
| 67 | |
| 68 | thread writeThread(SlowWrite, file); |
| 69 | |
| 70 | |
| 71 | |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame^] | 72 | usleep(10000000); |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 73 | |
| 74 | // cleanup |
| 75 | if (fs::exists(dir)) |
| 76 | { |
| 77 | fs::remove_all(dir); |
| 78 | } |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame^] | 79 | |
| 80 | usleep(1000000); |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | BOOST_AUTO_TEST_SUITE_END() |