Skip to content

Commit

Permalink
Refs #7992: Initial basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasLohnert committed Jun 22, 2017
1 parent 9744a50 commit b754d85
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Framework/LiveData/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set ( SRC_FILES
src/ADARA/ADARAParser.cpp
src/FakeEventDataListener.cpp
src/FileEventDataListener.cpp
src/FileWatcher.cpp
src/ISIS/DAE/idc.cpp
src/ISIS/DAE/isisds_command.cpp
src/ISIS/FakeISISEventDAE.cpp
Expand All @@ -27,6 +28,7 @@ set ( INC_FILES
inc/MantidLiveData/Exception.h
inc/MantidLiveData/FakeEventDataListener.h
inc/MantidLiveData/FileEventDataListener.h
inc/MantidLiveData/FileWatcher.h
inc/MantidLiveData/ISIS/FakeISISEventDAE.h
inc/MantidLiveData/ISIS/FakeISISHistoDAE.h
inc/MantidLiveData/ISIS/ISISHistoDataListener.h
Expand All @@ -47,6 +49,7 @@ set ( TEST_FILES
ADARAPacketTest.h
FakeEventDataListenerTest.h
FileEventDataListenerTest.h
FileWatcherTest.h
ISISHistoDataListenerTest.h
LiveDataAlgorithmTest.h
LoadLiveDataTest.h
Expand Down
59 changes: 59 additions & 0 deletions Framework/LiveData/inc/MantidLiveData/FileWatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#ifndef MANTID_LIVEDATA_FILEWATCHER_H_
#define MANTID_LIVEDATA_FILEWATCHER_H_

#include "Poco/DirectoryWatcher.h"
#include "Poco/Delegate.h"
#include "Poco/FileStream.h"
#include "MantidKernel/System.h"

namespace Mantid {
namespace LiveData {

/** FileWatcher : TODO: DESCRIPTION
Copyright © 2017 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge
National Laboratory & European Spallation Source
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport FileWatcher {
public:
FileWatcher(std::string path);
//~FileWatcher();
std::string get_path();
bool hasChanged();
std::vector<Poco::File> changedFiles;

void start();

private:
bool m_changed;
std::string m_path;

void onItemAdded(const Poco::DirectoryWatcher::DirectoryEvent& ev);
void onItemRemoved(const Poco::DirectoryWatcher::DirectoryEvent & ev);
void onItemModified(const Poco::DirectoryWatcher::DirectoryEvent & ev);
void onItemMovedFrom(const Poco::DirectoryWatcher::DirectoryEvent & ev);
void onItemMovedTo(const Poco::DirectoryWatcher::DirectoryEvent & ev);
};

} // namespace LiveData
} // namespace Mantid

#endif /* MANTID_LIVEDATA_FILEWATCHER_H_ */
69 changes: 69 additions & 0 deletions Framework/LiveData/src/FileWatcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "MantidLiveData/FileWatcher.h"
#include <iostream>

namespace Mantid {
namespace LiveData {
/**
* The constructor
*/
FileWatcher::FileWatcher(std::string path)
: m_path(path), m_changed(false) {}

std::string FileWatcher::get_path() {
return m_path;
}

bool FileWatcher::hasChanged() {
return m_changed;
}

void FileWatcher::start() {
Poco::DirectoryWatcher dw(m_path, Poco::DirectoryWatcher::DW_FILTER_ENABLE_ALL, 2);
dw.itemAdded += Poco::delegate(this, &FileWatcher::onItemAdded);
dw.itemRemoved += Poco::delegate(this, &FileWatcher::onItemRemoved);
dw.itemModified += Poco::delegate(this, &FileWatcher::onItemModified);
dw.itemMovedFrom += Poco::delegate(this, &FileWatcher::onItemMovedFrom);
dw.itemMovedTo += Poco::delegate(this, &FileWatcher::onItemMovedTo);
while (true) {}
}

void FileWatcher::onItemAdded(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::cout << "ADDED: item at " << ev.item.path() << std::endl;
m_changed = true;
}

void FileWatcher::onItemRemoved(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::cout << "REMOVED: item at " << ev.item.path() << std::endl;
m_changed = true;
}

void FileWatcher::onItemModified(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::cout << "MODIFIED: item at " << ev.item.path() << std::endl;
m_changed = true;
}

// Never happens (on win)?
void FileWatcher::onItemMovedFrom(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::cout << "MOVED FROM: item at " << ev.item.path() << std::endl;
m_changed = true;
}

// Never happens (on win)?
void FileWatcher::onItemMovedTo(const Poco::DirectoryWatcher::DirectoryEvent& ev)
{
std::cout << "MOVED TO: item at " << ev.item.path() << std::endl;
m_changed = true;
}

// TODO set changed method

// TODO read

// TODO white/blacklist files? (regex?)

} // namespace LiveData
} // namespace Mantid
62 changes: 62 additions & 0 deletions Framework/LiveData/test/FileWatcherTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef MANTID_LIVEDATA_FILEWATCHERTEST_H_
#define MANTID_LIVEDATA_FILEWATCHERTEST_H_

#include <cxxtest/TestSuite.h>
#include <fstream>
#include <stdio.h>

#include "MantidLiveData/FileWatcher.h"

static const std::string folderPath = "C:\\Instrument\\FileWatcherTestBed\\";
static std::vector<std::string> testFiles;
using Mantid::LiveData::FileWatcher;

class FileWatcherTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor isn't called when running other tests
static FileWatcherTest *createSuite() { return new FileWatcherTest(); }
static void destroySuite( FileWatcherTest *suite ) { delete suite; }

void test_pathIsSet()
{
FileWatcher fw(folderPath);
TS_ASSERT_EQUALS(fw.get_path(), folderPath);
}

void test_flagIsFalse(){
FileWatcher fw(folderPath);
TS_ASSERT_EQUALS(fw.hasChanged(), false);
}

void test_flagSetToTrueIfFileAdded() {
FileWatcher fw(folderPath);
fw.start();

std::ofstream testFile = openFile("example.txt");
testFile.close();

Sleep(1000);
TS_ASSERT_EQUALS(fw.hasChanged(), true);
}
std::ofstream openFile(std::string fileName) {
std::ofstream file;

// check doesnt exist
std::string filePath = folderPath + fileName;
testFiles.push_back(filePath);

file.open(filePath);
return file;
}

void tearDown() override {
// Remove all files created
for (auto const& filePath : testFiles) {
remove(filePath.c_str());
}
}

};

#endif /* MANTID_LIVEDATA_FILEWATCHERTEST_H_ */

0 comments on commit b754d85

Please sign in to comment.