EventQueueTests.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Text;
  32. using log4net.Config;
  33. using Nini.Config;
  34. using NUnit.Framework;
  35. using OpenMetaverse;
  36. using OpenMetaverse.Packets;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Region.ClientStack.Linden;
  42. using OpenSim.Region.CoreModules.Framework;
  43. using OpenSim.Region.Framework.Scenes;
  44. using OpenSim.Region.OptionalModules.World.NPC;
  45. using OpenSim.Tests.Common;
  46. namespace OpenSim.Region.ClientStack.Linden.Tests
  47. {
  48. [TestFixture]
  49. public class EventQueueTests : OpenSimTestCase
  50. {
  51. private TestScene m_scene;
  52. private EventQueueGetModule m_eqgMod;
  53. private NPCModule m_npcMod;
  54. [SetUp]
  55. public override void SetUp()
  56. {
  57. base.SetUp();
  58. uint port = 9999;
  59. // This is an unfortunate bit of clean up we have to do because MainServer manages things through static
  60. // variables and the VM is not restarted between tests.
  61. MainServer.RemoveHttpServer(port);
  62. BaseHttpServer server = new BaseHttpServer(port, false, "","","");
  63. MainServer.AddHttpServer(server);
  64. MainServer.Instance = server;
  65. IConfigSource config = new IniConfigSource();
  66. config.AddConfig("Startup");
  67. CapabilitiesModule capsModule = new CapabilitiesModule();
  68. m_eqgMod = new EventQueueGetModule();
  69. // For NPC test support
  70. config.AddConfig("NPC");
  71. config.Configs["NPC"].Set("Enabled", "true");
  72. m_npcMod = new NPCModule();
  73. m_scene = new SceneHelpers().SetupScene();
  74. SceneHelpers.SetupSceneModules(m_scene, config, capsModule, m_eqgMod, m_npcMod);
  75. }
  76. [Test]
  77. public void TestAddForClient()
  78. {
  79. TestHelpers.InMethod();
  80. // log4net.Config.XmlConfigurator.Configure();
  81. SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  82. // TODO: Add more assertions for the other aspects of event queues
  83. Assert.That(MainServer.Instance.GetPollServiceHandlerKeys().Count, Is.EqualTo(1));
  84. }
  85. [Test]
  86. public void TestRemoveForClient()
  87. {
  88. TestHelpers.InMethod();
  89. // TestHelpers.EnableLogging();
  90. UUID spId = TestHelpers.ParseTail(0x1);
  91. SceneHelpers.AddScenePresence(m_scene, spId);
  92. m_scene.CloseAgent(spId, false);
  93. // TODO: Add more assertions for the other aspects of event queues
  94. Assert.That(MainServer.Instance.GetPollServiceHandlerKeys().Count, Is.EqualTo(0));
  95. }
  96. [Test]
  97. public void TestEnqueueMessage()
  98. {
  99. TestHelpers.InMethod();
  100. // log4net.Config.XmlConfigurator.Configure();
  101. ScenePresence sp = SceneHelpers.AddScenePresence(m_scene, TestHelpers.ParseTail(0x1));
  102. string messageName = "TestMessage";
  103. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), sp.UUID);
  104. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  105. // initial queue as null events
  106. // eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  107. if((int)eventsResponse["int_response_code"] != (int)HttpStatusCode.OK)
  108. {
  109. eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  110. if((int)eventsResponse["int_response_code"] != (int)HttpStatusCode.OK)
  111. eventsResponse = m_eqgMod.GetEvents(UUID.Zero, sp.UUID);
  112. }
  113. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.OK));
  114. // Console.WriteLine("Response [{0}]", (string)eventsResponse["str_response_string"]);
  115. string data = String.Empty;
  116. if(eventsResponse["bin_response_data"] != null)
  117. data = Encoding.UTF8.GetString((byte[])eventsResponse["bin_response_data"]);
  118. OSDMap rawOsd = (OSDMap)OSDParser.DeserializeLLSDXml(data);
  119. OSDArray eventsOsd = (OSDArray)rawOsd["events"];
  120. bool foundUpdate = false;
  121. foreach (OSD osd in eventsOsd)
  122. {
  123. OSDMap eventOsd = (OSDMap)osd;
  124. if (eventOsd["message"] == messageName)
  125. foundUpdate = true;
  126. }
  127. Assert.That(foundUpdate, Is.True, string.Format("Did not find {0} in response", messageName));
  128. }
  129. /// <summary>
  130. /// Test an attempt to put a message on the queue of a user that is not in the region.
  131. /// </summary>
  132. [Test]
  133. public void TestEnqueueMessageNoUser()
  134. {
  135. TestHelpers.InMethod();
  136. TestHelpers.EnableLogging();
  137. string messageName = "TestMessage";
  138. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), TestHelpers.ParseTail(0x1));
  139. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, TestHelpers.ParseTail(0x1));
  140. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.BadGateway));
  141. }
  142. /// <summary>
  143. /// NPCs do not currently have an event queue but a caller may try to send a message anyway, so check behaviour.
  144. /// </summary>
  145. [Test]
  146. public void TestEnqueueMessageToNpc()
  147. {
  148. TestHelpers.InMethod();
  149. // TestHelpers.EnableLogging();
  150. UUID npcId
  151. = m_npcMod.CreateNPC(
  152. "John", "Smith", new Vector3(128, 128, 30), UUID.Zero, true, m_scene, new AvatarAppearance());
  153. ScenePresence npc = m_scene.GetScenePresence(npcId);
  154. string messageName = "TestMessage";
  155. m_eqgMod.Enqueue(m_eqgMod.BuildEvent(messageName, new OSDMap()), npc.UUID);
  156. Hashtable eventsResponse = m_eqgMod.GetEvents(UUID.Zero, npc.UUID);
  157. Assert.That((int)eventsResponse["int_response_code"], Is.EqualTo((int)HttpStatusCode.BadGateway));
  158. }
  159. }
  160. }