nux-1.14.0
|
00001 #include <string> 00002 #include <fstream> 00003 00004 #include <iostream> 00005 00006 #include <gmock/gmock.h> 00007 00008 #include <boost/filesystem.hpp> 00009 00010 #include <glib.h> 00011 00012 #include "NuxCore/AsyncFileWriter.h" 00013 00014 #include "Helpers.h" 00015 00016 namespace bf = boost::filesystem; 00017 using namespace testing; 00018 using namespace nux::testing; 00019 00020 namespace { 00021 00022 const std::string TEST_ROOT("/tmp/nux-test-cases"); 00023 00024 00025 class TestAsyncfileWriter : public ::testing::Test 00026 { 00027 protected: 00028 virtual void SetUp() { 00029 // Make sure that the tests start with and empty TEST_ROOT. 00030 bf::remove_all(TEST_ROOT); 00031 bf::create_directories(TEST_ROOT); 00032 } 00033 00034 virtual void TearDown() { 00035 // Delete the unity test directory 00036 bf::remove_all(TEST_ROOT); 00037 } 00038 00039 bool WaitForOpen(nux::AsyncFileWriter& writer, unsigned timeout = 5) { 00040 TestCallback opened; 00041 TestCallback timed_out; 00042 g_timeout_add_seconds(timeout, &TestCallback::glib_callback, &timed_out); 00043 writer.opened.connect(opened.sigc_callback()); 00044 00045 while (!opened.happened && !timed_out.happened) { 00046 PumpGObjectMainLoop(); 00047 } 00048 return opened.happened; 00049 } 00050 00051 bool WaitForClose(nux::AsyncFileWriter& writer, unsigned timeout = 5) { 00052 TestCallback closed; 00053 TestCallback timed_out; 00054 g_timeout_add_seconds(timeout, &TestCallback::glib_callback, &timed_out); 00055 writer.closed.connect(closed.sigc_callback()); 00056 00057 while (!closed.happened && !timed_out.happened) { 00058 PumpGObjectMainLoop(); 00059 } 00060 return closed.happened; 00061 } 00062 00063 }; 00064 00065 TEST_F(TestAsyncfileWriter, TestConstructor) { 00066 std::string filename(TEST_ROOT + "/empty-file"); 00067 { 00068 nux::AsyncFileWriter writer(filename); 00069 bool opened = WaitForOpen(writer); 00070 EXPECT_TRUE(opened); 00071 } 00072 EXPECT_TRUE(bf::exists(filename)); 00073 EXPECT_THAT(ReadFile(filename), Eq("")); 00074 } 00075 00076 TEST_F(TestAsyncfileWriter, TestWrites) { 00077 std::string filename(TEST_ROOT + "/write-file"); 00078 std::string data(200, 'x'); 00079 { 00080 nux::AsyncFileWriter writer(filename); 00081 writer.Write(data); 00082 writer.Close(); 00083 bool closed = WaitForClose(writer); 00084 EXPECT_TRUE(closed); 00085 } 00086 EXPECT_THAT(ReadFile(filename), Eq(data)); 00087 } 00088 00089 TEST_F(TestAsyncfileWriter, TestWriteLots) { 00090 std::string filename(TEST_ROOT + "/lots-file"); 00091 std::string data(200, 'x'); 00092 const int loop_count = 1000; 00093 { 00094 nux::AsyncFileWriter writer(filename); 00095 for (int i = 0; i < loop_count; ++i) { 00096 writer.Write(data); 00097 } 00098 writer.Close(); 00099 bool closed = WaitForClose(writer); 00100 EXPECT_TRUE(closed); 00101 } 00102 std::string file_content = ReadFile(filename); 00103 EXPECT_THAT(file_content.size(), Eq(data.size() * loop_count)); 00104 // They are all x's. 00105 EXPECT_THAT(file_content, MatchesRegex("^x+$")); 00106 } 00107 00108 00109 00110 } // anon namespace