2
0

AggregateCacheDependencyTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // System.Web.Hosting.HostingEnvironmentTest
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2007 Gert Driesen
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. using System.Text;
  32. using System.Threading;
  33. using System.Web;
  34. using System.Web.Caching;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Web.Caching
  37. {
  38. [TestFixture]
  39. public class AggregateCacheDependencyTest
  40. {
  41. private string _tempFolder;
  42. [SetUp]
  43. public void SetUp ()
  44. {
  45. _tempFolder = Path.Combine (Path.GetTempPath (),
  46. "MonoTests.System.Web.Caching.AggregateCacheDependencyTest");
  47. if (Directory.Exists (_tempFolder))
  48. Directory.Delete (_tempFolder, true);
  49. Directory.CreateDirectory (_tempFolder);
  50. }
  51. [TearDown]
  52. public void TearDown ()
  53. {
  54. if (Directory.Exists (_tempFolder))
  55. Directory.Delete (_tempFolder, true);
  56. }
  57. [Test] // bug #82419
  58. [Ignore ("The test is not written in reproducible way and hence keeps build looks as if it regressed incorrectly")]
  59. public void SingleDependency ()
  60. {
  61. string depFile = Path.Combine (_tempFolder, "dep.tmp");
  62. AggregateCacheDependency aggregate = new AggregateCacheDependency ();
  63. aggregate.Add (new CacheDependency (depFile));
  64. DateTime absoluteExpiration = DateTime.Now.AddSeconds (4);
  65. TimeSpan slidingExpiration = TimeSpan.Zero;
  66. CacheItemPriority priority = CacheItemPriority.Default;
  67. string original = "MONO";
  68. HttpRuntime.Cache.Insert ("key", original, aggregate,
  69. absoluteExpiration, slidingExpiration, priority,
  70. null);
  71. string cachedValue = HttpRuntime.Cache.Get ("key") as string;
  72. Assert.IsNotNull (cachedValue, "#A1");
  73. Assert.AreEqual ("MONO", cachedValue, "#A2");
  74. Assert.IsFalse (aggregate.HasChanged, "#A3");
  75. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  76. Assert.IsNotNull (cachedValue, "#B1");
  77. Assert.AreEqual ("MONO", cachedValue, "#B2");
  78. Assert.IsFalse (aggregate.HasChanged, "#B3");
  79. File.WriteAllText (depFile, "OK", Encoding.UTF8);
  80. Thread.Sleep (500);
  81. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  82. Assert.IsNull (cachedValue, "#C1");
  83. Assert.IsTrue (aggregate.HasChanged, "#C2");
  84. }
  85. [Test]
  86. [Ignore ("This test is racy, it fails from time to time.")]
  87. public void AbsoluteExpiration ()
  88. {
  89. string depFile = Path.Combine (_tempFolder, "dep.tmp");
  90. AggregateCacheDependency aggregate = new AggregateCacheDependency ();
  91. aggregate.Add (new CacheDependency (depFile));
  92. DateTime absoluteExpiration = DateTime.Now.AddMilliseconds (100);
  93. TimeSpan slidingExpiration = TimeSpan.Zero;
  94. CacheItemPriority priority = CacheItemPriority.Default;
  95. string original = "MONO";
  96. HttpRuntime.Cache.Insert ("key", original, aggregate,
  97. absoluteExpiration, slidingExpiration, priority,
  98. null);
  99. string cachedValue = HttpRuntime.Cache.Get ("key") as string;
  100. Assert.IsNotNull (cachedValue, "#A1");
  101. Assert.AreEqual ("MONO", cachedValue, "#A2");
  102. Assert.IsFalse (aggregate.HasChanged, "#A3");
  103. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  104. Assert.IsNotNull (cachedValue, "#B1");
  105. Assert.AreEqual ("MONO", cachedValue, "#B2");
  106. Assert.IsFalse (aggregate.HasChanged, "#B3");
  107. Thread.Sleep (500);
  108. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  109. Assert.IsNull (cachedValue, "#C1");
  110. Assert.IsFalse (aggregate.HasChanged, "#C2");
  111. }
  112. [Test]
  113. [Category ("NotWorking")]
  114. public void SlidingExpiration ()
  115. {
  116. string depFile = Path.Combine (_tempFolder, "dep.tmp");
  117. AggregateCacheDependency aggregate = new AggregateCacheDependency ();
  118. aggregate.Add (new CacheDependency (depFile));
  119. DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
  120. TimeSpan slidingExpiration = new TimeSpan (0, 0, 0, 0, 100);
  121. CacheItemPriority priority = CacheItemPriority.Default;
  122. string original = "MONO";
  123. HttpRuntime.Cache.Insert ("key", original, aggregate,
  124. absoluteExpiration, slidingExpiration, priority,
  125. null);
  126. string cachedValue = HttpRuntime.Cache.Get ("key") as string;
  127. Assert.IsNotNull (cachedValue, "#A1");
  128. Assert.AreEqual ("MONO", cachedValue, "#A2");
  129. Assert.IsFalse (aggregate.HasChanged, "#A3");
  130. Thread.Sleep (50);
  131. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  132. Assert.IsNotNull (cachedValue, "#B1");
  133. Assert.AreEqual ("MONO", cachedValue, "#B2");
  134. Assert.IsFalse (aggregate.HasChanged, "#B3");
  135. Thread.Sleep (120);
  136. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  137. Assert.IsNull (cachedValue, "#C1");
  138. Assert.IsFalse (aggregate.HasChanged, "#C2");
  139. }
  140. }
  141. }