public void GetOrAdd_IgnoresCache_IfCachedItemIsNotInstrumentedAndEnableInstrumentationIsTrue() { // Arrange var lastModified = DateTime.UtcNow; var cache = new CompilerCache(); var fileInfo = new Mock <IFileInfo>(); fileInfo.SetupGet(f => f.PhysicalPath) .Returns("test"); fileInfo.SetupGet(f => f.LastModified) .Returns(lastModified); var type = GetType(); var uncachedResult1 = UncachedCompilationResult.Successful(type, "hello world"); var uncachedResult2 = UncachedCompilationResult.Successful(typeof(object), "hello world"); var uncachedResult3 = UncachedCompilationResult.Successful(typeof(Guid), "hello world"); var runtimeFileInfo = new RelativeFileInfo() { FileInfo = fileInfo.Object, RelativePath = "test", }; // Act cache.GetOrAdd(runtimeFileInfo, false, () => uncachedResult1); var actual1 = cache.GetOrAdd(runtimeFileInfo, true, () => uncachedResult2); var actual2 = cache.GetOrAdd(runtimeFileInfo, false, () => uncachedResult3); // Assert Assert.Same(uncachedResult2, actual1); Assert.Same(typeof(object), actual1.CompiledType); Assert.NotSame(actual2, uncachedResult3); Assert.Same(typeof(object), actual2.CompiledType); }
public void GetOrAdd_DoesNotCacheCompiledContent_OnCallsAfterInitial() { // Arrange var lastModified = DateTime.UtcNow; var cache = new CompilerCache(); var fileInfo = new Mock <IFileInfo>(); fileInfo.SetupGet(f => f.PhysicalPath) .Returns("test"); fileInfo.SetupGet(f => f.LastModified) .Returns(lastModified); var type = GetType(); var uncachedResult = UncachedCompilationResult.Successful(type, "hello world"); // Act cache.GetOrAdd(fileInfo.Object, () => uncachedResult); var actual1 = cache.GetOrAdd(fileInfo.Object, () => uncachedResult); var actual2 = cache.GetOrAdd(fileInfo.Object, () => uncachedResult); // Assert Assert.NotSame(uncachedResult, actual1); Assert.NotSame(uncachedResult, actual2); var result = Assert.IsType <CompilationResult>(actual1); Assert.Null(actual1.CompiledContent); Assert.Same(type, actual1.CompiledType); result = Assert.IsType <CompilationResult>(actual2); Assert.Null(actual2.CompiledContent); Assert.Same(type, actual2.CompiledType); }
public void GetOrAdd_ReturnsCompilationResultFromFactory() { // Arrange var cache = new CompilerCache(); var fileInfo = new Mock <IFileInfo>(); fileInfo .SetupGet(i => i.LastModified) .Returns(DateTime.FromFileTimeUtc(10000)); var type = GetType(); var expected = UncachedCompilationResult.Successful(type, "hello world"); var runtimeFileInfo = new RelativeFileInfo() { FileInfo = fileInfo.Object, RelativePath = "ab", }; // Act var actual = cache.GetOrAdd(runtimeFileInfo, false, () => expected); // Assert Assert.Same(expected, actual); Assert.Equal("hello world", actual.CompiledContent); Assert.Same(type, actual.CompiledType); }
public void GetOrAdd_ReturnsCompilationResultFromFactory() { // Arrange var cache = new CompilerCache(); var fileInfo = Mock.Of <IFileInfo>(); var type = GetType(); var expected = UncachedCompilationResult.Successful(type, "hello world"); // Act var actual = cache.GetOrAdd(fileInfo, () => expected); // Assert Assert.Same(expected, actual); Assert.Equal("hello world", actual.CompiledContent); Assert.Same(type, actual.CompiledType); }
public void FileWithTheSameLengthAndDifferentTime_DoesNot_OverridesPrecompilation( Type resultViewType, long fileTimeUTC, bool swapsPreCompile) { // Arrange var instance = (View)Activator.CreateInstance(resultViewType); var length = Encoding.UTF8.GetByteCount(instance.Content); var collection = new ViewCollection(); var cache = new CompilerCache(new[] { new ViewCollection() }); var fileInfo = new Mock <IFileInfo>(); fileInfo .SetupGet(i => i.Length) .Returns(length); fileInfo .SetupGet(i => i.LastModified) .Returns(DateTime.FromFileTimeUtc(fileTimeUTC)); fileInfo.Setup(i => i.CreateReadStream()) .Returns(GetMemoryStream(instance.Content)); var preCompileType = typeof(PreCompile); var runtimeFileInfo = new RelativeFileInfo() { FileInfo = fileInfo.Object, RelativePath = "ab", }; // Act var actual = cache.GetOrAdd(runtimeFileInfo, enableInstrumentation: false, compile: () => CompilationResult.Successful(resultViewType)); // Assert if (swapsPreCompile) { Assert.Equal(actual.CompiledType, resultViewType); } else { Assert.Equal(actual.CompiledType, typeof(PreCompile)); } }