Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- 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 Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 21 | #include "fs-watcher.h" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 22 | #include <boost/bind.hpp> |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 23 | #include <boost/filesystem.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 24 | #include <boost/filesystem/fstream.hpp> |
| 25 | #include <boost/lexical_cast.hpp> |
| 26 | #include <boost/make_shared.hpp> |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 27 | #include <boost/test/unit_test.hpp> |
| 28 | #include <boost/thread/thread.hpp> |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 29 | #include <QtGui> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 30 | #include <fstream> |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 31 | #include <iostream> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 32 | #include <set> |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 33 | |
| 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | namespace fs = boost::filesystem; |
| 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(TestFsWatcherDelay) |
| 39 | |
| 40 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 41 | onChange(const fs::path& file) |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 42 | { |
| 43 | cerr << "onChange called" << endl; |
| 44 | } |
| 45 | |
| 46 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 47 | onDelete(const fs::path& file) |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 48 | { |
| 49 | cerr << "onDelete called" << endl; |
| 50 | } |
| 51 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 52 | void |
| 53 | run(fs::path dir, FsWatcher::LocalFile_Change_Callback c, FsWatcher::LocalFile_Change_Callback d) |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 54 | { |
| 55 | int x = 0; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | QCoreApplication app(x, 0); |
| 57 | FsWatcher watcher(dir.string().c_str(), c, d); |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 58 | app.exec(); |
| 59 | sleep(100); |
| 60 | } |
| 61 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 62 | void |
| 63 | SlowWrite(fs::path& file) |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 64 | { |
| 65 | fs::ofstream off(file, std::ios::out); |
Alexander Afanasyev | e83c056 | 2016-12-24 10:20:41 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 67 | for (int i = 0; i < 10; i++) { |
| 68 | off << i << endl; |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 69 | usleep(200000); |
| 70 | } |
| 71 | } |
| 72 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | BOOST_AUTO_TEST_CASE(TestFsWatcherDelay) |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 74 | { |
| 75 | fs::path dir = fs::absolute(fs::path("TestFsWatcher")); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | if (fs::exists(dir)) { |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 77 | 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 Afanasyev | e83c056 | 2016-12-24 10:20:41 -0800 | [diff] [blame] | 86 | |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 87 | thread watcherThread(run, dir, fileChange, fileDelete); |
| 88 | |
| 89 | thread writeThread(SlowWrite, file); |
Alexander Afanasyev | e83c056 | 2016-12-24 10:20:41 -0800 | [diff] [blame] | 90 | |
| 91 | |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 92 | usleep(10000000); |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 93 | |
| 94 | // cleanup |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 95 | if (fs::exists(dir)) { |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 96 | fs::remove_all(dir); |
| 97 | } |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 98 | |
| 99 | usleep(1000000); |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | BOOST_AUTO_TEST_SUITE_END() |