Example #1
0
        public void GetOrAdd_IgnoresCachedValue_IfGlobalFileWasChangedSinceCacheWasCreated(
            RazorFileInfo viewStartRazorFileInfo, IFileInfo globalFileInfo)
        {
            // Arrange
            var expectedType          = typeof(RuntimeCompileDifferent);
            var lastModified          = DateTime.UtcNow;
            var viewStartLastModified = DateTime.UtcNow;
            var content  = "some content";
            var fileInfo = new TestFileInfo
            {
                Length       = 1020,
                Content      = content,
                LastModified = lastModified,
                PhysicalPath = "Views\\home\\index.cshtml"
            };

            var fileProvider = new TestFileProvider();

            fileProvider.AddFile(fileInfo.PhysicalPath, fileInfo);
            fileProvider.AddFile(viewStartRazorFileInfo.RelativePath, globalFileInfo);
            var viewCollection = new ViewCollection();
            var cache          = new CompilerCache(new[] { viewCollection }, TestLoadContext, fileProvider);

            // Act
            var result = cache.GetOrAdd(fileInfo.PhysicalPath,
                                        compile: _ => CompilationResult.Successful(expectedType));

            // Assert
            Assert.NotSame(CompilerCacheResult.FileNotFound, result);
            var actual = result.CompilationResult;

            Assert.NotNull(actual);
            Assert.Equal(expectedType, actual.CompiledType);
        }
        public async Task <CompilationResult> Compile(IFileInfo fileInfo)
        {
            Directory.CreateDirectory(_tempDir);
            string        outFile = Path.Combine(_tempDir, Path.GetRandomFileName() + ".dll");
            StringBuilder args    = new StringBuilder("/target:library ");

            args.AppendFormat("/out:\"{0}\" ", outFile);
            foreach (var file in  Directory.EnumerateFiles(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "*.dll"))
            {
                args.AppendFormat("/R:\"{0}\" ", file);
            }
            args.AppendFormat("\"{0}\"", fileInfo.PhysicalPath);
            var outputStream = new MemoryStream();

            // common execute
            var process  = CreateProcess(args.ToString());
            int exitCode = await Start(process, outputStream);

            string output = GetString(outputStream);

            if (exitCode != 0)
            {
                IEnumerable <CompilationMessage> messages = output.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
                                                            .Skip(3)
                                                            .Select(e => new CompilationMessage(e));
                return(CompilationResult.Failed(String.Empty, messages));
            }

            var type = Assembly.LoadFrom(outFile)
                       .GetExportedTypes()
                       .First();

            return(CompilationResult.Successful(String.Empty, type));
        }
Example #3
0
        public void GetOrAdd_RecompilesFile_IfContentAndLengthAreChanged(
            Type resultViewType,
            long fileTimeUTC)
        {
            // Arrange
            var instance     = (View)Activator.CreateInstance(resultViewType);
            var length       = Encoding.UTF8.GetByteCount(instance.Content);
            var fileProvider = new TestFileProvider();
            var cache        = new CompilerCache(new[] { new ViewCollection() }, TestLoadContext, fileProvider);

            var fileInfo = new TestFileInfo
            {
                Length       = length,
                LastModified = DateTime.FromFileTimeUtc(fileTimeUTC),
                Content      = instance.Content
            };

            fileProvider.AddFile(ViewPath, fileInfo);

            // Act
            var result = cache.GetOrAdd(ViewPath,
                                        compile: _ => CompilationResult.Successful(resultViewType));

            // Assert
            Assert.NotSame(CompilerCacheResult.FileNotFound, result);
            var actual = result.CompilationResult;

            Assert.NotNull(actual);
            Assert.Equal(resultViewType, actual.CompiledType);
        }
Example #4
0
        public void GetOrAdd_IgnoresCachedValueIfFileIsIdentical_ButViewStartWasDeletedSinceCacheWasCreated()
        {
            // Arrange
            var expectedType = typeof(RuntimeCompileDifferent);
            var lastModified = DateTime.UtcNow;
            var fileSystem   = new TestFileSystem();

            var viewCollection  = new ViewCollection();
            var precompiledView = viewCollection.FileInfos[0];

            precompiledView.RelativePath = "Views\\Index.cshtml";
            var viewFileInfo = new TestFileInfo
            {
                Content      = new PreCompile().Content,
                LastModified = precompiledView.LastModified,
                PhysicalPath = precompiledView.RelativePath
            };

            fileSystem.AddFile(viewFileInfo.PhysicalPath, viewFileInfo);

            var viewStartFileInfo = new TestFileInfo
            {
                PhysicalPath = "Views\\_ViewStart.cshtml",
                Content      = "viewstart-content",
                LastModified = lastModified
            };
            var viewStart = new RazorFileInfo
            {
                FullTypeName = typeof(RuntimeCompileIdentical).FullName,
                RelativePath = viewStartFileInfo.PhysicalPath,
                LastModified = viewStartFileInfo.LastModified,
                Hash         = RazorFileHash.GetHash(viewStartFileInfo),
                Length       = viewStartFileInfo.Length
            };

            fileSystem.AddFile(viewStartFileInfo.PhysicalPath, viewStartFileInfo);

            viewCollection.Add(viewStart);
            var cache    = new CompilerCache(new[] { viewCollection }, fileSystem);
            var fileInfo = new RelativeFileInfo(viewFileInfo, viewFileInfo.PhysicalPath);

            // Act 1
            var actual1 = cache.GetOrAdd(fileInfo,
                                         compile: _ => { throw new Exception("should not be called"); });

            // Assert 1
            Assert.Equal(typeof(PreCompile), actual1.CompiledType);

            // Act 2
            fileSystem.DeleteFile(viewStartFileInfo.PhysicalPath);
            var actual2 = cache.GetOrAdd(fileInfo,
                                         compile: _ => CompilationResult.Successful(expectedType));

            // Assert 2
            Assert.Equal(expectedType, actual2.CompiledType);
        }
Example #5
0
        private CompilerCacheEntry GetOrAdd(RelativeFileInfo relativeFileInfo,
                                            Func <RelativeFileInfo, CompilationResult> compile,
                                            out CompilationResult result)
        {
            CompilerCacheEntry cacheEntry;
            var normalizedPath = NormalizePath(relativeFileInfo.RelativePath);

            if (!_cache.TryGetValue(normalizedPath, out cacheEntry))
            {
                return(OnCacheMiss(relativeFileInfo, normalizedPath, compile, out result));
            }
            else
            {
                var fileInfo = relativeFileInfo.FileInfo;
                if (cacheEntry.Length != fileInfo.Length)
                {
                    // Recompile if the file lengths differ
                    return(OnCacheMiss(relativeFileInfo, normalizedPath, compile, out result));
                }

                if (AssociatedViewStartsChanged(cacheEntry, compile))
                {
                    // Recompile if the view starts have changed since the entry was created.
                    return(OnCacheMiss(relativeFileInfo, normalizedPath, compile, out result));
                }

                if (cacheEntry.LastModified == fileInfo.LastModified)
                {
                    result = CompilationResult.Successful(cacheEntry.CompiledType);
                    return(cacheEntry);
                }

                // Timestamp doesn't match but it might be because of deployment, compare the hash.
                if (cacheEntry.IsPreCompiled &&
                    string.Equals(cacheEntry.Hash, RazorFileHash.GetHash(fileInfo), StringComparison.Ordinal))
                {
                    // Cache hit, but we need to update the entry.
                    // Assigning to LastModified is an atomic operation and will result in a safe race if it is
                    // being concurrently read and written or updated concurrently.
                    cacheEntry.LastModified = fileInfo.LastModified;
                    result = CompilationResult.Successful(cacheEntry.CompiledType);

                    return(cacheEntry);
                }

                // it's not a match, recompile
                return(OnCacheMiss(relativeFileInfo, normalizedPath, compile, out result));
            }
        }
Example #6
0
        public CompilationResult GetOrAdd(IFileInfo file, Func <CompilationResult> compile)
        {
            // Generate a content id
            var contentId = file.PhysicalPath + '|' + file.LastModified.Ticks;

            Type compiledType;

            if (!_cache.TryGetValue(contentId, out compiledType))
            {
                var result = compile();
                _cache.TryAdd(contentId, result.CompiledType);

                return(result);
            }

            return(CompilationResult.Successful(compiledType));
        }
Example #7
0
        public void GetOrAdd_IgnoresCachedValueIfFileIsIdentical_ButGlobalImportWasAdedSinceTheCacheWasCreated()
        {
            // Arrange
            var expectedType    = typeof(RuntimeCompileDifferent);
            var fileProvider    = new TestFileProvider();
            var collection      = new ViewCollection();
            var precompiledFile = collection.FileInfos[0];

            precompiledFile.RelativePath = "Views\\home\\index.cshtml";
            var cache    = new CompilerCache(new[] { collection }, TestLoadContext, fileProvider);
            var testFile = new TestFileInfo
            {
                Content      = new PreCompile().Content,
                LastModified = precompiledFile.LastModified,
                PhysicalPath = precompiledFile.RelativePath
            };

            fileProvider.AddFile(precompiledFile.RelativePath, testFile);
            var relativeFile = new RelativeFileInfo(testFile, testFile.PhysicalPath);

            // Act 1
            var result1 = cache.GetOrAdd(testFile.PhysicalPath,
                                         compile: _ => { throw new Exception("should not be called"); });

            // Assert 1
            Assert.NotSame(CompilerCacheResult.FileNotFound, result1);
            var actual1 = result1.CompilationResult;

            Assert.NotNull(actual1);
            Assert.Equal(typeof(PreCompile), actual1.CompiledType);

            // Act 2
            var globalTrigger = fileProvider.GetTrigger("Views\\_GlobalImport.cshtml");

            globalTrigger.IsExpired = true;
            var result2 = cache.GetOrAdd(testFile.PhysicalPath,
                                         compile: _ => CompilationResult.Successful(expectedType));

            // Assert 2
            Assert.NotSame(CompilerCacheResult.FileNotFound, result2);
            var actual2 = result2.CompilationResult;

            Assert.NotNull(actual2);
            Assert.Equal(expectedType, actual2.CompiledType);
        }
Example #8
0
        public async Task <CompilationResult> GetOrAdd(IFileInfo file, Func <Task <CompilationResult> > compile)
        {
            // Generate a content id
            string contentId = file.PhysicalPath + '|' + file.LastModified.Ticks;

            Type compiledType;

            if (!_cache.TryGetValue(contentId, out compiledType))
            {
                CompilationResult result = await compile();

                _cache.TryAdd(contentId, result.CompiledType);

                return(result);
            }

            return(CompilationResult.Successful(generatedCode: null, type: compiledType));
        }
Example #9
0
        /// <inheritdoc />
        public CompilationResult GetOrAdd([NotNull] RelativeFileInfo fileInfo,
                                          bool enableInstrumentation,
                                          [NotNull] Func <CompilationResult> compile)
        {
            CompilerCacheEntry cacheEntry;

            if (!_cache.TryGetValue(NormalizePath(fileInfo.RelativePath), out cacheEntry))
            {
                return(OnCacheMiss(fileInfo, enableInstrumentation, compile));
            }
            else
            {
                if ((cacheEntry.Length != fileInfo.FileInfo.Length) ||
                    (enableInstrumentation && !cacheEntry.IsInstrumented))
                {
                    // Recompile if
                    // (a) If the file lengths differ
                    // (b) If the compiled type is not instrumented but we require it to be instrumented.
                    return(OnCacheMiss(fileInfo, enableInstrumentation, compile));
                }

                if (cacheEntry.LastModified == fileInfo.FileInfo.LastModified)
                {
                    // Match, not update needed
                    return(CompilationResult.Successful(cacheEntry.CompiledType));
                }

                var hash = RazorFileHash.GetHash(fileInfo.FileInfo);

                // Timestamp doesn't match but it might be because of deployment, compare the hash.
                if (cacheEntry.IsPreCompiled &&
                    string.Equals(cacheEntry.Hash, hash, StringComparison.Ordinal))
                {
                    // Cache hit, but we need to update the entry
                    return(OnCacheMiss(fileInfo,
                                       enableInstrumentation,
                                       () => CompilationResult.Successful(cacheEntry.CompiledType)));
                }

                // it's not a match, recompile
                return(OnCacheMiss(fileInfo, enableInstrumentation, compile));
            }
        }
Example #10
0
        public void GetOrAdd_IgnoresCachedValue_IfViewStartWasChangedSinceCacheWasCreated(
            RazorFileInfo viewStartRazorFileInfo, TestFileInfo viewStartFileInfo)
        {
            // Arrange
            var expectedType          = typeof(RuntimeCompileDifferent);
            var lastModified          = DateTime.UtcNow;
            var viewStartLastModified = DateTime.UtcNow;
            var content  = "some content";
            var fileInfo = new TestFileInfo
            {
                Length       = 1020,
                Content      = content,
                LastModified = lastModified,
                PhysicalPath = "Views\\home\\index.cshtml"
            };

            var runtimeFileInfo = new RelativeFileInfo(fileInfo, fileInfo.PhysicalPath);

            var razorFileInfo = new RazorFileInfo
            {
                FullTypeName = typeof(PreCompile).FullName,
                Hash         = RazorFileHash.GetHash(fileInfo),
                LastModified = lastModified,
                Length       = Encoding.UTF8.GetByteCount(content),
                RelativePath = fileInfo.PhysicalPath,
            };

            var fileSystem = new TestFileSystem();

            fileSystem.AddFile(viewStartRazorFileInfo.RelativePath, viewStartFileInfo);
            var viewCollection = new ViewCollection();
            var cache          = new CompilerCache(new[] { viewCollection }, fileSystem);

            // Act
            var actual = cache.GetOrAdd(runtimeFileInfo,
                                        compile: _ => CompilationResult.Successful(expectedType));

            // Assert
            Assert.Equal(expectedType, actual.CompiledType);
        }
Example #11
0
        public void GetOrAdd_RecompilesFile_IfContentAndLengthAreChanged(
            Type resultViewType,
            long fileTimeUTC)
        {
            // Arrange
            var instance   = (View)Activator.CreateInstance(resultViewType);
            var length     = Encoding.UTF8.GetByteCount(instance.Content);
            var collection = new ViewCollection();
            var fileSystem = new TestFileSystem();
            var cache      = new CompilerCache(new[] { new ViewCollection() }, fileSystem);

            var fileInfo = new TestFileInfo
            {
                Length       = length,
                LastModified = DateTime.FromFileTimeUtc(fileTimeUTC),
                Content      = instance.Content
            };

            var runtimeFileInfo = new RelativeFileInfo(fileInfo, "ab");

            var precompiledContent = new PreCompile().Content;
            var razorFileInfo      = new RazorFileInfo
            {
                FullTypeName = typeof(PreCompile).FullName,
                Hash         = RazorFileHash.GetHash(GetMemoryStream(precompiledContent)),
                LastModified = DateTime.FromFileTimeUtc(10000),
                Length       = Encoding.UTF8.GetByteCount(precompiledContent),
                RelativePath = "ab",
            };

            // Act
            var actual = cache.GetOrAdd(runtimeFileInfo,
                                        compile: _ => CompilationResult.Successful(resultViewType));

            // Assert
            Assert.Equal(resultViewType, actual.CompiledType);
        }
Example #12
0
        public void GetOrAdd_IgnoresCachedValueIfFileIsIdentical_ButViewStartWasAdedSinceTheCacheWasCreated()
        {
            // Arrange
            var expectedType    = typeof(RuntimeCompileDifferent);
            var lastModified    = DateTime.UtcNow;
            var fileSystem      = new TestFileSystem();
            var collection      = new ViewCollection();
            var precompiledFile = collection.FileInfos[0];

            precompiledFile.RelativePath = "Views\\home\\index.cshtml";
            var cache    = new CompilerCache(new[] { collection }, fileSystem);
            var testFile = new TestFileInfo
            {
                Content      = new PreCompile().Content,
                LastModified = precompiledFile.LastModified,
                PhysicalPath = precompiledFile.RelativePath
            };

            fileSystem.AddFile(precompiledFile.RelativePath, testFile);
            var relativeFile = new RelativeFileInfo(testFile, testFile.PhysicalPath);

            // Act 1
            var actual1 = cache.GetOrAdd(relativeFile,
                                         compile: _ => { throw new Exception("should not be called"); });

            // Assert 1
            Assert.Equal(typeof(PreCompile), actual1.CompiledType);

            // Act 2
            fileSystem.AddFile("Views\\_ViewStart.cshtml", "");
            var actual2 = cache.GetOrAdd(relativeFile,
                                         compile: _ => CompilationResult.Successful(expectedType));

            // Assert 2
            Assert.Equal(expectedType, actual2.CompiledType);
        }
Example #13
0
        private GetOrAddResult GetOrAddCore(string relativePath,
                                            Func <RelativeFileInfo, CompilationResult> compile)
        {
            var normalizedPath = NormalizePath(relativePath);
            var cacheEntry     = _cache.Get <CompilerCacheEntry>(normalizedPath);

            if (cacheEntry == null)
            {
                var fileInfo = _fileProvider.GetFileInfo(relativePath);
                if (!fileInfo.Exists)
                {
                    return(null);
                }

                var relativeFileInfo = new RelativeFileInfo(fileInfo, relativePath);
                return(OnCacheMiss(relativeFileInfo, normalizedPath, compile));
            }
            else if (cacheEntry.IsPreCompiled && !cacheEntry.IsValidatedPreCompiled)
            {
                // For precompiled views, the first time the entry is read, we need to ensure that no changes were made
                // either to the file associated with this entry, or any _GlobalImport associated with it between the time
                // the View was precompiled and the time EnsureInitialized was called. For later iterations, we can
                // rely on expiration triggers ensuring the validity of the entry.

                var fileInfo = _fileProvider.GetFileInfo(relativePath);
                if (!fileInfo.Exists)
                {
                    return(null);
                }

                var relativeFileInfo = new RelativeFileInfo(fileInfo, relativePath);
                if (cacheEntry.Length != fileInfo.Length)
                {
                    // Recompile if the file lengths differ
                    return(OnCacheMiss(relativeFileInfo, normalizedPath, compile));
                }

                if (AssociatedGlobalFilesChanged(cacheEntry, compile))
                {
                    // Recompile if _GlobalImports have changed since the entry was created.
                    return(OnCacheMiss(relativeFileInfo, normalizedPath, compile));
                }

                if (cacheEntry.LastModified == fileInfo.LastModified)
                {
                    // Assigning to IsValidatedPreCompiled is an atomic operation and will result in a safe race
                    // if it is being concurrently updated and read.
                    cacheEntry.IsValidatedPreCompiled = true;
                    return(new GetOrAddResult
                    {
                        CompilationResult = CompilationResult.Successful(cacheEntry.CompiledType),
                        CompilerCacheEntry = cacheEntry
                    });
                }

                // Timestamp doesn't match but it might be because of deployment, compare the hash.
                if (cacheEntry.IsPreCompiled &&
                    string.Equals(cacheEntry.Hash,
                                  RazorFileHash.GetHash(fileInfo, cacheEntry.HashAlgorithmVersion),
                                  StringComparison.Ordinal))
                {
                    // Cache hit, but we need to update the entry.
                    // Assigning to LastModified and IsValidatedPreCompiled are atomic operations and will result in safe race
                    // if the entry is being concurrently read or updated.
                    cacheEntry.LastModified           = fileInfo.LastModified;
                    cacheEntry.IsValidatedPreCompiled = true;
                    return(new GetOrAddResult
                    {
                        CompilationResult = CompilationResult.Successful(cacheEntry.CompiledType),
                        CompilerCacheEntry = cacheEntry
                    });
                }

                // it's not a match, recompile
                return(OnCacheMiss(relativeFileInfo, normalizedPath, compile));
            }

            return(new GetOrAddResult
            {
                CompilationResult = CompilationResult.Successful(cacheEntry.CompiledType),
                CompilerCacheEntry = cacheEntry
            });
        }
Example #14
0
        public void GetOrAdd_IgnoresCachedValueIfFileIsIdentical_ButGlobalWasDeletedSinceCacheWasCreated()
        {
            // Arrange
            var expectedType = typeof(RuntimeCompileDifferent);
            var lastModified = DateTime.UtcNow;
            var fileProvider = new TestFileProvider();

            var viewCollection  = new ViewCollection();
            var precompiledView = viewCollection.FileInfos[0];

            precompiledView.RelativePath = "Views\\Index.cshtml";
            var viewFileInfo = new TestFileInfo
            {
                Content      = new PreCompile().Content,
                LastModified = precompiledView.LastModified,
                PhysicalPath = precompiledView.RelativePath
            };

            fileProvider.AddFile(viewFileInfo.PhysicalPath, viewFileInfo);

            var globalFileInfo = new TestFileInfo
            {
                PhysicalPath = "Views\\_GlobalImport.cshtml",
                Content      = "viewstart-content",
                LastModified = lastModified
            };
            var globalFile = new RazorFileInfo
            {
                FullTypeName         = typeof(RuntimeCompileIdentical).FullName,
                RelativePath         = globalFileInfo.PhysicalPath,
                LastModified         = globalFileInfo.LastModified,
                Hash                 = RazorFileHash.GetHash(globalFileInfo, hashAlgorithmVersion: 1),
                HashAlgorithmVersion = 1,
                Length               = globalFileInfo.Length
            };

            fileProvider.AddFile(globalFileInfo.PhysicalPath, globalFileInfo);

            viewCollection.Add(globalFile);
            var cache = new CompilerCache(new[] { viewCollection }, TestLoadContext, fileProvider);

            // Act 1
            var result1 = cache.GetOrAdd(viewFileInfo.PhysicalPath,
                                         compile: _ => { throw new Exception("should not be called"); });

            // Assert 1
            Assert.NotSame(CompilerCacheResult.FileNotFound, result1);
            var actual1 = result1.CompilationResult;

            Assert.NotNull(actual1);
            Assert.Equal(typeof(PreCompile), actual1.CompiledType);

            // Act 2
            var trigger = fileProvider.GetTrigger(globalFileInfo.PhysicalPath);

            trigger.IsExpired = true;
            var result2 = cache.GetOrAdd(viewFileInfo.PhysicalPath,
                                         compile: _ => CompilationResult.Successful(expectedType));

            // Assert 2
            Assert.NotSame(CompilerCacheResult.FileNotFound, result2);
            var actual2 = result2.CompilationResult;

            Assert.NotNull(actual2);
            Assert.Equal(expectedType, actual2.CompiledType);
        }