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