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" |
| 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 | |
| 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | namespace fs = boost::filesystem; |
| 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(TestFsWatcherDelay) |
| 39 | |
| 40 | void |
| 41 | onChange(const fs::path &file) |
| 42 | { |
| 43 | cerr << "onChange called" << endl; |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | onDelete(const fs::path &file) |
| 48 | { |
| 49 | cerr << "onDelete called" << endl; |
| 50 | } |
| 51 | |
| 52 | void 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 | |
| 61 | void SlowWrite(fs::path & file) |
| 62 | { |
| 63 | fs::ofstream off(file, std::ios::out); |
Alexander Afanasyev | e83c056 | 2016-12-24 10:20:41 -0800 | [diff] [blame] | 64 | |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 65 | for (int i = 0; i < 10; i++){ |
| 66 | off << i << endl; |
| 67 | usleep(200000); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | BOOST_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 Afanasyev | e83c056 | 2016-12-24 10:20:41 -0800 | [diff] [blame] | 85 | |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 86 | thread watcherThread(run, dir, fileChange, fileDelete); |
| 87 | |
| 88 | thread writeThread(SlowWrite, file); |
Alexander Afanasyev | e83c056 | 2016-12-24 10:20:41 -0800 | [diff] [blame] | 89 | |
| 90 | |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 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 |
| 95 | if (fs::exists(dir)) |
| 96 | { |
| 97 | fs::remove_all(dir); |
| 98 | } |
Yingdi Yu | 57f667b | 2013-07-11 10:37:59 -0700 | [diff] [blame] | 99 | |
| 100 | usleep(1000000); |
Yingdi Yu | a264b87 | 2013-07-10 18:07:23 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_SUITE_END() |