CacheTests.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using NUnit.Framework;
  29. using OpenMetaverse;
  30. namespace OpenSim.Framework.Tests
  31. {
  32. [TestFixture]
  33. public class CacheTests
  34. {
  35. private Cache cache;
  36. private UUID cacheItemUUID;
  37. [SetUp]
  38. public void Build()
  39. {
  40. cache = new Cache();
  41. cache = new Cache(CacheMedium.Memory,CacheStrategy.Aggressive,CacheFlags.AllowUpdate);
  42. cacheItemUUID = UUID.Random();
  43. MemoryCacheItem cachedItem = new MemoryCacheItem(cacheItemUUID.ToString(),DateTime.Now + TimeSpan.FromDays(1));
  44. byte[] foo = new byte[1];
  45. foo[0] = 255;
  46. cachedItem.Store(foo);
  47. cache.Store(cacheItemUUID.ToString(), cachedItem);
  48. }
  49. [Test]
  50. public void TestRetreive()
  51. {
  52. CacheItemBase citem = (CacheItemBase)cache.Get(cacheItemUUID.ToString());
  53. byte[] data = (byte[]) citem.Retrieve();
  54. Assert.That(data.Length == 1, "Cached Item should have one byte element");
  55. Assert.That(data[0] == 255, "Cached Item element should be 255");
  56. }
  57. [Test]
  58. public void TestNotInCache()
  59. {
  60. UUID randomNotIn = UUID.Random();
  61. while (randomNotIn == cacheItemUUID)
  62. {
  63. randomNotIn = UUID.Random();
  64. }
  65. object citem = cache.Get(randomNotIn.ToString());
  66. Assert.That(citem == null, "Item should not be in Cache");
  67. }
  68. [Test]
  69. public void ExpireItemManually()
  70. {
  71. UUID ImmediateExpiryUUID = UUID.Random();
  72. MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), TimeSpan.FromDays(1));
  73. byte[] foo = new byte[1];
  74. foo[0] = 1;
  75. cachedItem.Store(foo);
  76. cache.Store(cacheItemUUID.ToString(), cachedItem);
  77. cache.Invalidate(cacheItemUUID.ToString());
  78. cache.Get(cacheItemUUID.ToString());
  79. object citem = cache.Get(cacheItemUUID.ToString());
  80. Assert.That(citem == null, "Item should not be in Cache because we manually invalidated it");
  81. }
  82. [Test]
  83. public void ClearCacheTest()
  84. {
  85. UUID ImmediateExpiryUUID = UUID.Random();
  86. MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), DateTime.Now - TimeSpan.FromDays(1));
  87. byte[] foo = new byte[1];
  88. foo[0] = 1;
  89. cachedItem.Store(foo);
  90. cache.Store(cacheItemUUID.ToString(), cachedItem);
  91. cache.Clear();
  92. object citem = cache.Get(cacheItemUUID.ToString());
  93. Assert.That(citem == null, "Item should not be in Cache because we manually invalidated it");
  94. }
  95. [Test]
  96. public void CacheItemMundane()
  97. {
  98. UUID Random1 = UUID.Random();
  99. UUID Random2 = UUID.Random();
  100. byte[] data = new byte[0];
  101. CacheItemBase cb1 = new CacheItemBase(Random1.ToString(), DateTime.Now + TimeSpan.FromDays(1));
  102. CacheItemBase cb2 = new CacheItemBase(Random2.ToString(), DateTime.Now + TimeSpan.FromDays(1));
  103. CacheItemBase cb3 = new CacheItemBase(Random1.ToString(), DateTime.Now + TimeSpan.FromDays(1));
  104. cb1.Store(data);
  105. Assert.That(cb1.Equals(cb3), "cb1 should equal cb3, their uuids are the same");
  106. Assert.That(!cb2.Equals(cb1), "cb2 should not equal cb1, their uuids are NOT the same");
  107. Assert.That(cb1.IsLocked() == false, "CacheItemBase default is false");
  108. Assert.That(cb1.Retrieve() == null, "Virtual Retrieve method should return null");
  109. }
  110. }
  111. }