File: test_pooledhandle.cc

package info (click to toggle)
eckit 2.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 11,044 kB
  • sloc: cpp: 111,103; ansic: 2,826; yacc: 590; lex: 361; python: 302; sh: 162; makefile: 53
file content (260 lines) | stat: -rw-r--r-- 6,584 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <cstring>

#include "eckit/config/Resource.h"
#include "eckit/filesystem/PathName.h"
#include "eckit/io/AutoCloser.h"
#include "eckit/io/Buffer.h"
#include "eckit/io/FileHandle.h"
#include "eckit/io/MemoryHandle.h"
#include "eckit/io/MultiHandle.h"
#include "eckit/io/PartFileHandle.h"
#include "eckit/io/PooledHandle.h"
#include "eckit/log/Log.h"
#include "eckit/runtime/Tool.h"
#include "eckit/testing/Test.h"
#include "eckit/types/Types.h"
#include "eckit/utils/StringTools.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

class Tester {
public:

    Tester() {

        std::string base = Resource<std::string>("$TMPDIR", "/tmp");
        path1_           = PathName::unique(base + "/path1");
        path1_ += ".dat";

        path2_ = PathName::unique(base + "/path2");
        path2_ += ".dat";

        FileHandle f1(path1_);
        f1.openForWrite(0);
        f1.write(message, ::strlen(message));
        f1.close();

        string upper = StringTools::upper(message);

        FileHandle f2(path2_);
        f2.openForWrite(0);
        f2.write(upper.c_str(), upper.size());
        f2.close();
    }

    ~Tester() {
        bool verbose = false;
        path1_.unlink(verbose);
        path2_.unlink(verbose);
    }

    const char* message = "abcdefghijklmnopqrstuvwxyz";

    PathName path1_;
    PathName path2_;
};

CASE("From one PooledHandle") {

    Tester test;

    SECTION("Read from begin") {

        Buffer buffer(64);
        buffer.zero();

        PooledHandle f1(test.path1_);
        PooledHandle f2(test.path1_);

        EXPECT_NO_THROW(f1.openForRead());
        EXPECT_NO_THROW(f2.openForRead());

        EXPECT(f1.read(buffer, 1) == 1);
        EXPECT(buffer[0] == 'a');

        EXPECT(f2.read(buffer, 1) == 1);
        EXPECT(buffer[0] == 'a');

        EXPECT(f1.read(buffer, 13) == 13);
        EXPECT(std::string(buffer) == std::string("bcdefghijklmn"));

        EXPECT(f2.read(buffer, 25) == 25);
        EXPECT(std::string(buffer) == std::string("bcdefghijklmnopqrstuvwxyz"));

        EXPECT_NO_THROW(f1.close());
        EXPECT_NO_THROW(f2.close());

        EXPECT(f1.nbOpens() == 1);
        EXPECT(f1.nbReads() == 4);
        EXPECT(f1.nbSeeks() == 0);
    }

    SECTION("Read interlaced") {

        const size_t N     = 4;
        const size_t M     = ::strlen(test.message);
        const size_t bsize = N * M;

        std::vector<char> result(bsize, '\0');
        std::vector<char> buffer(bsize, '\0');

        std::vector<unique_ptr<PooledHandle>> files;
        for (size_t i = 0; i < N; ++i) {
            files.emplace_back(new PooledHandle(test.path1_));
        }

        for (auto& file : files) {
            EXPECT_NO_THROW(file->openForRead());
        }

        // read interlaced from N pooled files
        [[maybe_unused]] size_t p = 0;
        for (size_t i = 0; i < M; ++i) {
            for (size_t j = 0; j < N; ++j) {
                char b[2];
                long len = files[j]->read(b, 1);
                EXPECT(len == 1);
                p++;

                buffer[i * N + j] = b[0];
                result[i * N + j] = test.message[i];
            }
        }

        std::cout << std::string(result.data(), result.size()) << std::endl;
        std::cout << std::string(buffer.data(), buffer.size()) << std::endl;

        EXPECT(result == buffer);

        EXPECT(files[0]->nbOpens() == 1);
        EXPECT(files[0]->nbReads() == N * M);

        for (auto& file : files) {
            EXPECT_NO_THROW(file->close());
        }
    }
}

CASE("Error handling") {

    SECTION("Failed to open") {
        const char* randomName = "loiuytgbhytresdfgtr";
        PooledHandle f(randomName);
        EXPECT_THROWS_AS(f.openForRead(), CantOpenFile);
    }

    SECTION("Failed to close is not an error") {
        Tester test;
        PooledHandle* f = new PooledHandle(test.path1_);
        EXPECT_NO_THROW(f->openForRead());
        EXPECT_NO_THROW(delete f);
    }
}

CASE("Seeking to location") {

    Tester test;

    SECTION("1") {
        Buffer b(32);
        b.zero();

        PooledHandle f(test.path1_);
        auto c = closer(f);

        EXPECT_NO_THROW(f.openForRead());


        EXPECT(f.seek(10) == Offset(10));
        EXPECT(f.read(b, 1) == 1);

        EXPECT(b[0] == 'k');

        EXPECT(f.seek(25) == Offset(25));
        EXPECT(f.read(b, 1) == 1);

        EXPECT(b[0] == 'z');

        EXPECT(f.seek(5) == Offset(5));
        EXPECT(f.read(b, 1) == 1);

        EXPECT(b[0] == 'f');

        EXPECT(f.seek(0) == Offset(0));
        EXPECT(f.read(b, 1) == 1);

        EXPECT(b[0] == 'a');

        EXPECT(f.seek(60) == Offset(60));
        EXPECT(f.read(b, 1) == 0);  // read past end of file returns 0

        EXPECT(f.seek(20) == Offset(20));
        EXPECT(f.read(b, 10) == 6);  // read until end of file
    }

    SECTION("2") {
        Buffer b(32);
        b.zero();

        PooledHandle f1(test.path1_);
        auto c1 = closer(f1);

        PooledHandle f2(test.path2_);
        auto c2 = closer(f2);

        PooledHandle f3(test.path1_);
        auto c3 = closer(f3);

        PooledHandle f4(test.path2_);
        auto c4 = closer(f4);

        EXPECT_NO_THROW(f1.openForRead());
        EXPECT_NO_THROW(f2.openForRead());
        EXPECT_NO_THROW(f3.openForRead());
        EXPECT_NO_THROW(f4.openForRead());

        EXPECT(f1.seek(10) == Offset(10));
        EXPECT(f1.read(b, 1) == 1);

        EXPECT(b[0] == 'k');

        EXPECT(f2.seek(25) == Offset(25));
        EXPECT(f2.read(b, 1) == 1);

        EXPECT(b[0] == 'Z');

        EXPECT(f3.seek(5) == Offset(5));
        EXPECT(f3.read(b, 1) == 1);

        EXPECT(b[0] == 'f');

        EXPECT(f4.seek(0) == Offset(0));
        EXPECT(f4.read(b, 1) == 1);

        EXPECT(b[0] == 'A');
    }
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

int main(int argc, char** argv) {
    return run_tests(argc, argv);
}