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 <vector> 00032 #include <fstream> 00033 00034 #include <boost/thread/thread.hpp> 00035 #include <boost/thread/xtime.hpp> 00036 #include <boost/bind.hpp> 00037 00038 #include "../../include/moost/io/file_watcher.hpp" 00039 #include "../../include/moost/thread/xtime_util.hpp" 00040 00041 using namespace moost::io; 00042 using namespace moost::thread; 00043 00044 BOOST_AUTO_TEST_SUITE( file_watcher_test ) 00045 00046 struct Fixture 00047 { 00048 file_watcher fw; 00049 std::string test_path; 00050 std::ofstream out; 00051 00052 int Creates; 00053 int Changes; 00054 int Deletes; 00055 00056 void file_changed(file_watcher::file_action action, const std::string & /*path*/) 00057 { 00058 switch (action) 00059 { 00060 case file_watcher::CREATED: ++Creates; break; 00061 case file_watcher::CHANGED: ++Changes; break; 00062 case file_watcher::DELETED: ++Deletes; break; 00063 } 00064 } 00065 00066 Fixture() 00067 : test_path("file_watcher_Test_File"), 00068 Creates(0), 00069 Changes(0), 00070 Deletes(0) 00071 { 00072 fw.insert(test_path, boost::bind(&Fixture::file_changed, this, _1, _2)); 00073 00074 // start up the file_watcher 00075 fw.start(); 00076 } 00077 ~Fixture() 00078 { 00079 // stop the file_watcher 00080 fw.stop(); 00081 00082 if (out.is_open()) 00083 out.close(); 00084 boost::filesystem::remove(boost::filesystem::path(test_path)); 00085 } 00086 00087 void sleep(int seconds) 00088 { 00089 boost::thread::sleep(xtime_util::add_sec(xtime_util::now(), seconds)); 00090 } 00091 }; 00092 00093 // what happens when we do nothing? 00094 BOOST_FIXTURE_TEST_CASE( test_nothing, Fixture ) 00095 { 00096 sleep(1); 00097 BOOST_REQUIRE_EQUAL(Creates, 0); 00098 BOOST_REQUIRE_EQUAL(Changes, 0); 00099 BOOST_REQUIRE_EQUAL(Deletes, 0); 00100 } 00101 00102 // what happens when we create a file? 00103 BOOST_FIXTURE_TEST_CASE( test_create, Fixture ) 00104 { 00105 out.open(test_path.c_str()); 00106 sleep(1); 00107 00108 BOOST_REQUIRE_EQUAL(Creates, 1); 00109 BOOST_REQUIRE_EQUAL(Changes, 0); 00110 BOOST_REQUIRE_EQUAL(Deletes, 0); 00111 } 00112 00113 BOOST_FIXTURE_TEST_CASE( test_change, Fixture ) 00114 { 00115 out.open(test_path.c_str()); 00116 sleep(2); 00117 00118 out << "wasssaaaaapp!!!" << std::flush; 00119 00120 sleep(2); 00121 00122 BOOST_REQUIRE_EQUAL(Creates, 1); 00123 BOOST_REQUIRE_EQUAL(Changes, 1); 00124 BOOST_REQUIRE_EQUAL(Deletes, 0); 00125 } 00126 00127 BOOST_FIXTURE_TEST_CASE( test_delete, Fixture ) 00128 { 00129 out.open(test_path.c_str()); 00130 sleep(1); 00131 00132 out.close(); 00133 boost::filesystem::remove(boost::filesystem::path(test_path)); 00134 00135 sleep(1); 00136 00137 BOOST_REQUIRE_EQUAL(Creates, 1); 00138 BOOST_REQUIRE_EQUAL(Changes, 0); 00139 BOOST_REQUIRE_EQUAL(Deletes, 1); 00140 } 00141 00142 BOOST_AUTO_TEST_SUITE_END()