1
0

LSL_ApiUserTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.Threading;
  30. using Nini.Config;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Region.ScriptEngine.Shared.Api;
  36. using OpenSim.Region.ScriptEngine.Shared.ScriptBase;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Tests.Common;
  39. namespace OpenSim.Region.ScriptEngine.Shared.Tests
  40. {
  41. [TestFixture]
  42. public class LSL_ApiUserTests : OpenSimTestCase
  43. {
  44. private Scene m_scene;
  45. private MockScriptEngine m_engine;
  46. [SetUp]
  47. public override void SetUp()
  48. {
  49. base.SetUp();
  50. m_engine = new MockScriptEngine();
  51. m_scene = new SceneHelpers().SetupScene();
  52. SceneHelpers.SetupSceneModules(m_scene, m_engine);
  53. }
  54. [Test]
  55. public void TestLlRequestAgentDataOnline()
  56. {
  57. TestHelpers.InMethod();
  58. // TestHelpers.EnableLogging();
  59. UUID userId = TestHelpers.ParseTail(0x1);
  60. UserAccount ua1 = UserAccountHelpers.CreateUserWithInventory(m_scene, userId);
  61. SceneObjectPart part = SceneHelpers.AddSceneObject(m_scene).RootPart;
  62. TaskInventoryItem scriptItem = TaskInventoryHelpers.AddScript(m_scene.AssetService, part);
  63. LSL_Api apiGrp1 = new LSL_Api();
  64. apiGrp1.Initialize(m_engine, part, scriptItem);
  65. // Initially long timeout to test cache
  66. apiGrp1.LlRequestAgentDataCacheTimeoutMs = 20000;
  67. // Offline test
  68. {
  69. apiGrp1.llRequestAgentData(userId.ToString(), ScriptBaseClass.DATA_ONLINE);
  70. Assert.That(m_engine.PostedEvents.ContainsKey(scriptItem.ItemID));
  71. List<EventParams> events = m_engine.PostedEvents[scriptItem.ItemID];
  72. Assert.That(events.Count, Is.EqualTo(1));
  73. EventParams eventParams = events[0];
  74. Assert.That(eventParams.EventName, Is.EqualTo("dataserver"));
  75. string data = eventParams.Params[1].ToString();
  76. Assert.AreEqual(0, int.Parse(data));
  77. m_engine.PostedEvents.Clear();
  78. }
  79. // Online test. Should get the 'wrong' result because of caching.
  80. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, ua1);
  81. {
  82. apiGrp1.llRequestAgentData(userId.ToString(), ScriptBaseClass.DATA_ONLINE);
  83. Assert.That(m_engine.PostedEvents.ContainsKey(scriptItem.ItemID));
  84. List<EventParams> events = m_engine.PostedEvents[scriptItem.ItemID];
  85. Assert.That(events.Count, Is.EqualTo(1));
  86. EventParams eventParams = events[0];
  87. Assert.That(eventParams.EventName, Is.EqualTo("dataserver"));
  88. string data = eventParams.Params[1].ToString();
  89. Assert.AreEqual(0, int.Parse(data));
  90. m_engine.PostedEvents.Clear();
  91. }
  92. apiGrp1.LlRequestAgentDataCacheTimeoutMs = 20;
  93. // Make absolutely sure that we should trigger cache timeout.
  94. Thread.Sleep(apiGrp1.LlRequestAgentDataCacheTimeoutMs + 50);
  95. {
  96. apiGrp1.llRequestAgentData(userId.ToString(), ScriptBaseClass.DATA_ONLINE);
  97. Assert.That(m_engine.PostedEvents.ContainsKey(scriptItem.ItemID));
  98. List<EventParams> events = m_engine.PostedEvents[scriptItem.ItemID];
  99. Assert.That(events.Count, Is.EqualTo(1));
  100. EventParams eventParams = events[0];
  101. Assert.That(eventParams.EventName, Is.EqualTo("dataserver"));
  102. string data = eventParams.Params[1].ToString();
  103. Assert.AreEqual(1, int.Parse(data));
  104. m_engine.PostedEvents.Clear();
  105. }
  106. m_scene.CloseAgent(userId, false);
  107. Thread.Sleep(apiGrp1.LlRequestAgentDataCacheTimeoutMs + 50);
  108. {
  109. apiGrp1.llRequestAgentData(userId.ToString(), ScriptBaseClass.DATA_ONLINE);
  110. Assert.That(m_engine.PostedEvents.ContainsKey(scriptItem.ItemID));
  111. List<EventParams> events = m_engine.PostedEvents[scriptItem.ItemID];
  112. Assert.That(events.Count, Is.EqualTo(1));
  113. EventParams eventParams = events[0];
  114. Assert.That(eventParams.EventName, Is.EqualTo("dataserver"));
  115. string data = eventParams.Params[1].ToString();
  116. Assert.AreEqual(0, int.Parse(data));
  117. m_engine.PostedEvents.Clear();
  118. }
  119. }
  120. }
  121. }