1616#include < gtest/gtest.h>
1717#include < helpers/PiMock.hpp>
1818#include < llvm/Support/FileSystem.h>
19+ #include < optional>
1920#include < vector>
2021
22+ #define ASSERT_NO_ERROR (x ) \
23+ if (std::error_code EC = x) { \
24+ FAIL () << #x " : did not return errc::success.\n " \
25+ << " error number: " << EC.value () << " \n " \
26+ << " error message: " << EC.message () << " \n " ; \
27+ }
28+
2129// TODO: Introduce common unit tests header and move it there
2230static void set_env (const char *name, const char *value) {
2331#ifdef _WIN32
24- (void )_putenv_s (name, value);
32+ (void )_putenv_s (name, value ? value : " " );
2533#else
26- (void )setenv (name, value, /* overwrite*/ 1 );
34+ if (value)
35+ (void )setenv (name, value, /* overwrite*/ 1 );
36+ else
37+ (void )unsetenv (name);
2738#endif
2839}
2940
@@ -81,12 +92,47 @@ class PersistenDeviceCodeCache : public ::testing::Test {
8192 return _putenv_s (name, value);
8293 }
8394#endif
95+
96+ std::optional<std::string> SYCLCachePersistentBefore;
97+ bool SYCLCachePersistentChanged = false ;
98+
99+ // Caches the initial value of the SYCL_CACHE_PERSISTENT environment variable
100+ // before overwriting it with the new value.
101+ // Tear-down will reset the environment variable.
102+ void SetSYCLCachePersistentEnv (const char *NewValue) {
103+ char *SYCLCachePersistent = getenv (" SYCL_CACHE_PERSISTENT" );
104+ // We can skip if the new value is the same as the old one.
105+ if ((!NewValue && !SYCLCachePersistent) ||
106+ (NewValue && SYCLCachePersistent &&
107+ !strcmp (NewValue, SYCLCachePersistent)))
108+ return ;
109+
110+ // Cache the old value of SYCL_CACHE_PERSISTENT if it is not already saved.
111+ if (!SYCLCachePersistentChanged && SYCLCachePersistent)
112+ SYCLCachePersistentBefore = std::string{SYCLCachePersistent};
113+
114+ // Set the environment variable and signal the configuration file and the
115+ // persistent cache.
116+ set_env (" SYCL_CACHE_PERSISTENT" , NewValue);
117+ sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
118+ detail::PersistentDeviceCodeCache::reparseConfig ();
119+ SYCLCachePersistentChanged = true ;
120+ }
121+
84122 virtual void SetUp () {
85123 EXPECT_NE (getenv (" SYCL_CACHE_DIR" ), nullptr )
86124 << " Please set SYCL_CACHE_DIR environment variable pointing to cache "
87125 " location." ;
88126 }
89127
128+ virtual void TearDown () {
129+ // If we changed the cache, set it back to the old value.
130+ if (SYCLCachePersistentChanged)
131+ SetSYCLCachePersistentEnv (SYCLCachePersistentBefore
132+ ? SYCLCachePersistentBefore->c_str ()
133+ : nullptr );
134+ }
135+
90136 PersistenDeviceCodeCache () : Plt{default_selector ()} {
91137
92138 if (Plt.is_host () || Plt.get_backend () != backend::opencl) {
@@ -112,16 +158,15 @@ class PersistenDeviceCodeCache : public ::testing::Test {
112158 return ;
113159 }
114160
115- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
116- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
161+ SetSYCLCachePersistentEnv (" 1" );
117162
118163 std::string BuildOptions{" --concurrent-access=" +
119164 std::to_string (ThreadCount)};
120165 DeviceCodeID = ProgramID;
121166 std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
122167 Dev, Img, {' S' , ' p' , ' e' , ' c' , ' C' , ' o' , ' n' , ' s' , ' t' , ProgramID},
123168 BuildOptions);
124- llvm::sys::fs::remove_directories (ItemDir);
169+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
125170
126171 Barrier b (ThreadCount);
127172 {
@@ -147,7 +192,7 @@ class PersistenDeviceCodeCache : public ::testing::Test {
147192
148193 ThreadPool MPool (ThreadCount, testLambda);
149194 }
150- llvm::sys::fs::remove_directories (ItemDir);
195+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
151196 }
152197
153198protected:
@@ -181,14 +226,13 @@ TEST_F(PersistenDeviceCodeCache, KeysWithNullTermSymbol) {
181226 return ;
182227 }
183228
184- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
185- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
229+ SetSYCLCachePersistentEnv (" 1" );
186230
187231 std::string Key{' 1' , ' \0 ' , ' 3' , ' 4' , ' \0 ' };
188232 std::vector<unsigned char > SpecConst (Key.begin (), Key.end ());
189233 std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
190234 Dev, Img, SpecConst, Key);
191- llvm::sys::fs::remove_directories (ItemDir);
235+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
192236
193237 detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, SpecConst, Key,
194238 NativeProg);
@@ -204,7 +248,7 @@ TEST_F(PersistenDeviceCodeCache, KeysWithNullTermSymbol) {
204248 }
205249 }
206250
207- llvm::sys::fs::remove_directories (ItemDir);
251+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
208252}
209253
210254/* Do read/write for the same cache item to/from 300 threads for small device
@@ -240,13 +284,12 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
240284 return ;
241285 }
242286
243- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
244- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
287+ SetSYCLCachePersistentEnv (" 1" );
245288
246289 std::string BuildOptions{" --corrupted-file" };
247290 std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
248291 Dev, Img, {}, BuildOptions);
249- llvm::sys::fs::remove_directories (ItemDir);
292+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
250293
251294 // Only source file is present
252295 detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -257,7 +300,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
257300 BuildOptions);
258301 EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
259302 << " Item with missed binary file was read" ;
260- llvm::sys::fs::remove_directories (ItemDir);
303+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
261304
262305 // Only binary file is present
263306 detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -268,7 +311,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
268311 BuildOptions);
269312 EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
270313 << " Item with missed source file was read" ;
271- llvm::sys::fs::remove_directories (ItemDir);
314+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
272315
273316 // Binary file is corrupted
274317 detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -286,7 +329,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
286329 EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
287330 << " Item with corrupted binary file was read" ;
288331
289- llvm::sys::fs::remove_directories (ItemDir);
332+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
290333
291334 // Source file is empty
292335 detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -299,7 +342,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
299342 BuildOptions);
300343 EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
301344 << " Item with corrupted binary file was read" ;
302- llvm::sys::fs::remove_directories (ItemDir);
345+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
303346}
304347
305348/* Checks that lock file affects cache operations as expected:
@@ -311,13 +354,12 @@ TEST_F(PersistenDeviceCodeCache, LockFile) {
311354 return ;
312355 }
313356
314- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
315- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
357+ SetSYCLCachePersistentEnv (" 1" );
316358
317359 std::string BuildOptions{" --obsolete-lock" };
318360 std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
319361 Dev, Img, {}, BuildOptions);
320- llvm::sys::fs::remove_directories (ItemDir);
362+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
321363
322364 // Create 1st cahe item
323365 detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -356,7 +398,7 @@ TEST_F(PersistenDeviceCodeCache, LockFile) {
356398 << " Corrupted image loaded from persistent cache" ;
357399 }
358400 }
359- llvm::sys::fs::remove_directories (ItemDir);
401+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
360402}
361403
362404#ifndef _WIN32
@@ -368,32 +410,35 @@ TEST_F(PersistenDeviceCodeCache, AccessDeniedForCacheDir) {
368410 return ;
369411 }
370412
371- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
372- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
413+ SetSYCLCachePersistentEnv (" 1" );
373414
374415 std::string BuildOptions{" --build-options" };
375416 std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
376417 Dev, Img, {}, BuildOptions);
377- llvm::sys::fs::remove_directories (ItemDir);
418+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
378419 detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
379420 NativeProg);
380421 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);
422+ ASSERT_NO_ERROR (llvm::sys::fs::setPermissions (ItemDir + " /0.bin" ,
423+ llvm::sys::fs::no_perms));
382424 // No access to binary file new cache item to be created
383425 detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
384426 NativeProg);
385427 EXPECT_TRUE (llvm::sys::fs::exists (ItemDir + " /1.bin" )) << " No file created" ;
386428
387- llvm::sys::fs::setPermissions (ItemDir + " /1.bin" , llvm::sys::fs::no_perms);
429+ ASSERT_NO_ERROR (llvm::sys::fs::setPermissions (ItemDir + " /1.bin" ,
430+ llvm::sys::fs::no_perms));
388431 auto Res = detail::PersistentDeviceCodeCache::getItemFromDisc (Dev, Img, {},
389432 BuildOptions);
390433
391434 // No image to be read due to lack of permissions from source file
392435 EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
393436 << " Read from the file without permissions." ;
394437
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);
438+ ASSERT_NO_ERROR (llvm::sys::fs::setPermissions (ItemDir + " /0.bin" ,
439+ llvm::sys::fs::all_perms));
440+ ASSERT_NO_ERROR (llvm::sys::fs::setPermissions (ItemDir + " /1.bin" ,
441+ llvm::sys::fs::all_perms));
397442
398443 Res = detail::PersistentDeviceCodeCache::getItemFromDisc (Dev, Img, {},
399444 BuildOptions);
@@ -404,7 +449,7 @@ TEST_F(PersistenDeviceCodeCache, AccessDeniedForCacheDir) {
404449 << " Corrupted image loaded from persistent cache" ;
405450 }
406451 }
407- llvm::sys::fs::remove_directories (ItemDir);
452+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
408453}
409454#endif // _WIN32
410455} // namespace
0 commit comments