blob: 162d1d47dc1726b539a447ac1e322dc4204be8f0 [file] [log] [blame]
Yingdi Yua264b872013-07-10 18:07:23 -07001#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
14using namespace std;
15using namespace boost;
16namespace fs = boost::filesystem;
17
18BOOST_AUTO_TEST_SUITE(TestFsWatcherDelay)
19
20void
21onChange(const fs::path &file)
22{
23 cerr << "onChange called" << endl;
24}
25
26void
27onDelete(const fs::path &file)
28{
29 cerr << "onDelete called" << endl;
30}
31
32void 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
41void 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
51BOOST_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 Yu57f667b2013-07-11 10:37:59 -070072 usleep(10000000);
Yingdi Yua264b872013-07-10 18:07:23 -070073
74 // cleanup
75 if (fs::exists(dir))
76 {
77 fs::remove_all(dir);
78 }
Yingdi Yu57f667b2013-07-11 10:37:59 -070079
80 usleep(1000000);
Yingdi Yua264b872013-07-10 18:07:23 -070081}
82
83BOOST_AUTO_TEST_SUITE_END()