6
#include <gmock/gmock.h>
8
#include <boost/filesystem.hpp>
12
#include "NuxCore/AsyncFileWriter.h"
16
namespace bf = boost::filesystem;
17
using namespace testing;
18
using namespace nux::testing;
22
const std::string TEST_ROOT("/tmp/nux-test-cases");
25
class TestAsyncfileWriter : public ::testing::Test
28
virtual void SetUp() {
29
// Make sure that the tests start with and empty TEST_ROOT.
30
bf::remove_all(TEST_ROOT);
31
bf::create_directories(TEST_ROOT);
34
virtual void TearDown() {
35
// Delete the unity test directory
36
bf::remove_all(TEST_ROOT);
39
bool WaitForOpen(nux::AsyncFileWriter& writer, unsigned timeout = 5) {
41
TestCallback timed_out;
42
g_timeout_add_seconds(timeout, &TestCallback::glib_callback, &timed_out);
43
writer.opened.connect(opened.sigc_callback());
45
while (!opened.happened && !timed_out.happened) {
46
PumpGObjectMainLoop();
48
return opened.happened;
51
bool WaitForClose(nux::AsyncFileWriter& writer, unsigned timeout = 5) {
53
TestCallback timed_out;
54
g_timeout_add_seconds(timeout, &TestCallback::glib_callback, &timed_out);
55
writer.closed.connect(closed.sigc_callback());
57
while (!closed.happened && !timed_out.happened) {
58
PumpGObjectMainLoop();
60
return closed.happened;
65
TEST_F(TestAsyncfileWriter, TestConstructor) {
66
std::string filename(TEST_ROOT + "/empty-file");
68
nux::AsyncFileWriter writer(filename);
69
bool opened = WaitForOpen(writer);
72
EXPECT_TRUE(bf::exists(filename));
73
EXPECT_THAT(ReadFile(filename), Eq(""));
76
TEST_F(TestAsyncfileWriter, TestWrites) {
77
std::string filename(TEST_ROOT + "/write-file");
78
std::string data(200, 'x');
80
nux::AsyncFileWriter writer(filename);
83
bool closed = WaitForClose(writer);
86
EXPECT_THAT(ReadFile(filename), Eq(data));
89
TEST_F(TestAsyncfileWriter, TestWriteLots) {
90
std::string filename(TEST_ROOT + "/lots-file");
91
std::string data(200, 'x');
92
const int loop_count = 1000;
94
nux::AsyncFileWriter writer(filename);
95
for (int i = 0; i < loop_count; ++i) {
99
bool closed = WaitForClose(writer);
102
std::string file_content = ReadFile(filename);
103
EXPECT_THAT(file_content.size(), Eq(data.size() * loop_count));
105
EXPECT_THAT(file_content, MatchesRegex("^x+$"));