blob: 0ccd5ccf8e0a974fabd6df592554d714752a241c [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"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080022#include <boost/bind.hpp>
Yingdi Yua264b872013-07-10 18:07:23 -070023#include <boost/filesystem.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080024#include <boost/filesystem/fstream.hpp>
25#include <boost/lexical_cast.hpp>
26#include <boost/make_shared.hpp>
Yingdi Yua264b872013-07-10 18:07:23 -070027#include <boost/test/unit_test.hpp>
28#include <boost/thread/thread.hpp>
Yingdi Yua264b872013-07-10 18:07:23 -070029#include <QtGui>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080030#include <fstream>
Yingdi Yua264b872013-07-10 18:07:23 -070031#include <iostream>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080032#include <set>
Yingdi Yua264b872013-07-10 18:07:23 -070033
34using namespace std;
35using namespace boost;
36namespace fs = boost::filesystem;
37
38BOOST_AUTO_TEST_SUITE(TestFsWatcherDelay)
39
40void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041onChange(const fs::path& file)
Yingdi Yua264b872013-07-10 18:07:23 -070042{
43 cerr << "onChange called" << endl;
44}
45
46void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047onDelete(const fs::path& file)
Yingdi Yua264b872013-07-10 18:07:23 -070048{
49 cerr << "onDelete called" << endl;
50}
51
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052void
53run(fs::path dir, FsWatcher::LocalFile_Change_Callback c, FsWatcher::LocalFile_Change_Callback d)
Yingdi Yua264b872013-07-10 18:07:23 -070054{
55 int x = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080056 QCoreApplication app(x, 0);
57 FsWatcher watcher(dir.string().c_str(), c, d);
Yingdi Yua264b872013-07-10 18:07:23 -070058 app.exec();
59 sleep(100);
60}
61
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080062void
63SlowWrite(fs::path& file)
Yingdi Yua264b872013-07-10 18:07:23 -070064{
65 fs::ofstream off(file, std::ios::out);
Alexander Afanasyeve83c0562016-12-24 10:20:41 -080066
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080067 for (int i = 0; i < 10; i++) {
68 off << i << endl;
Yingdi Yua264b872013-07-10 18:07:23 -070069 usleep(200000);
70 }
71}
72
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073BOOST_AUTO_TEST_CASE(TestFsWatcherDelay)
Yingdi Yua264b872013-07-10 18:07:23 -070074{
75 fs::path dir = fs::absolute(fs::path("TestFsWatcher"));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080076 if (fs::exists(dir)) {
Yingdi Yua264b872013-07-10 18:07:23 -070077 fs::remove_all(dir);
78 }
79
80 fs::create_directory(dir);
81
82 FsWatcher::LocalFile_Change_Callback fileChange = boost::bind(onChange, _1);
83 FsWatcher::LocalFile_Change_Callback fileDelete = boost::bind(onDelete, _1);
84
85 fs::path file = dir / "test.text";
Alexander Afanasyeve83c0562016-12-24 10:20:41 -080086
Yingdi Yua264b872013-07-10 18:07:23 -070087 thread watcherThread(run, dir, fileChange, fileDelete);
88
89 thread writeThread(SlowWrite, file);
Alexander Afanasyeve83c0562016-12-24 10:20:41 -080090
91
Yingdi Yu57f667b2013-07-11 10:37:59 -070092 usleep(10000000);
Yingdi Yua264b872013-07-10 18:07:23 -070093
94 // cleanup
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080095 if (fs::exists(dir)) {
Yingdi Yua264b872013-07-10 18:07:23 -070096 fs::remove_all(dir);
97 }
Yingdi Yu57f667b2013-07-11 10:37:59 -070098
99 usleep(1000000);
Yingdi Yua264b872013-07-10 18:07:23 -0700100}
101
102BOOST_AUTO_TEST_SUITE_END()