CacheTests.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. using OpenSim.Tests.Common;
  31. namespace OpenSim.Framework.Tests
  32. {
  33. [TestFixture]
  34. public class CacheTests : OpenSimTestCase
  35. {
  36. private Cache cache;
  37. private UUID cacheItemUUID;
  38. [SetUp]
  39. public void Build()
  40. {
  41. cache = new Cache();
  42. cache = new Cache(CacheMedium.Memory,CacheStrategy.Aggressive,CacheFlags.AllowUpdate);
  43. cacheItemUUID = UUID.Random();
  44. MemoryCacheItem cachedItem = new MemoryCacheItem(cacheItemUUID.ToString(),DateTime.Now + TimeSpan.FromDays(1));
  45. byte[] foo = new byte[1];
  46. foo[0] = 255;
  47. cachedItem.Store(foo);
  48. cache.Store(cacheItemUUID.ToString(), cachedItem);
  49. }
  50. [Test]
  51. public void TestRetreive()
  52. {
  53. CacheItemBase citem = (CacheItemBase)cache.Get(cacheItemUUID.ToString());
  54. byte[] data = (byte[]) citem.Retrieve();
  55. Assert.That(data.Length == 1, "Cached Item should have one byte element");
  56. Assert.That(data[0] == 255, "Cached Item element should be 255");
  57. }
  58. [Test]
  59. public void TestNotInCache()
  60. {
  61. UUID randomNotIn = UUID.Random();
  62. while (randomNotIn == cacheItemUUID)
  63. {
  64. randomNotIn = UUID.Random();
  65. }
  66. object citem = cache.Get(randomNotIn.ToString());
  67. Assert.That(citem == null, "Item should not be in Cache");
  68. }
  69. [Test]
  70. public void ExpireItemManually()
  71. {
  72. UUID ImmediateExpiryUUID = UUID.Random();
  73. MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), TimeSpan.FromDays(1));
  74. byte[] foo = new byte[1];
  75. foo[0] = 1;
  76. cachedItem.Store(foo);
  77. cache.Store(cacheItemUUID.ToString(), cachedItem);
  78. cache.Invalidate(cacheItemUUID.ToString());
  79. cache.Get(cacheItemUUID.ToString());
  80. object citem = cache.Get(cacheItemUUID.ToString());
  81. Assert.That(citem == null, "Item should not be in Cache because we manually invalidated it");
  82. }
  83. [Test]
  84. public void ClearCacheTest()
  85. {
  86. UUID ImmediateExpiryUUID = UUID.Random();
  87. MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), DateTime.Now - TimeSpan.FromDays(1));
  88. byte[] foo = new byte[1];
  89. foo[0] = 1;
  90. cachedItem.Store(foo);
  91. cache.Store(cacheItemUUID.ToString(), cachedItem);
  92. cache.Clear();
  93. object citem = cache.Get(cacheItemUUID.ToString());
  94. Assert.That(citem == null, "Item should not be in Cache because we manually invalidated it");
  95. }
  96. [Test]
  97. public void CacheItemMundane()
  98. {
  99. UUID Random1 = UUID.Random();
  100. UUID Random2 = UUID.Random();
  101. byte[] data = Array.Empty<byte>();
  102. CacheItemBase cb1 = new CacheItemBase(Random1.ToString(), DateTime.Now + TimeSpan.FromDays(1));
  103. CacheItemBase cb2 = new CacheItemBase(Random2.ToString(), DateTime.Now + TimeSpan.FromDays(1));
  104. CacheItemBase cb3 = new CacheItemBase(Random1.ToString(), DateTime.Now + TimeSpan.FromDays(1));
  105. cb1.Store(data);
  106. Assert.That(cb1.Equals(cb3), "cb1 should equal cb3, their uuids are the same");
  107. Assert.That(!cb2.Equals(cb1), "cb2 should not equal cb1, their uuids are NOT the same");
  108. Assert.That(cb1.IsLocked() == false, "CacheItemBase default is false");
  109. Assert.That(cb1.Retrieve() == null, "Virtual Retrieve method should return null");
  110. }
  111. }
  112. }