blob: b647487405b3f04d683c2a7773b4c81059ca372f [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
4 *
5 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
6 *
7 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
19 */
20
Yingdi Yua264b872013-07-10 18:07:23 -070021#include "fs-watcher.h"
22#include <boost/make_shared.hpp>
23#include <boost/filesystem.hpp>
24#include <boost/test/unit_test.hpp>
25#include <boost/thread/thread.hpp>
26#include <boost/bind.hpp>
27#include <boost/lexical_cast.hpp>
28#include <boost/filesystem/fstream.hpp>
29#include <fstream>
30#include <set>
31#include <QtGui>
32#include <iostream>
33
34using namespace std;
35using namespace boost;
36namespace fs = boost::filesystem;
37
38BOOST_AUTO_TEST_SUITE(TestFsWatcherDelay)
39
40void
41onChange(const fs::path &file)
42{
43 cerr << "onChange called" << endl;
44}
45
46void
47onDelete(const fs::path &file)
48{
49 cerr << "onDelete called" << endl;
50}
51
52void run(fs::path dir, FsWatcher::LocalFile_Change_Callback c, FsWatcher::LocalFile_Change_Callback d)
53{
54 int x = 0;
55 QCoreApplication app (x, 0);
56 FsWatcher watcher (dir.string().c_str(), c, d);
57 app.exec();
58 sleep(100);
59}
60
61void SlowWrite(fs::path & file)
62{
63 fs::ofstream off(file, std::ios::out);
Alexander Afanasyeve83c0562016-12-24 10:20:41 -080064
Yingdi Yua264b872013-07-10 18:07:23 -070065 for (int i = 0; i < 10; i++){
66 off << i << endl;
67 usleep(200000);
68 }
69}
70
71BOOST_AUTO_TEST_CASE (TestFsWatcherDelay)
72{
73 fs::path dir = fs::absolute(fs::path("TestFsWatcher"));
74 if (fs::exists(dir))
75 {
76 fs::remove_all(dir);
77 }
78
79 fs::create_directory(dir);
80
81 FsWatcher::LocalFile_Change_Callback fileChange = boost::bind(onChange, _1);
82 FsWatcher::LocalFile_Change_Callback fileDelete = boost::bind(onDelete, _1);
83
84 fs::path file = dir / "test.text";
Alexander Afanasyeve83c0562016-12-24 10:20:41 -080085
Yingdi Yua264b872013-07-10 18:07:23 -070086 thread watcherThread(run, dir, fileChange, fileDelete);
87
88 thread writeThread(SlowWrite, file);
Alexander Afanasyeve83c0562016-12-24 10:20:41 -080089
90
Yingdi Yua264b872013-07-10 18:07:23 -070091
Yingdi Yu57f667b2013-07-11 10:37:59 -070092 usleep(10000000);
Yingdi Yua264b872013-07-10 18:07:23 -070093
94 // cleanup
95 if (fs::exists(dir))
96 {
97 fs::remove_all(dir);
98 }
Yingdi Yu57f667b2013-07-11 10:37:59 -070099
100 usleep(1000000);
Yingdi Yua264b872013-07-10 18:07:23 -0700101}
102
103BOOST_AUTO_TEST_SUITE_END()