AggregateCacheDependencyTest.cs 5.4 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. #if NET_2_0
  30. using System;
  31. using System.IO;
  32. using System.Text;
  33. using System.Threading;
  34. using System.Web;
  35. using System.Web.Caching;
  36. using NUnit.Framework;
  37. namespace MonoTests.System.Web.Caching
  38. {
  39. [TestFixture]
  40. public class AggregateCacheDependencyTest
  41. {
  42. private string _tempFolder;
  43. [SetUp]
  44. public void SetUp ()
  45. {
  46. _tempFolder = Path.Combine (Path.GetTempPath (),
  47. "MonoTests.System.Web.Caching.AggregateCacheDependencyTest");
  48. if (Directory.Exists (_tempFolder))
  49. Directory.Delete (_tempFolder, true);
  50. Directory.CreateDirectory (_tempFolder);
  51. }
  52. [TearDown]
  53. public void TearDown ()
  54. {
  55. if (Directory.Exists (_tempFolder))
  56. Directory.Delete (_tempFolder, true);
  57. }
  58. [Test] // bug #82419
  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 (100);
  81. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  82. Assert.IsNull (cachedValue, "#C1");
  83. Assert.IsTrue (aggregate.HasChanged, "#C2");
  84. }
  85. [Test]
  86. public void AbsoluteExpiration ()
  87. {
  88. string depFile = Path.Combine (_tempFolder, "dep.tmp");
  89. AggregateCacheDependency aggregate = new AggregateCacheDependency ();
  90. aggregate.Add (new CacheDependency (depFile));
  91. DateTime absoluteExpiration = DateTime.Now.AddMilliseconds (100);
  92. TimeSpan slidingExpiration = TimeSpan.Zero;
  93. CacheItemPriority priority = CacheItemPriority.Default;
  94. string original = "MONO";
  95. HttpRuntime.Cache.Insert ("key", original, aggregate,
  96. absoluteExpiration, slidingExpiration, priority,
  97. null);
  98. string cachedValue = HttpRuntime.Cache.Get ("key") as string;
  99. Assert.IsNotNull (cachedValue, "#A1");
  100. Assert.AreEqual ("MONO", cachedValue, "#A2");
  101. Assert.IsFalse (aggregate.HasChanged, "#A3");
  102. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  103. Assert.IsNotNull (cachedValue, "#B1");
  104. Assert.AreEqual ("MONO", cachedValue, "#B2");
  105. Assert.IsFalse (aggregate.HasChanged, "#B3");
  106. Thread.Sleep (100);
  107. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  108. Assert.IsNull (cachedValue, "#C1");
  109. Assert.IsFalse (aggregate.HasChanged, "#C2");
  110. }
  111. [Test]
  112. [Category ("NotWorking")]
  113. public void SlidingExpiration ()
  114. {
  115. string depFile = Path.Combine (_tempFolder, "dep.tmp");
  116. AggregateCacheDependency aggregate = new AggregateCacheDependency ();
  117. aggregate.Add (new CacheDependency (depFile));
  118. DateTime absoluteExpiration = Cache.NoAbsoluteExpiration;
  119. TimeSpan slidingExpiration = new TimeSpan (0, 0, 0, 0, 100);
  120. CacheItemPriority priority = CacheItemPriority.Default;
  121. string original = "MONO";
  122. HttpRuntime.Cache.Insert ("key", original, aggregate,
  123. absoluteExpiration, slidingExpiration, priority,
  124. null);
  125. string cachedValue = HttpRuntime.Cache.Get ("key") as string;
  126. Assert.IsNotNull (cachedValue, "#A1");
  127. Assert.AreEqual ("MONO", cachedValue, "#A2");
  128. Assert.IsFalse (aggregate.HasChanged, "#A3");
  129. Thread.Sleep (50);
  130. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  131. Assert.IsNotNull (cachedValue, "#B1");
  132. Assert.AreEqual ("MONO", cachedValue, "#B2");
  133. Assert.IsFalse (aggregate.HasChanged, "#B3");
  134. Thread.Sleep (120);
  135. cachedValue = HttpRuntime.Cache.Get ("key") as string;
  136. Assert.IsNull (cachedValue, "#C1");
  137. Assert.IsFalse (aggregate.HasChanged, "#C2");
  138. }
  139. }
  140. }
  141. #endif