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
|
/*
* (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 <algorithm>
#include <cmath>
#include <cstring>
#include <thread>
#include "eckit/config/Resource.h"
#include "eckit/exception/Exceptions.h"
#include "eckit/io/Buffer.h"
#include "eckit/io/DataHandle.h"
#include "eckit/io/FileHandle.h"
#include "eckit/io/FilePool.h"
#include "eckit/testing/Test.h"
using namespace std;
using namespace eckit;
using namespace eckit::testing;
namespace eckit::test {
//----------------------------------------------------------------------------------------------------------------------
static const size_t BUF_SIZE = 1024;
static const char* files[] = {"foo.data", "bar.data", "baz.data", "marco.data", "polo.data"};
//----------------------------------------------------------------------------------------------------------------------
CASE("test_eckit_io_filepool_threads") {
FilePool pool(1);
size_t nThreads = Resource<size_t>("$ECKIT_TEST_THREADS", 16);
nThreads = std::min(size_t(255), nThreads);
std::vector<std::thread> threads;
for (size_t i = 0; i < nThreads; ++i) {
threads.emplace_back(std::thread([&pool, i]() {
std::vector<unsigned char> buffer(BUF_SIZE, i + 1);
DataHandle* foo = pool.checkout("foo.data");
foo->write(&buffer[0], buffer.size());
pool.checkin(foo);
}));
}
for (auto& t : threads) {
t.join();
}
// verfy the data
DataHandle* foo = pool.checkout("foo.data");
EXPECT(foo->openForRead() >= Length(nThreads * BUF_SIZE));
// Check we have nThreads blocks of BUF_SIZE with Bytes 1 to nThreads
std::vector<bool> found(nThreads);
for (size_t i = 0; i < nThreads; ++i) {
std::vector<unsigned char> buffer(BUF_SIZE);
foo->read(&buffer[0], BUF_SIZE);
const unsigned char c = buffer[0];
EXPECT(c > 0);
EXPECT(!found[c - 1]);
std::vector<unsigned char> expect(BUF_SIZE, c);
EXPECT(buffer == expect);
found[c - 1] = true;
}
for (size_t i = 0; i < nThreads; ++i) {
EXPECT(found[i]);
}
pool.checkin(foo);
EXPECT(pool.usage() == 0);
EXPECT(pool.size() == 1);
}
CASE("test_eckit_io_filepool_0") {
Buffer buffer(BUF_SIZE);
::memset(buffer, 0, buffer.size());
FilePool pool(1);
// pool sized 1
EXPECT(pool.capacity() == 1);
EXPECT(pool.size() == 0);
pool.capacity(3);
// pool is larger
EXPECT(pool.capacity() == 3);
EXPECT(pool.size() == 0);
DataHandle* foo = pool.checkout("foo.data");
foo->write(buffer, buffer.size());
// foo is in use
EXPECT(pool.usage() == 1);
EXPECT(pool.size() == 0);
DataHandle* bar = pool.checkout("bar.data");
bar->write(buffer, buffer.size());
// foo + bar are in use
EXPECT(pool.usage() == 2);
EXPECT(pool.size() == 0);
Log::info() << pool << std::endl;
pool.checkin(foo);
pool.checkin(bar);
Log::info() << pool << std::endl;
// foo + bar are in pool, none in use
EXPECT(pool.usage() == 0);
EXPECT(pool.size() == 2);
DataHandle* foo2 = pool.checkout("foo.data");
foo2->write(buffer, buffer.size());
// foo in use again, bar in pool
EXPECT(pool.usage() == 1);
EXPECT(pool.size() == 1);
pool.checkin(foo);
// foo + bar are again in pool, none in use
EXPECT(pool.usage() == 0);
EXPECT(pool.size() == 2);
DataHandle* baz = pool.checkout("baz.data");
baz->write(buffer, buffer.size());
// foo + bar in pool, baz in use
EXPECT(pool.usage() == 1);
EXPECT(pool.size() == 2);
pool.checkin(baz);
// baz + foo + bar in pool, none in use
EXPECT(pool.usage() == 0);
EXPECT(pool.size() == 3);
DataHandle* marco = pool.checkout("marco.data");
marco->write(buffer, buffer.size());
// baz + foo + bar in pool, marco in use
EXPECT(pool.usage() == 1);
EXPECT(pool.size() == 3);
pool.checkin(marco);
// marco + foo + baz in pool, none in use, bar was purged (closed)
EXPECT(pool.usage() == 0);
EXPECT(pool.size() == 3);
Log::info() << pool << std::endl;
pool.capacity(1);
// marco in pool, none in use, foo + baz were purged (closed) due to reduced capacity
EXPECT(pool.usage() == 0);
EXPECT(pool.size() == 1);
Log::info() << pool << std::endl;
DataHandle* polo = pool.checkout("polo.data");
polo->write(buffer, buffer.size());
// marco in pool, polo is in use
EXPECT(pool.usage() == 1);
EXPECT(pool.size() == 1);
Log::info() << pool << std::endl;
pool.checkin(polo);
// polo pushes marco out and remains alone in pool
EXPECT(pool.usage() == 0);
EXPECT(pool.size() == 1);
Log::info() << pool << std::endl;
// remove polo and marco (latter has no effect but is legal)
EXPECT(pool.remove("polo.data"));
EXPECT(!pool.remove("marco.data"));
EXPECT(pool.usage() == 0);
EXPECT(pool.size() == 0);
// checking in a never checked out DataHandle throws
std::unique_ptr<FileHandle> fhp(new FileHandle("foo.data"));
EXPECT_THROWS_AS(pool.checkin(fhp.get()), eckit::SeriousBug);
}
//----------------------------------------------------------------------------------------------------------------------
} // namespace eckit::test
int main(int argc, char** argv) {
int failures = run_tests(argc, argv);
for (size_t i = 0; i < 5; ++i) {
PathName path(eckit::test::files[i]);
if (path.exists()) {
path.unlink();
}
}
return failures;
}
|