File: FileExistenceCacheTest.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (97 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Linq;
using System.Threading;
using System.Web.Hosting;
using Moq;
using Xunit;

namespace System.Web.WebPages.Test
{
    public class FileExistenceCacheTest
    {
        [Fact]
        public void ConstructorTest()
        {
            var ms = 1000;
            var cache = new FileExistenceCache(null);
            Assert.Null(cache.VirtualPathProvider);

            var vpp = new Mock<VirtualPathProvider>().Object;
            cache = new FileExistenceCache(vpp);
            Assert.Equal(vpp, cache.VirtualPathProvider);
            Assert.Equal(ms, cache.MilliSecondsBeforeReset);

            ms = 9999;
            cache = new FileExistenceCache(vpp, ms);
            Assert.Equal(vpp, cache.VirtualPathProvider);
            Assert.Equal(ms, cache.MilliSecondsBeforeReset);
        }

        [Fact]
        public void TimeExceededFalseTest()
        {
            var ms = 100000;
            var cache = new FileExistenceCache(GetVpp(), ms);
            Assert.False(cache.TimeExceeded);
        }

        [Fact]
        public void TimeExceededTrueTest()
        {
            var ms = 5;
            var cache = new FileExistenceCache(GetVpp(), ms);
            Thread.Sleep(300);
            Assert.True(cache.TimeExceeded);
        }

        [Fact]
        public void ResetTest()
        {
            var cache = new FileExistenceCache(GetVpp());
            var cacheInternal = cache.CacheInternal;
            cache.Reset();
            Assert.NotSame(cacheInternal, cache.CacheInternal);
        }

        [Fact]
        public void FileExistsTest()
        {
            var path = "~/index.cshtml";
            var cache = new FileExistenceCache(GetVpp(path));
            Assert.True(cache.FileExists(path));
            Assert.False(cache.FileExists("~/test.cshtml"));
        }

        [Fact]
        public void FileExistsVppLaterTest()
        {
            var path = "~/index.cshtml";
            var cache = new FileExistenceCache(GetVpp(path));
            Assert.True(cache.FileExists(path));
            Assert.False(cache.FileExists("~/test.cshtml"));
        }

        [Fact]
        public void FileExistsTimeExceededTest()
        {
            var path = "~/index.cshtml";
            Utils.SetupVirtualPathInAppDomain(path, "");

            var cache = new FileExistenceCache(GetVpp(path));
            var cacheInternal = cache.CacheInternal;
            cache.MilliSecondsBeforeReset = 5;
            Thread.Sleep(300);
            Assert.True(cache.FileExists(path));
            Assert.False(cache.FileExists("~/test.cshtml"));
            Assert.NotEqual(cacheInternal, cache.CacheInternal);
        }

        private static VirtualPathProvider GetVpp(params string[] files)
        {
            var vpp = new Mock<VirtualPathProvider>();
            vpp.Setup(c => c.FileExists(It.IsAny<string>())).Returns<string>(p => files.Contains(p, StringComparer.OrdinalIgnoreCase));
            return vpp.Object;
        }
    }
}