summaryrefslogtreecommitdiffstats
path: root/memfilepersistence/src/tests/spi/simulatedfailurefile.cpp
blob: b7da294f8eb9a5742ead08cceb030ef30df4d065 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simulatedfailurefile.h"
#include <vespa/vespalib/util/exceptions.h>

namespace storage {
namespace memfile {

vespalib::LazyFile::UP
SimulatedFailureLazyFile::Factory::createFile(const std::string& fileName) const {
    return vespalib::LazyFile::UP(
            new SimulatedFailureLazyFile(fileName,
                    vespalib::File::DIRECTIO,
                    _readOpsBeforeFailure,
                    _writeOpsBeforeFailure));
}

SimulatedFailureLazyFile::SimulatedFailureLazyFile(
        const std::string& filename,
        int flags,
        int readOpsBeforeFailure,
        int writeOpsBeforeFailure)
    : LazyFile(filename, flags),
      _readOpsBeforeFailure(readOpsBeforeFailure),
      _writeOpsBeforeFailure(writeOpsBeforeFailure)
{
}

off_t
SimulatedFailureLazyFile::write(const void *buf, size_t bufsize, off_t offset)
{
    if (_writeOpsBeforeFailure == 0) {
        throw vespalib::IoException(
                "A simulated I/O write exception was triggered",
                vespalib::IoException::CORRUPT_DATA, VESPA_STRLOC);
    }
    --_writeOpsBeforeFailure;
    return vespalib::LazyFile::write(buf, bufsize, offset);
}

size_t
SimulatedFailureLazyFile::read(void *buf, size_t bufsize, off_t offset) const
{
    if (_readOpsBeforeFailure == 0) {
        throw vespalib::IoException(
                "A simulated I/O read exception was triggered",
                vespalib::IoException::CORRUPT_DATA, VESPA_STRLOC);
    }
    --_readOpsBeforeFailure;
    return vespalib::LazyFile::read(buf, bufsize, offset);
}

} // ns memfile
} // ns storage