summaryrefslogtreecommitdiffstats
path: root/memfilepersistence/src/tests/spi/buffered_file_writer_test.cpp
blob: 36270335fda56652b05d198092bbfe74979ff7b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/memfilepersistence/mapper/bufferedfilewriter.h>
#include <vespa/memfilepersistence/mapper/buffer.h>
#include <vespa/vespalib/io/fileutil.h>

namespace storage {
namespace memfile {

class BufferedFileWriterTest : public CppUnit::TestFixture
{
public:
    void noImplicitFlushingWhenDestructing();

    CPPUNIT_TEST_SUITE(BufferedFileWriterTest);
    CPPUNIT_TEST(noImplicitFlushingWhenDestructing);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(BufferedFileWriterTest);

namespace {

// Partial mock of vespalib::File. Unfortunately, there's currently no
// base interface to implement so have to override a class that already has
// implementation code present.
class MockFile : public vespalib::File
{
public:
    bool _didWrite;

    MockFile(const std::string& filename)
        : File(filename),
          _didWrite(false)
    {
    }

    void open(int flags, bool autoCreateDirectories) override {
        (void) flags;
        (void) autoCreateDirectories;
        // Don't do anything here to prevent us from actually opening a file
        // on disk.
    }

    off_t write(const void *buf, size_t bufsize, off_t offset) override {
        (void) buf;
        (void) bufsize;
        (void) offset;
        _didWrite = true;
        return 0;
    }
};

}

void
BufferedFileWriterTest::noImplicitFlushingWhenDestructing()
{
    MockFile file("foo");
    {
        Buffer buffer(1024);
        BufferedFileWriter writer(file, buffer, buffer.getSize());
        // Do a buffered write. This fits well within the buffer and should
        // consequently not be immediately written out to the backing file.
        writer.write("blarg", 5);
        // Escape scope without having flushed anything.
    }
    // Since BufferedFileWriter is meant to be used with O_DIRECT files,
    // flushing just implies writing rather than syncing (this is a half truth
    // since you still sync directories etc to ensure metadata is written, but
    // this constrained assumption works fine in the context of this test).
    CPPUNIT_ASSERT(!file._didWrite);
}

} // memfile
} // storage