CacheTests.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. cacheItemUUID = UUID.Random();
  42. MemoryCacheItem cachedItem = new MemoryCacheItem(cacheItemUUID.ToString(),DateTime.Now + TimeSpan.FromDays(1));
  43. byte[] foo = new byte[1];
  44. foo[0] = 255;
  45. cachedItem.Store(foo);
  46. cache.Store(cacheItemUUID.ToString(), cachedItem);
  47. }
  48. [Test]
  49. public void TestRetreive()
  50. {
  51. CacheItemBase citem = (CacheItemBase)cache.Get(cacheItemUUID.ToString());
  52. byte[] data = (byte[]) citem.Retrieve();
  53. Assert.That(data.Length == 1, "Cached Item should have one byte element");
  54. Assert.That(data[0] == 255, "Cached Item element should be 255");
  55. }
  56. [Test]
  57. public void TestNotInCache()
  58. {
  59. UUID randomNotIn = UUID.Random();
  60. while (randomNotIn == cacheItemUUID)
  61. {
  62. randomNotIn = UUID.Random();
  63. }
  64. object citem = cache.Get(randomNotIn.ToString());
  65. Assert.That(citem == null, "Item should not be in Cache");
  66. }
  67. //NOTE: Test Case disabled until Cache is fixed
  68. [Test]
  69. public void TestTTLExpiredEntry()
  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.Get(cacheItemUUID.ToString());
  78. //object citem = cache.Get(cacheItemUUID.ToString());
  79. //Assert.That(citem == null, "Item should not be in Cache because the expiry time was before now");
  80. }
  81. //NOTE: Test Case disabled until Cache is fixed
  82. [Test]
  83. public void ExpireItemManually()
  84. {
  85. UUID ImmediateExpiryUUID = UUID.Random();
  86. MemoryCacheItem cachedItem = new MemoryCacheItem(ImmediateExpiryUUID.ToString(), 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.Invalidate(ImmediateExpiryUUID.ToString());
  92. cache.Get(cacheItemUUID.ToString());
  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. }
  97. }