/// <summary> /// Returns a cache initializer to a real instance of the cache /// </summary> protected CacheInitializer GetRealCacheInitializerForTests() { var tempDir = OperatingSystemHelper.IsUnixOS ? "/tmp/buildxl-temp" : TemporaryDirectory; string cacheDirectory = Path.Combine(tempDir, "cache"); AbsolutePath cacheConfigPath = WriteTestCacheConfigToDisk(cacheDirectory); var translator = new RootTranslator(); if (TryGetSubstSourceAndTarget(out var substSource, out var substTarget)) { translator.AddTranslation(substTarget, substSource); } translator.Seal(); Configuration.Cache.CacheConfigFile = cacheConfigPath; Configuration.Cache.CacheLogFilePath = AbsolutePath.Create(Context.PathTable, tempDir).Combine(Context.PathTable, "cache.log"); var maybeCacheInitializer = CacheInitializer.GetCacheInitializationTask( LoggingContext, Context.PathTable, cacheDirectory, Configuration.Cache, translator, recoveryStatus: false, cancellationToken: CancellationToken.None).GetAwaiter().GetResult(); if (!maybeCacheInitializer.Succeeded) { throw new BuildXLException("Unable to initialize the real cache: " + maybeCacheInitializer.Failure.DescribeIncludingInnerFailures()); } return(maybeCacheInitializer.Result); }
public void TestProperLogMessageOnCacheLockAcquisitionFailure() { var tempDir = Path.Combine( OperatingSystemHelper.IsUnixOS ? "/tmp/bxl-temp" : TemporaryDirectory, Guid.NewGuid().ToString()); string cacheDirectory = Path.Combine(tempDir, "cache"); string cacheConfigJson = $@"{{ ""MaxCacheSizeInMB"": 1024, ""CacheId"": ""TestCache"", ""Assembly"": ""BuildXL.Cache.MemoizationStoreAdapter"", ""CacheLogPath"": ""[BuildXLSelectedLogPath]"", ""Type"": ""BuildXL.Cache.MemoizationStoreAdapter.MemoizationStoreCacheFactory"", ""CacheRootPath"": ""{cacheDirectory.Replace("\\", "\\\\")}"", ""UseStreamCAS"": false, ""SingleInstanceTimeoutInSeconds"" : 5 }}"; AbsolutePath cacheConfigPath = WriteTestCacheConfigToDisk(cacheDirectory, cacheConfigJson); var translator = new RootTranslator(); translator.Seal(); var possibleFirstCacheInitializer = CacheInitializer.GetCacheInitializationTask( LoggingContext, Context.PathTable, cacheDirectory, new CacheConfiguration { CacheLogFilePath = AbsolutePath.Create(Context.PathTable, tempDir).Combine(Context.PathTable, "cache.log"), CacheConfigFile = cacheConfigPath }, translator, recoveryStatus: false, cancellationToken: CancellationToken.None).GetAwaiter().GetResult(); if (!possibleFirstCacheInitializer.Succeeded) { AssertTrue(false, "Failed to initialize the cache: " + possibleFirstCacheInitializer.Failure.DescribeIncludingInnerFailures()); } var possibleSecondCacheInitializer = CacheInitializer.GetCacheInitializationTask( LoggingContext, Context.PathTable, cacheDirectory, new CacheConfiguration { // need a different name for the log file (due to the order in which the things are initialized) CacheLogFilePath = AbsolutePath.Create(Context.PathTable, tempDir).Combine(Context.PathTable, "cache_2.log"), CacheConfigFile = cacheConfigPath, }, translator, recoveryStatus: false, cancellationToken: CancellationToken.None).GetAwaiter().GetResult(); // close and dispose the first cache (must be done before the assert block bellow) var firstCacheInitializer = possibleFirstCacheInitializer.Result; AssertSuccess(firstCacheInitializer.Close()); firstCacheInitializer.Dispose(); AssertErrorEventLogged(global::BuildXL.Engine.Tracing.LogEventId.FailedToAcquireDirectoryLock); AssertTrue(!possibleSecondCacheInitializer.Succeeded, "Initialization of the second cache should have failed."); }