CacheItemPriorityQueueTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Authors:
  2. // Marek Habersack <[email protected]>
  3. //
  4. // Copyright (C) 2010 Novell Inc. http://novell.com
  5. //
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the
  9. // "Software"), to deal in the Software without restriction, including
  10. // without limitation the rights to use, copy, modify, merge, publish,
  11. // distribute, sublicense, and/or sell copies of the Software, and to
  12. // permit persons to whom the Software is furnished to do so, subject to
  13. // the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. //
  26. using System;
  27. using System.Collections.Generic;
  28. using System.ComponentModel;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Web.Caching;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Web.Caching
  35. {
  36. [TestFixture]
  37. public partial class CacheItemPriorityQueueTest
  38. {
  39. const string DATA_DIR = "CacheItemPriorityQueueTestData";
  40. static readonly string dataDir;
  41. sealed class TestCacheItem : CacheItem
  42. {
  43. public Guid Guid;
  44. public TestCacheItem (string dataLine)
  45. {
  46. string[] data = dataLine.Split (',');
  47. int index = 0;
  48. Key = LoadField <string> (index++, data);
  49. AbsoluteExpiration = new DateTime (LoadField <long> (index++, data));
  50. SlidingExpiration = new TimeSpan (LoadField <long> (index++, data));
  51. Priority = LoadField <CacheItemPriority> (index++, data);
  52. LastChange = new DateTime (LoadField <long> (index++, data));
  53. ExpiresAt = LoadField <long> (index++, data);
  54. Disabled = LoadField <bool> (index++, data);
  55. Guid = new Guid (LoadField <string> (index++, data));
  56. if (data.Length > index)
  57. PriorityQueueIndex = LoadField <int> (index++, data);
  58. else
  59. PriorityQueueIndex = -1;
  60. }
  61. public override string ToString ()
  62. {
  63. return String.Format ("CacheItem [{0}]\n[{1}][{2}][{3}]", this.Guid, Key, Disabled, ExpiresAt > 0 ? new DateTime (ExpiresAt).ToString () : "0");
  64. }
  65. T LoadField <T> (int index, string[] data)
  66. {
  67. if (data == null || data.Length <= index)
  68. throw new ArgumentOutOfRangeException ("index");
  69. string s = data [index];
  70. if (String.IsNullOrEmpty (s))
  71. return default (T);
  72. TypeConverter cvt = TypeDescriptor.GetConverter (typeof (T));
  73. if (cvt == null)
  74. throw new InvalidOperationException (String.Format ("Cannot find converter for type {0}, field {1}", typeof (T), index));
  75. if (!cvt.CanConvertFrom (typeof (string)))
  76. throw new InvalidOperationException (String.Format ("Converter for type {0} cannot convert from string, field {1}", typeof (T), index));
  77. return (T)cvt.ConvertFromString (s);
  78. }
  79. }
  80. static CacheItemPriorityQueueTest ()
  81. {
  82. // Assumes this is compiled into mcs/class/lib/<profile>
  83. string class_dir = Directory.GetParent (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location)).Parent.FullName;
  84. string system_web_dir = Path.Combine (class_dir, "System.Web", "Test", "System.Web.Caching");
  85. dataDir = Path.Combine (system_web_dir, DATA_DIR);
  86. }
  87. void RunTest (string testsFileName, string listFileName)
  88. {
  89. var queue = new CacheItemPriorityQueue ();
  90. var cacheItems = new List <TestCacheItem> ();
  91. string listPath = Path.Combine (dataDir, listFileName);
  92. string testsPath = Path.Combine (dataDir, testsFileName);
  93. string line;
  94. using (var sr = new StreamReader (listPath, Encoding.UTF8)) {
  95. while ((line = sr.ReadLine ()) != null) {
  96. if (line [0] == '#')
  97. continue;
  98. cacheItems.Add (new TestCacheItem (line));
  99. }
  100. }
  101. using (var sr = new StreamReader (testsPath, Encoding.UTF8)) {
  102. int i = 0;
  103. while ((line = sr.ReadLine ()) != null) {
  104. if (line [0] == '#')
  105. continue;
  106. RunItem (new CacheItemPriorityQueueTestItem (line), queue, cacheItems, i++);
  107. }
  108. }
  109. }
  110. void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
  111. {
  112. TestCacheItem ci;
  113. string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
  114. switch (item.Operation) {
  115. case QueueOperation.Enqueue:
  116. queue.Enqueue (list [item.ListIndex]);
  117. Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "1");
  118. Assert.AreEqual (item.Guid, ((TestCacheItem)queue.Peek ()).Guid.ToString (), messagePrefix + "2");
  119. break;
  120. case QueueOperation.Dequeue:
  121. ci = (TestCacheItem)queue.Dequeue ();
  122. if (item.IsNull)
  123. Assert.IsNull (ci, messagePrefix + "1");
  124. else {
  125. Assert.IsNotNull (ci, messagePrefix + "2");
  126. Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
  127. Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
  128. }
  129. Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
  130. break;
  131. case QueueOperation.Disable:
  132. ci = list [item.ListIndex];
  133. if (item.IsNull)
  134. Assert.IsNull (ci, messagePrefix + "1");
  135. else {
  136. Assert.IsNotNull (ci, messagePrefix + "2");
  137. Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
  138. Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
  139. ci.Disabled = item.Disable;
  140. }
  141. break;
  142. case QueueOperation.Peek:
  143. ci = (TestCacheItem)queue.Peek ();
  144. if (item.IsNull)
  145. Assert.IsNull (ci, messagePrefix + "1");
  146. else {
  147. Assert.IsNotNull (ci, messagePrefix + "2");
  148. Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
  149. Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
  150. }
  151. Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
  152. break;
  153. case QueueOperation.QueueSize:
  154. Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
  155. break;
  156. case QueueOperation.Update:
  157. ci = list [item.ListIndex];
  158. queue.Update (ci);
  159. if (item.IsNull)
  160. Assert.IsNull (ci, messagePrefix + "1");
  161. else {
  162. Assert.IsNotNull (ci, messagePrefix + "2");
  163. Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
  164. Assert.AreEqual (item.ExpiresAt, ci.ExpiresAt, messagePrefix + "4");
  165. Assert.AreEqual (item.PriorityQueueIndex, ci.PriorityQueueIndex, messagePrefix + "5");
  166. }
  167. break;
  168. default:
  169. Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);
  170. break;
  171. }
  172. }
  173. }
  174. }