libmoost
|
00001 /* vim:set ts=3 sw=3 sts=3 et: */ 00028 #include <boost/test/unit_test.hpp> 00029 #include <boost/test/test_tools.hpp> 00030 00031 #include <boost/filesystem.hpp> 00032 #include <boost/thread.hpp> 00033 00034 #include "../../include/moost/io/ionotify.hpp" 00035 #include "../../include/moost/testing/test_directory_creator.hpp" 00036 00037 #include <vector> 00038 #include <fstream> 00039 00040 using namespace moost::io; 00041 00042 BOOST_AUTO_TEST_SUITE( ionotify_test ) 00043 00044 struct Fixture 00045 { 00046 moost::testing::test_directory_creator tdc; 00047 00048 00049 Fixture() 00050 { 00051 } 00052 00053 ~Fixture() 00054 { 00055 } 00056 00057 }; 00058 00059 namespace 00060 { 00061 typedef std::vector<ionotify::file_action> file_actions_t; 00062 00063 struct functor 00064 { 00065 functor(file_actions_t & file_actions) : file_actions(file_actions) 00066 { 00067 } 00068 00069 file_actions_t & file_actions; 00070 00071 void operator()(ionotify::file_action fa, const std::string & /*path*/) 00072 { 00073 if(file_actions.empty() || file_actions.back() != fa) 00074 file_actions.push_back(fa); 00075 } 00076 }; 00077 } 00078 00079 BOOST_FIXTURE_TEST_CASE( test_ionotify1, Fixture ) 00080 { 00081 std::string const & fname = tdc.GetFilePath("testfile"); 00082 00083 std::ofstream out(fname.c_str()); 00084 00085 file_actions_t file_actions; 00086 functor f(file_actions); 00087 00088 file_actions_t expected; 00089 expected.push_back(ionotify::CHANGED); 00090 expected.push_back(ionotify::DELETED); 00091 00092 ionotify ion; 00093 ion.insert(fname, f); 00094 ion.start(); 00095 00096 out << "hello" << std::endl; 00097 boost::this_thread::sleep(boost::posix_time::seconds(1)); 00098 out << "world" << std::endl; 00099 boost::this_thread::sleep(boost::posix_time::seconds(1)); 00100 out << "goodnight" << std::endl; 00101 boost::this_thread::sleep(boost::posix_time::seconds(1)); 00102 out << "vienna" << std::endl; 00103 boost::this_thread::sleep(boost::posix_time::seconds(1)); 00104 00105 out.close(); 00106 00107 boost::filesystem::remove(fname); 00108 00109 // Give the IO time to react and trigger the events 00110 for(size_t x = 0 ; x < 5 && file_actions.size() < expected.size() ; ++x) 00111 { 00112 boost::this_thread::sleep(boost::posix_time::time_duration(0,0,1)); 00113 } 00114 00115 ion.stop(); 00116 00117 // The windows version uses file_watcher and the results are not 00118 // as accurate as inotify so there tests fail in windows :( 00119 #ifndef WIN32 00120 BOOST_ASSERT(expected == file_actions); 00121 #endif 00122 } 00123 00124 00125 BOOST_FIXTURE_TEST_CASE( test_ionotify2, Fixture ) 00126 { 00127 std::string const & fname = tdc.GetFilePath("testfile"); 00128 00129 std::ofstream out(fname.c_str()); 00130 00131 file_actions_t file_actions; 00132 functor f(file_actions); 00133 00134 file_actions_t expected; 00135 expected.push_back(ionotify::CHANGED); 00136 expected.push_back(ionotify::DELETED); 00137 00138 ionotify ion; 00139 ion.insert(fname, f); 00140 ion.start(); 00141 00142 out << "hello" << std::endl; 00143 out << "world" << std::endl; 00144 out << "goodnight" << std::endl; 00145 out << "vienna" << std::endl; 00146 00147 out.close(); 00148 00149 boost::filesystem::remove(fname); 00150 00151 // Give the IO time to react and trigger the events 00152 for(int x = 0 ; x < 5 && file_actions.size() < expected.size() ; ++x) 00153 { 00154 boost::this_thread::sleep(boost::posix_time::time_duration(0,0,1)); 00155 } 00156 00157 ion.stop(); 00158 00159 // The windows version uses file_watcher and the results are not 00160 // as accurate as inotify so there tests fail in windows :( 00161 #ifndef WIN32 00162 BOOST_ASSERT(expected == file_actions); 00163 #endif 00164 } 00165 00166 BOOST_AUTO_TEST_SUITE_END()