CacheItemPriorityQueueTest.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. dataDir =
  83. Path.Combine (
  84. Path.Combine (
  85. Path.Combine (Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location), "Test"),
  86. "System.Web.Caching"),
  87. DATA_DIR);
  88. }
  89. void RunTest (string testsFileName, string listFileName)
  90. {
  91. var queue = new CacheItemPriorityQueue ();
  92. var cacheItems = new List <TestCacheItem> ();
  93. string listPath = Path.Combine (dataDir, listFileName);
  94. string testsPath = Path.Combine (dataDir, testsFileName);
  95. string line;
  96. using (var sr = new StreamReader (listPath, Encoding.UTF8)) {
  97. while ((line = sr.ReadLine ()) != null) {
  98. if (line [0] == '#')
  99. continue;
  100. cacheItems.Add (new TestCacheItem (line));
  101. }
  102. }
  103. using (var sr = new StreamReader (testsPath, Encoding.UTF8)) {
  104. int i = 0;
  105. while ((line = sr.ReadLine ()) != null) {
  106. if (line [0] == '#')
  107. continue;
  108. RunItem (new CacheItemPriorityQueueTestItem (line), queue, cacheItems, i++);
  109. }
  110. }
  111. }
  112. void RunItem (CacheItemPriorityQueueTestItem item, CacheItemPriorityQueue queue, List <TestCacheItem> list, int testNum)
  113. {
  114. TestCacheItem ci;
  115. string messagePrefix = String.Format ("{0}-{1:00000}-{2:00000}-", item.Operation, item.OperationCount, testNum);
  116. switch (item.Operation) {
  117. case QueueOperation.Enqueue:
  118. queue.Enqueue (list [item.ListIndex]);
  119. Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "1");
  120. Assert.AreEqual (item.Guid, ((TestCacheItem)queue.Peek ()).Guid.ToString (), messagePrefix + "2");
  121. break;
  122. case QueueOperation.Dequeue:
  123. ci = (TestCacheItem)queue.Dequeue ();
  124. if (item.IsNull)
  125. Assert.IsNull (ci, messagePrefix + "1");
  126. else {
  127. Assert.IsNotNull (ci, messagePrefix + "2");
  128. Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
  129. Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
  130. }
  131. Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
  132. break;
  133. case QueueOperation.Disable:
  134. ci = list [item.ListIndex];
  135. if (item.IsNull)
  136. Assert.IsNull (ci, messagePrefix + "1");
  137. else {
  138. Assert.IsNotNull (ci, messagePrefix + "2");
  139. Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
  140. Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
  141. ci.Disabled = item.Disable;
  142. }
  143. break;
  144. case QueueOperation.Peek:
  145. ci = (TestCacheItem)queue.Peek ();
  146. if (item.IsNull)
  147. Assert.IsNull (ci, messagePrefix + "1");
  148. else {
  149. Assert.IsNotNull (ci, messagePrefix + "2");
  150. Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
  151. Assert.AreEqual (item.IsDisabled, ci.Disabled, messagePrefix + "4");
  152. }
  153. Assert.AreEqual (item.QueueCount, queue.Count, messagePrefix + "5");
  154. break;
  155. case QueueOperation.QueueSize:
  156. Assert.AreEqual (item.QueueCount, queue.Count, "Queue size after sequence");
  157. break;
  158. case QueueOperation.Update:
  159. ci = list [item.ListIndex];
  160. queue.Update (ci);
  161. if (item.IsNull)
  162. Assert.IsNull (ci, messagePrefix + "1");
  163. else {
  164. Assert.IsNotNull (ci, messagePrefix + "2");
  165. Assert.AreEqual (item.Guid, ci.Guid.ToString (), messagePrefix + "3");
  166. Assert.AreEqual (item.ExpiresAt, ci.ExpiresAt, messagePrefix + "4");
  167. Assert.AreEqual (item.PriorityQueueIndex, ci.PriorityQueueIndex, messagePrefix + "5");
  168. }
  169. break;
  170. default:
  171. Assert.Fail ("Unknown QueueOperation: {0}", item.Operation);
  172. break;
  173. }
  174. }
  175. }
  176. }