Skip to content

Commit 9de5a74

Browse files
[SYCL] Add checks on file-system operations in persistent cache unit-test (#6064)
This commit adds checks to file-system operations in the persistent device code cache unit-test cases that did not have them. Signed-off-by: Larsen, Steffen <steffen.larsen@intel.com>
1 parent 3f89f27 commit 9de5a74

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

sycl/unittests/kernel-and-program/PersistentDeviceCodeCache.cpp

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
#include <llvm/Support/FileSystem.h>
1919
#include <vector>
2020

21+
#define ASSERT_NO_ERROR(x) \
22+
if (std::error_code EC = x) { \
23+
FAIL() << #x ": did not return errc::success.\n" \
24+
<< "error number: " << EC.value() << "\n" \
25+
<< "error message: " << EC.message() << "\n"; \
26+
}
27+
2128
// TODO: Introduce common unit tests header and move it there
2229
static void set_env(const char *name, const char *value) {
2330
#ifdef _WIN32
@@ -121,7 +128,7 @@ class PersistenDeviceCodeCache : public ::testing::Test {
121128
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath(
122129
Dev, Img, {'S', 'p', 'e', 'c', 'C', 'o', 'n', 's', 't', ProgramID},
123130
BuildOptions);
124-
llvm::sys::fs::remove_directories(ItemDir);
131+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
125132

126133
Barrier b(ThreadCount);
127134
{
@@ -147,7 +154,7 @@ class PersistenDeviceCodeCache : public ::testing::Test {
147154

148155
ThreadPool MPool(ThreadCount, testLambda);
149156
}
150-
llvm::sys::fs::remove_directories(ItemDir);
157+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
151158
}
152159

153160
protected:
@@ -188,7 +195,7 @@ TEST_F(PersistenDeviceCodeCache, KeysWithNullTermSymbol) {
188195
std::vector<unsigned char> SpecConst(Key.begin(), Key.end());
189196
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath(
190197
Dev, Img, SpecConst, Key);
191-
llvm::sys::fs::remove_directories(ItemDir);
198+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
192199

193200
detail::PersistentDeviceCodeCache::putItemToDisc(Dev, Img, SpecConst, Key,
194201
NativeProg);
@@ -204,7 +211,7 @@ TEST_F(PersistenDeviceCodeCache, KeysWithNullTermSymbol) {
204211
}
205212
}
206213

207-
llvm::sys::fs::remove_directories(ItemDir);
214+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
208215
}
209216

210217
/* Do read/write for the same cache item to/from 300 threads for small device
@@ -246,7 +253,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
246253
std::string BuildOptions{"--corrupted-file"};
247254
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath(
248255
Dev, Img, {}, BuildOptions);
249-
llvm::sys::fs::remove_directories(ItemDir);
256+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
250257

251258
// Only source file is present
252259
detail::PersistentDeviceCodeCache::putItemToDisc(Dev, Img, {}, BuildOptions,
@@ -257,7 +264,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
257264
BuildOptions);
258265
EXPECT_EQ(Res.size(), static_cast<size_t>(0))
259266
<< "Item with missed binary file was read";
260-
llvm::sys::fs::remove_directories(ItemDir);
267+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
261268

262269
// Only binary file is present
263270
detail::PersistentDeviceCodeCache::putItemToDisc(Dev, Img, {}, BuildOptions,
@@ -268,7 +275,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
268275
BuildOptions);
269276
EXPECT_EQ(Res.size(), static_cast<size_t>(0))
270277
<< "Item with missed source file was read";
271-
llvm::sys::fs::remove_directories(ItemDir);
278+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
272279

273280
// Binary file is corrupted
274281
detail::PersistentDeviceCodeCache::putItemToDisc(Dev, Img, {}, BuildOptions,
@@ -286,7 +293,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
286293
EXPECT_EQ(Res.size(), static_cast<size_t>(0))
287294
<< "Item with corrupted binary file was read";
288295

289-
llvm::sys::fs::remove_directories(ItemDir);
296+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
290297

291298
// Source file is empty
292299
detail::PersistentDeviceCodeCache::putItemToDisc(Dev, Img, {}, BuildOptions,
@@ -299,7 +306,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
299306
BuildOptions);
300307
EXPECT_EQ(Res.size(), static_cast<size_t>(0))
301308
<< "Item with corrupted binary file was read";
302-
llvm::sys::fs::remove_directories(ItemDir);
309+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
303310
}
304311

305312
/* Checks that lock file affects cache operations as expected:
@@ -317,7 +324,7 @@ TEST_F(PersistenDeviceCodeCache, LockFile) {
317324
std::string BuildOptions{"--obsolete-lock"};
318325
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath(
319326
Dev, Img, {}, BuildOptions);
320-
llvm::sys::fs::remove_directories(ItemDir);
327+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
321328

322329
// Create 1st cahe item
323330
detail::PersistentDeviceCodeCache::putItemToDisc(Dev, Img, {}, BuildOptions,
@@ -356,7 +363,7 @@ TEST_F(PersistenDeviceCodeCache, LockFile) {
356363
<< "Corrupted image loaded from persistent cache";
357364
}
358365
}
359-
llvm::sys::fs::remove_directories(ItemDir);
366+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
360367
}
361368

362369
#ifndef _WIN32
@@ -374,26 +381,30 @@ TEST_F(PersistenDeviceCodeCache, AccessDeniedForCacheDir) {
374381
std::string BuildOptions{"--build-options"};
375382
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath(
376383
Dev, Img, {}, BuildOptions);
377-
llvm::sys::fs::remove_directories(ItemDir);
384+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
378385
detail::PersistentDeviceCodeCache::putItemToDisc(Dev, Img, {}, BuildOptions,
379386
NativeProg);
380387
EXPECT_TRUE(llvm::sys::fs::exists(ItemDir + "/0.bin")) << "No file created";
381-
llvm::sys::fs::setPermissions(ItemDir + "/0.bin", llvm::sys::fs::no_perms);
388+
ASSERT_NO_ERROR(llvm::sys::fs::setPermissions(ItemDir + "/0.bin",
389+
llvm::sys::fs::no_perms));
382390
// No access to binary file new cache item to be created
383391
detail::PersistentDeviceCodeCache::putItemToDisc(Dev, Img, {}, BuildOptions,
384392
NativeProg);
385393
EXPECT_TRUE(llvm::sys::fs::exists(ItemDir + "/1.bin")) << "No file created";
386394

387-
llvm::sys::fs::setPermissions(ItemDir + "/1.bin", llvm::sys::fs::no_perms);
395+
ASSERT_NO_ERROR(llvm::sys::fs::setPermissions(ItemDir + "/1.bin",
396+
llvm::sys::fs::no_perms));
388397
auto Res = detail::PersistentDeviceCodeCache::getItemFromDisc(Dev, Img, {},
389398
BuildOptions);
390399

391400
// No image to be read due to lack of permissions from source file
392401
EXPECT_EQ(Res.size(), static_cast<size_t>(0))
393402
<< "Read from the file without permissions.";
394403

395-
llvm::sys::fs::setPermissions(ItemDir + "/0.bin", llvm::sys::fs::all_perms);
396-
llvm::sys::fs::setPermissions(ItemDir + "/1.bin", llvm::sys::fs::all_perms);
404+
ASSERT_NO_ERROR(llvm::sys::fs::setPermissions(ItemDir + "/0.bin",
405+
llvm::sys::fs::all_perms));
406+
ASSERT_NO_ERROR(llvm::sys::fs::setPermissions(ItemDir + "/1.bin",
407+
llvm::sys::fs::all_perms));
397408

398409
Res = detail::PersistentDeviceCodeCache::getItemFromDisc(Dev, Img, {},
399410
BuildOptions);
@@ -404,7 +415,7 @@ TEST_F(PersistenDeviceCodeCache, AccessDeniedForCacheDir) {
404415
<< "Corrupted image loaded from persistent cache";
405416
}
406417
}
407-
llvm::sys::fs::remove_directories(ItemDir);
418+
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
408419
}
409420
#endif //_WIN32
410421
} // namespace

0 commit comments

Comments
 (0)