FlotsamAssetCacheTests.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 System.Collections.Generic;
  29. using System.IO;
  30. using System.Reflection;
  31. using System.Threading;
  32. using log4net.Config;
  33. using Nini.Config;
  34. using NUnit.Framework;
  35. using OpenMetaverse;
  36. using OpenMetaverse.Assets;
  37. using Flotsam.RegionModules.AssetCache;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Region.Framework.Scenes.Serialization;
  41. using OpenSim.Tests.Common;
  42. using OpenSim.Tests.Common.Mock;
  43. namespace OpenSim.Region.CoreModules.Asset.Tests
  44. {
  45. /// <summary>
  46. /// At the moment we're only test the in-memory part of the FlotsamAssetCache. This is a considerable weakness.
  47. /// </summary>
  48. [TestFixture]
  49. public class FlotsamAssetCacheTests
  50. {
  51. protected TestScene m_scene;
  52. protected FlotsamAssetCache m_cache;
  53. [SetUp]
  54. public void SetUp()
  55. {
  56. IConfigSource config = new IniConfigSource();
  57. config.AddConfig("Modules");
  58. config.Configs["Modules"].Set("AssetCaching", "FlotsamAssetCache");
  59. config.AddConfig("AssetCache");
  60. config.Configs["AssetCache"].Set("FileCacheEnabled", "false");
  61. config.Configs["AssetCache"].Set("MemoryCacheEnabled", "true");
  62. m_cache = new FlotsamAssetCache();
  63. m_scene = new SceneHelpers().SetupScene();
  64. SceneHelpers.SetupSceneModules(m_scene, config, m_cache);
  65. }
  66. [Test]
  67. public void TestCacheAsset()
  68. {
  69. TestHelpers.InMethod();
  70. // log4net.Config.XmlConfigurator.Configure();
  71. AssetBase asset = AssetHelpers.CreateNotecardAsset();
  72. asset.ID = TestHelpers.ParseTail(0x1).ToString();
  73. // Check we don't get anything before the asset is put in the cache
  74. AssetBase retrievedAsset = m_cache.Get(asset.ID.ToString());
  75. Assert.That(retrievedAsset, Is.Null);
  76. m_cache.Store(asset);
  77. // Check that asset is now in cache
  78. retrievedAsset = m_cache.Get(asset.ID.ToString());
  79. Assert.That(retrievedAsset, Is.Not.Null);
  80. Assert.That(retrievedAsset.ID, Is.EqualTo(asset.ID));
  81. }
  82. [Test]
  83. public void TestExpireAsset()
  84. {
  85. TestHelpers.InMethod();
  86. // log4net.Config.XmlConfigurator.Configure();
  87. AssetBase asset = AssetHelpers.CreateNotecardAsset();
  88. asset.ID = TestHelpers.ParseTail(0x2).ToString();
  89. m_cache.Store(asset);
  90. m_cache.Expire(asset.ID);
  91. AssetBase retrievedAsset = m_cache.Get(asset.ID.ToString());
  92. Assert.That(retrievedAsset, Is.Null);
  93. }
  94. [Test]
  95. public void TestClearCache()
  96. {
  97. TestHelpers.InMethod();
  98. // log4net.Config.XmlConfigurator.Configure();
  99. AssetBase asset = AssetHelpers.CreateNotecardAsset();
  100. asset.ID = TestHelpers.ParseTail(0x2).ToString();
  101. m_cache.Store(asset);
  102. m_cache.Clear();
  103. AssetBase retrievedAsset = m_cache.Get(asset.ID.ToString());
  104. Assert.That(retrievedAsset, Is.Null);
  105. }
  106. }
  107. }